-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.sh
87 lines (74 loc) · 2.43 KB
/
terraform.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
#!/bin/bash
set -e
show_help() {
cat >/dev/stdout <<END
$0 -b <bucket_name> -r <primary_region> -k <tf_state_s3_path> -R <region_of_infra> -I <extra_init_options> -P <extra_plan_options> [ -d true # to destroy | -i true # to init only | -D true # for debugging mode]
Example:
$0 -b my-cool-bucket -r eu-west-1 -k eu-west-1/dev/sevice/terraform.tfstate -I "-plugin-dir=/providers" -I no-color -P "-var-file=dev-env.tfvars"
$0 -b my-cool-bucket -r eu-west-1 -k eu-west-1/dev/sevice/terraform.tfstate -d true # DESTROY
$0 -b my-cool-bucket -r eu-west-1 -k eu-west-1/dev/sevice/terraform.tfstate -i true # INIT ONLY (debugging, allows you to run your normal terraform commands afterwards on local machines)
END
exit 0
}
## Defaults
BUCKET_KEY="" ## Example of how you can have a structure to your tfstate files
STATE_BUCKET_NAME=""
STATE_BUCKET_REGION="us-east-2"
TF_IN_AUTOMATION="1"
export TF_IN_AUTOMATION
while getopts ":hb:k:d:i:r:I:P:D:" opt; do
case ${opt} in
b) STATE_BUCKET_NAME=${OPTARG} ;;
k) BUCKET_KEY=${OPTARG} ;;
r) STATE_BUCKET_REGION=${OPTARG} ;;
i) INIT_ONLY=${OPTARG} ;;
d) DESTROY=${OPTARG} ;;
D) DEBUGGING=${OPTARG} ;;
I) EXTRA_INIT+=("$OPTARG") ;;
P) EXTRA_PLAN+=("$OPTARG") ;;
h | \?)
show_help
;;
esac
done
shift $((OPTIND - 1))
init() {
echo "---- Launching Terraform init ---"
terraform init -backend-config="key=${BUCKET_KEY}" \
-backend-config="bucket=${STATE_BUCKET_NAME}" \
-backend-config="region=${STATE_BUCKET_REGION}" \
-backend=true -force-copy -get=true -input=false -reconfigure "${EXTRA_INIT[@]}"
}
plan() {
set -e
echo "---- Launching Terraform plan ---"
terraform plan -refresh=true -lock=true -no-color \
-out terraform.plan "${EXTRA_PLAN[@]}"
}
apply() {
set -e
echo "---- Launching Terraform apply ---"
terraform apply -no-color -lock=true terraform.plan
}
destroy() {
set -e
echo "---- Launching Terraform destroy ---"
terraform destroy -no-color -lock=true terraform.plan
}
if [[ ${DEBUGGING} == "true" ]]; then
export TF_LOG="DEBUG"
echo "Terraform options: Running with TF_LOG set to: ${TF_LOG}"
fi
echo "Terraform options: Running with TF_IN_AUTOMATION set to : ${TF_IN_AUTOMATION}"
if [[ ${INIT_ONLY} == "true" ]]; then
echo " ## ONLY RUNNING TF INIT FOR LOCAL DEBUGGING ##"
init
elif [[ ${DESTROY} == "true" ]]; then
echo "RUNNING DESTROY!!!!!!!!! You have 10 seconds..."
sleep 10
else
init
plan
apply
fi
exit 0