-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy.sh
executable file
·109 lines (92 loc) · 2.51 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /bin/bash
# Check .env file
DOT_ENV=$1
if [ -f $DOT_ENV ]
then
set -a; source $DOT_ENV; set +a
else
echo "Run: ./scripts/deploy.sh <.env_file>"
echo "Please create $DOT_ENV file first and try again"
exit 1
fi
function create_state_bucket {
# $1 region
# $2 bucket_name
aws s3 mb s3://$2 --region $1
aws s3api put-bucket-versioning \
--bucket $2 \
--versioning-configuration Status=Enabled
}
function create_dynamo_db {
# $1 region
# $2 table_name
aws dynamodb create-table \
--table-name $2 \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--region $1
}
function generate_terraform_variables {
tf_vars=(tf tfvars)
for tf_var in "${tf_vars[@]}"; do
(
echo "cat <<EOF"
cat terraform.${tf_var}.tmpl
echo EOF
) | sh > terraform.${tf_var}
done
}
function check_create_remote_state {
# $1 aws_region
# $2 bucket name
# $3 dynamotable_name
AWS_REGION=$1
STATE_BUCKET_NAME=$2
STATE_DYNAMO_TABLE=$3
bucketstatus=$(aws s3api head-bucket --bucket $STATE_BUCKET_NAME 2>&1)
table_exists=$(aws dynamodb describe-table --table-name $STATE_DYNAMO_TABLE --region $AWS_REGION 2>&1)
if echo "${table_exists}" | grep "An error";
then
echo "Creating dynamodb table for TF state"
create_dynamo_db $AWS_REGION $STATE_DYNAMO_TABLE
else
echo "DynamoDB $STATE_DYNAMO_TABLE exists. Continue..."
fi
if echo "${bucketstatus}" | grep 'Not Found';
then
echo "Creating TF remote state"
create_state_bucket $AWS_REGION $STATE_BUCKET_NAME
create_dynamo_db $AWS_REGION $STATE_DYNAMO_TABLE
elif echo "${bucketstatus}" | grep 'Forbidden';
then
echo "Bucket $STATE_BUCKET_NAME exists but not owned"
exit 1
elif echo "${bucketstatus}" | grep 'Bad Request';
then
echo "Bucket $STATE_BUCKET_NAME specified is less than 3 or greater than 63 characters"
exit 1
else
echo "State Bucket $STATE_BUCKET_NAME owned and exists. Continue...";
echo "State Dynamo table $STATE_DYNAMO_TABLE owned and exists. Continue...";
fi
}
cd ./infrastructure
generate_terraform_variables
check_create_remote_state $AWS_REGION $STATE_BUCKET_NAME $STATE_DYNAMO_TABLE
read -rp 'action [init|plan|deploy]: ' ACTION
case $ACTION in
init)
terraform init
;;
plan)
terraform plan
;;
deploy)
terraform apply --auto-approve
;;
*)
echo "Chose from 'init', 'plan' or 'deploy'"
exit 1
;;
esac