tfinit shortcut script

This commit is contained in:
Noah Masur 2024-06-13 15:59:10 +01:00
parent b051885b52
commit a4c9813341
No known key found for this signature in database

View File

@ -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"