diff --git a/modules/common/shell/bash/scripts/terraform-init.sh b/modules/common/shell/bash/scripts/terraform-init.sh new file mode 100755 index 0000000..224d461 --- /dev/null +++ b/modules/common/shell/bash/scripts/terraform-init.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +export AWS_PROFILE="gs" + +BUCKET_NAME_PART_1="t2" +BUCKET_NAME_PART_2="global" +BUCKET_NAME_PART_3="terraformstate" + +WORKFLOW_FILE=".github/workflows/terraform.yml" + +if [ ! -f $WORKFLOW_FILE ]; then + WORKFLOW_FILE=".github/workflows/apply.yml" +fi + +AWS_ACCOUNT_NUMBER=$( + awk '/aws_account_number: .*/ {print $2}' $WORKFLOW_FILE | # Grab account number + echo "$( + read -r s + s=${s//\'/} + echo "$s" + )" # Remove single quote if it exists +) + +if [ -z "${AWS_ACCOUNT_NUMBER}" ]; then + AWS_ACCOUNT_NUMBER=$( + awk '/AWS_ACCOUNT_NUMBER: .*/ {print $2}' $WORKFLOW_FILE | # Grab account number + echo "$( + read -r s + s=${s//\'/} + echo "$s" + )" # Remove single quote if it exists + ) +fi + +REPOSITORY=$( + git remote get-url origin | + awk -F'/' -v OFS='/' '{print $(NF-1),$NF }' | + echo "$( + read -r s + s=${s%.git} + echo "$s" + )" # Remove .git suffix if it exists +) +BRANCH=$(git branch --show-current) + +terraform init \ + -backend-config="region=us-east-1" \ + -backend-config="bucket=${BUCKET_NAME_PART_1}${BUCKET_NAME_PART_2}${BUCKET_NAME_PART_3}" \ + -backend-config="workspace_key_prefix=accounts/${AWS_ACCOUNT_NUMBER}/${REPOSITORY}" \ + -backend-config="key=state.tfstate" \ + -backend-config="dynamodb_table=global-tf-state-lock" \ + -upgrade + +terraform workspace select "$BRANCH"