Cheatsheets:
programming:
How-to:
Troubleshooting:
Rants:
Other:
Cheatsheets:
programming:
How-to:
Troubleshooting:
Rants:
Other:
[prod] aws_access_key_id = abcde1234 aws_secret_access_key = abcde1234 [stage] aws_access_key_id = abcde1234 aws_secret_access_key = abcde1234
SOME_VARIABLE='something' SOME_OTHER_VARIABLE='something-else' declare -A SOME_TOKEN SOME_TOKEN=( [dev]='my-development-token' [stage]='my-staging-token' [prod]='my-production-token' ) function terra-projectname { ENV="$1" ACTION="$2" shift 2 # Shift arguments to access additional parameters # Check if ENV and ACTION are provided if [ -z "$ENV" ] || [ -z "$ACTION" ]; then echo "Usage: $0 <env> <action> [additional terraform arguments]" echo "Example: $0 dev plan -out=tfplan" return 1 fi # Allowed environments VALID_ENVS=("global" "dev" "stage" "prod") # Validate ENV if [[ ! " ${VALID_ENVS[@]} " =~ " ${ENV} " ]]; then echo "Error: ENV must be one of 'global', 'dev', 'stage', or 'prod'." return 1 fi # Allowed Terraform actions VALID_ACTIONS=( "apply" "plan" "destroy" "init" "refresh" "validate" "fmt" "state" "import" "taint" "untaint" "graph" "output" "providers" "show" "workspace" "login" "logout" "version" "force-unlock" "debug" ) # Validate ACTION if [[ ! " ${VALID_ACTIONS[@]} " =~ " ${ACTION} " ]]; then echo "Error: ACTION must be a valid Terraform command." return 1 fi # Set common environment variables export ENV="$ENV" export TF_VAR_ENV="$ENV" export AWS_PROFILE="default" export TF_VAR_SOME_VARIABLE="${SOME_VARIABLE}" export TF_VAR_SOME_OTHER_VARIABLE="${SOME_OTHER_VARIABLE}" export SOME_TOKEN="${SOME_TOKEN[$ENV]}" # Alias terraform command local terraform_command=(/usr/bin/terraform "$ACTION") # If 'init', include backend configurations if [ "$ACTION" = "init" ]; then # Append backend-config parameters to the array terraform_command+=("-backend-config=bucket=projectName") terraform_command+=("-backend-config=dynamodb_table=projectName-${ENV}-state") terraform_command+=("-backend-config=region=${MY_REGION}") terraform_command+=("-backend-config=key=terraform-project/projectName-${ENV}.tfstate") terraform_command+=("-backend-config=encrypt=true") fi # Append any additional arguments passed to the script terraform_command+=("$@") # Echo the command for debugging printf "Executing:" for arg in "${terraform_command[@]}"; do printf " '%s'" "$arg" done printf "\n" # Execute the Terraform command "${terraform_command[@]}" }
$ terra-projectname dev init $ terra-projectname dev plan etc..