Skip to content

Instantly share code, notes, and snippets.

@r8or0pz
Last active December 23, 2021 16:03
Show Gist options
  • Save r8or0pz/9e879e0b6060c90bd4d4ca314b5d9b07 to your computer and use it in GitHub Desktop.
Save r8or0pz/9e879e0b6060c90bd4d4ca314b5d9b07 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
env=${1}
project_name=''
bucket_name=''
path='terraform'
init_file="${path}/init.tf"
region=''
lock_table="${project_name}-locks"
aws s3 ls s3://${bucket_name} &> /dev/null || aws s3 mb s3://${bucket_name}
aws s3api put-bucket-versioning --bucket $bucket_name --versioning-configuration=Enabled
aws dynamodb describe-table --table-name $lock_table &> /dev/null || \
aws dynamodb create-table --table-name $lock_table \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema KeyType=HASH,AttributeName=LockID \
--provisioned-hroughput WriteCapacityUnits=1,ReadCapacityUnits=1
cat <<EOF > $init_file
terraform {
backend "s3" {
bucket = "$bucket_name"
key = "terraform"
region = "$region"
dynamodb_table = "$lock_table"
}
}
EOF
pushd $path
rm -rf .terraform
wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh
chmod +x ./install.sh
b=`pwd`/.bin
./install.sh -b $b
rm -v ./install.sh
$b/tfswitch -b $b/terraform
$b/terraform -v
$b/terraform init -upgrade=true
$b/terraform workspace select $env || $b/terraform workspace new $env
$b/terraform plan -out plan.out -var "build_bucket=$bucket_name"
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment