Skip to content

Instantly share code, notes, and snippets.

@myclau
Created September 13, 2022 02:56
Show Gist options
  • Save myclau/644b786751f29c69a3e5928e58350073 to your computer and use it in GitHub Desktop.
Save myclau/644b786751f29c69a3e5928e58350073 to your computer and use it in GitHub Desktop.
Terrafrom init script for new project
#!/bin/bash
DEFAULT_BACKEND_S3BUCKET="<s3-bucket-name>"
DEFAULT_BACKEND_PROFILE="<s3-bucket-aws-profile>"
DEFAULT_BACKEND_REGION="<s3-bucket-region>"
DEFAULT_TERRAFORM_PROFILE="<default-aws-profile>"
DEFAULT_TERRAFORM_REGION="<default-aws-region>"
GIT_REMOTE_URL=$(git config --get remote.origin.url)
WORKSPACE_KEY_PREFIX=${GIT_REMOTE_URL##*'terraform/'}
WORKSPACE_KEY_PREFIX=${WORKSPACE_KEY_PREFIX//'.git'/}
echo "[Checking] Project namespace: $WORKSPACE_KEY_PREFIX"
cat << EOF > ./backend.tf
terraform {
backend "s3" {
bucket = "$DEFAULT_BACKEND_S3BUCKET"
key = "terraform.tfstate"
shared_credentials_file = "~/.aws/credentials"
profile = "$DEFAULT_BACKEND_PROFILE"
region = "$DEFAULT_BACKEND_REGION"
workspace_key_prefix = "$WORKSPACE_KEY_PREFIX"
}
}
EOF
echo "[Checking] backend.tf updated"
cat << EOF > ./providers.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
shared_credentials_files = ["~/.aws/credentials"]
profile = var.aws_profile
region = var.region
}
EOF
echo "[Checking] providers.tf updated"
cat << EOF > ./variables.tf
variable "aws_profile" {
type = string
}
variable "region" {
type = string
}
EOF
echo "[Checking] variables.tf updated"
cat << EOF > ./.gitignore
.terraform.lock.hcl
.terraform
.DS_Store
EOF
echo "[Checking] .gitignore updated"
mkdir -p ./values
cat << EOF > ./values/uat.tfvars
aws_profile = "$DEFAULT_TERRAFORM_PROFILE"
region = "$DEFAULT_TERRAFORM_REGION"
EOF
echo "[Checking] values/uat.tfvars updated"
cat << EOF > ./terraform.sh
#!/bin/bash
#This is for auto select workspace with values file
ACTION=\$1
WORKSPACE=\$2
if [ "\$ACTION" == "init" ]; then
terraform init
exit 0
fi
echo "Current Workspace:"
terraform workspace list
terraform workspace select $WORKSPACE
if [ "$?" != 0 ]; then
echo "Workspace $WORKSPACE not exist, do you want to create? (Only yes to create) : "
read create
if [ "$create" == "yes" ]; then
terraform workspace new $WORKSPACE
else
echo "Exit"
exit 0
fi
fi
case "\$ACTION" in
plan)
terraform plan -var-file ./values/\$WORKSPACE.tfvars
;;
apply)
terraform apply -var-file ./values/\$WORKSPACE.tfvars
;;
destroy)
terraform destroy -var-file ./values/\$WORKSPACE.tfvars
;;
output)
jsoncontext=\$(terraform output -json)
echo \$jsoncontext | jq .
echo \$jsoncontext | jq -r '. | to_entries[] | "\(.key) = \(.value | .value)"'
;;
*)
echo "Do you mean destory?"
;;
esac
EOF
chmod +x ./terraform.sh
echo "[Checking] terraform.sh updated"
mkdir -p ".vscode"
cat << EOF > ./.vscode/settings.json
{
"editor.formatOnSave": false,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true,
},
"files.associations": {
"*.tfvars": "terraform"
},
"[yaml]": {
"editor.autoIndent": "advanced",
"editor.tabSize": 2,
"editor.defaultFormatter": "redhat.vscode-yaml",
"editor.formatOnSave": true
},
"[yml]": {
"editor.autoIndent": "advanced",
"editor.tabSize": 2,
"editor.defaultFormatter": "redhat.vscode-yaml",
"editor.formatOnSave": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.tabSize": 4,
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"upsert"
],
"files.eol": "\n",
"yaml.customTags": [
"!And",
"!And sequence",
"!If",
"!If sequence",
"!Not",
"!Not sequence",
"!Equals",
"!Equals sequence",
"!Or",
"!Or sequence",
"!FindInMap",
"!FindInMap sequence",
"!Base64",
"!Join",
"!Join sequence",
"!Cidr",
"!Ref",
"!Sub",
"!Sub sequence",
"!GetAtt",
"!GetAZs",
"!ImportValue",
"!ImportValue sequence",
"!Select",
"!Select sequence",
"!Split",
"!Split sequence",
"!reference sequence"
]
}
EOF
echo "[Checking] .vscode/settings.json updated"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment