|
1 | 1 | #!/usr/bin/env bash |
| 2 | +# This script automates the cold-start process of provisioning the Terraform state backend using terraform |
2 | 3 |
|
3 | 4 | DISABLE_ROLE_ARN=${DISABLE_ROLE_ARN:-0} |
4 | 5 |
|
| 6 | +# Start from a clean slate |
| 7 | +rm -rf .terraform terraform.tfstate |
| 8 | + |
| 9 | +# Disable S3 backend |
5 | 10 | sed -Ei 's/^(\s+backend\s+)/#\1/' main.tf |
| 11 | + |
| 12 | +# Disable Role ARN (necessary for root account on cold-start) |
6 | 13 | [ "${DISABLE_ROLE_ARN}" == "0" ] || sed -Ei 's/^(\s+role_arn\s+)/#\1/' main.tf |
7 | 14 |
|
| 15 | +# Initialize terraform modules and providers |
8 | 16 | init-terraform |
9 | | -echo "yes" | terraform apply |
| 17 | + |
| 18 | +# Provision S3 bucket and dynamodb tables |
| 19 | +terraform apply -auto-approve |
10 | 20 |
|
11 | 21 | export TF_BUCKET=$(terraform output -json | jq -r .tfstate_backend_s3_bucket_id.value) |
12 | 22 | export TF_DYNAMODB_TABLE=$(terraform output -json | jq -r .tfstate_backend_dynamodb_table_id.value) |
13 | 23 | export TF_BUCKET_REGION=${TF_VAR_region} |
14 | 24 |
|
| 25 | +# Re-enable S3 backend |
15 | 26 | sed -Ei 's/^#(\s+backend\s+)/\1/' main.tf |
16 | 27 |
|
| 28 | +# Reinitialize terraform to import state to remote backend |
17 | 29 | echo "yes" | init-terraform |
18 | 30 |
|
| 31 | +# Re-enable Role ARN |
19 | 32 | [ "${DISABLE_ROLE_ARN}" == "0" ] || sed -Ei 's/^#(\s+role_arn\s+)/\1/' main.tf |
20 | 33 |
|
| 34 | +# Describe how to use the S3/DynamoDB resources with Geodesic |
21 | 35 | echo "Add the following to the Geodesic Module's Dockerfile:" |
22 | 36 | echo "#----------------------------------------------" |
23 | 37 | echo "ENV TF_BUCKET=\"${TF_BUCKET}\"" |
24 | 38 | echo "ENV TF_BUCKET_REGION=\"${TF_BUCKET_REGION}\"" |
25 | 39 | echo "ENV TF_DYNAMODB_TABLE=\"${TF_DYNAMODB_TABLE}\"" |
26 | 40 | echo "#----------------------------------------------" |
27 | | -echo "And rebuild the module" |
| 41 | +echo "...and rebuild the module" |
0 commit comments