Skip to content

Commit 0aaa1d4

Browse files
authored
[tfstate-backend] Update scripts to use true/false (#60)
* Update logic
1 parent 028370c commit 0aaa1d4

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Remove all versions and delete markers for each object
4+
OBJECT_VERSIONS=$(aws --output text s3api list-object-versions --bucket "$1" | grep -E '^VERSIONS|^DELETEMARKERS')
5+
6+
if [ $? -ne 0 ]; then
7+
echo "Aborting"
8+
exit 1
9+
fi
10+
11+
while read -r OBJECT_VERSION; do
12+
if [[ "$OBJECT_VERSION" == DELETEMARKERS* ]]; then
13+
KEY=$(echo $OBJECT_VERSION | awk '{print $3}')
14+
VERSION_ID=$(echo $OBJECT_VERSION | awk '{print $5}')
15+
else
16+
KEY=$(echo $OBJECT_VERSION | awk '{print $4}')
17+
VERSION_ID=$(echo $OBJECT_VERSION | awk '{print $8}')
18+
fi
19+
if [ -n "${KEY}" ] && [ -n "${VERSION_ID}" ]; then
20+
aws s3api delete-object --bucket $1 --key $KEY --version-id $VERSION_ID >/dev/null
21+
fi
22+
done <<< "$OBJECT_VERSIONS"
23+
24+
# Remove the bucket with --force option to remove any remaining files without versions.
25+
aws s3 rb --force s3://$1

aws/tfstate-backend/scripts/destroy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
#!/usr/bin/env bash
2+
# This script destroys the tfstate backend
3+
4+
# We use this variable consistently to pass the role we wish to assume in our root modules
5+
export TF_VAR_aws_assume_role_arn="${TF_VAR_aws_assume_role_arn:-false}"
6+
7+
DISABLE_ROLE_ARN=${DISABLE_ROLE_ARN:-false}
8+
19
# Start with a clean slate
210
rm -rf .terraform terraform.tfstate
311

12+
# Disable Role ARN (necessary for root account when using master credentials)
13+
[ "${DISABLE_ROLE_ARN}" == "true" ] || sed -Ei 's/^(\s+role_arn\s+)/#\1/' main.tf
14+
415
# Init terraform with S3 state enabled. Assumes state was previously initialized.
516
init-terraform
617

@@ -26,5 +37,8 @@ terraform destroy -auto-approve
2637
# Re-enable S3 backend
2738
sed -Ei 's/^#(\s+backend\s+)/\1/' main.tf
2839

40+
# Re-enable Role ARN
41+
[ "${DISABLE_ROLE_ARN}" == "true" ] || sed -Ei 's/^#(\s+role_arn\s+)/\1/' main.tf
42+
2943
# Clean up
3044
rm -rf .terraform terraform.tfstate

aws/tfstate-backend/scripts/init.sh

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
#!/usr/bin/env bash
22
# This script automates the cold-start process of provisioning the Terraform state backend using terraform
33

4-
DISABLE_ROLE_ARN=${DISABLE_ROLE_ARN:-0}
4+
set -e
5+
6+
# We use this variable consistently to pass the role we wish to assume in our root modules
7+
export TF_VAR_aws_assume_role_arn="${TF_VAR_aws_assume_role_arn:-false}"
8+
9+
DISABLE_ROLE_ARN=${DISABLE_ROLE_ARN:-false}
510

611
# Start from a clean slate
712
rm -rf .terraform terraform.tfstate
813

9-
# Disable S3 backend
14+
# Disable S3 backend. We'll import state afterwards.
1015
sed -Ei 's/^(\s+backend\s+)/#\1/' main.tf
1116

1217
# Disable Role ARN (necessary for root account on cold-start)
13-
[ "${DISABLE_ROLE_ARN}" == "0" ] || sed -Ei 's/^(\s+role_arn\s+)/#\1/' main.tf
18+
[ "${DISABLE_ROLE_ARN}" == "true" ] || sed -Ei 's/^(\s+role_arn\s+)/#\1/' main.tf
1419

1520
# Initialize terraform modules and providers
1621
init-terraform
1722

1823
# Provision S3 bucket and dynamodb tables
19-
terraform apply -auto-approve
24+
terraform apply -auto-approve -input=false
2025

21-
export TF_BUCKET=$(terraform output -json | jq -r .tfstate_backend_s3_bucket_id.value)
22-
export TF_DYNAMODB_TABLE=$(terraform output -json | jq -r .tfstate_backend_dynamodb_table_id.value)
26+
export TF_BUCKET=$(terraform output tfstate_backend_s3_bucket_id)
27+
export TF_DYNAMODB_TABLE=$(terraform output tfstate_backend_dynamodb_table_id)
2328
export TF_BUCKET_REGION=${TF_VAR_region}
2429

2530
# Re-enable S3 backend
@@ -29,7 +34,7 @@ sed -Ei 's/^#(\s+backend\s+)/\1/' main.tf
2934
echo "yes" | init-terraform
3035

3136
# Re-enable Role ARN
32-
[ "${DISABLE_ROLE_ARN}" == "0" ] || sed -Ei 's/^#(\s+role_arn\s+)/\1/' main.tf
37+
[ "${DISABLE_ROLE_ARN}" == "true" ] || sed -Ei 's/^#(\s+role_arn\s+)/\1/' main.tf
3338

3439
# Describe how to use the S3/DynamoDB resources with Geodesic
3540
echo "Add the following to the Geodesic Module's Dockerfile:"

0 commit comments

Comments
 (0)