This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| cheatsheet:terraform [2023/02/14 09:36] – created kamaradski | cheatsheet:terraform [2024/10/21 13:53] (current) – kamaradski | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== run any terraform command with a specific .aws profile name ===== | ===== run any terraform command with a specific .aws profile name ===== | ||
| - | <code bash> | + | ==== in your awscli config ==== |
| - | # in your bash profie | + | < |
| - | alias terra-prod=" | + | |
| - | alias terra-stage=" | + | |
| - | + | ||
| - | # in your awscli config | + | |
| [prod] | [prod] | ||
| aws_access_key_id = abcde1234 | aws_access_key_id = abcde1234 | ||
| Line 16: | Line 12: | ||
| aws_access_key_id = abcde1234 | aws_access_key_id = abcde1234 | ||
| aws_secret_access_key = abcde1234 | aws_secret_access_key = abcde1234 | ||
| + | </ | ||
| - | # on the cli from inside your terraform folder | + | ==== in your bash profile ==== |
| - | $ terra-prod init | + | |
| - | $ terra-prod plan | + | <code bash> |
| - | $ terra-prod apply | + | SOME_VARIABLE=' |
| + | SOME_OTHER_VARIABLE=' | ||
| + | |||
| + | declare -A SOME_TOKEN | ||
| + | |||
| + | |||
| + | SOME_TOKEN=( | ||
| + | [dev]=' | ||
| + | [stage]=' | ||
| + | [prod]=' | ||
| + | ) | ||
| + | |||
| + | function terra-projectname { | ||
| + | |||
| + | ENV=" | ||
| + | ACTION=" | ||
| + | shift 2 | ||
| + | |||
| + | # Check if ENV and ACTION are provided | ||
| + | if [ -z " | ||
| + | echo " | ||
| + | echo " | ||
| + | return 1 | ||
| + | fi | ||
| + | |||
| + | # Allowed environments | ||
| + | VALID_ENVS=(" | ||
| + | |||
| + | # Validate ENV | ||
| + | if [[ ! " ${VALID_ENVS[@]} " =~ " ${ENV} " ]]; then | ||
| + | echo " | ||
| + | return 1 | ||
| + | fi | ||
| + | |||
| + | # Allowed Terraform actions | ||
| + | VALID_ACTIONS=( | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ) | ||
| + | |||
| + | # Validate ACTION | ||
| + | if [[ ! " ${VALID_ACTIONS[@]} " =~ " ${ACTION} " ]]; then | ||
| + | echo " | ||
| + | return 1 | ||
| + | fi | ||
| + | |||
| + | # Set common environment variables | ||
| + | export ENV=" | ||
| + | export TF_VAR_ENV=" | ||
| + | export AWS_PROFILE=" | ||
| + | export TF_VAR_SOME_VARIABLE=" | ||
| + | export TF_VAR_SOME_OTHER_VARIABLE=" | ||
| + | export SOME_TOKEN=" | ||
| + | |||
| + | |||
| + | # Alias terraform command | ||
| + | local terraform_command=(/ | ||
| + | |||
| + | # If ' | ||
| + | if [ " | ||
| + | # Append backend-config parameters to the array | ||
| + | terraform_command+=(" | ||
| + | terraform_command+=(" | ||
| + | terraform_command+=(" | ||
| + | terraform_command+=(" | ||
| + | terraform_command+=(" | ||
| + | fi | ||
| + | |||
| + | # Append any additional arguments passed to the script | ||
| + | terraform_command+=(" | ||
| + | |||
| + | # Echo the command for debugging | ||
| + | printf " | ||
| + | for arg in " | ||
| + | printf " ' | ||
| + | done | ||
| + | printf " | ||
| + | |||
| + | # Execute the Terraform command | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== on the cli from inside your terraform folder | ||
| + | |||
| + | <code bash> | ||
| + | $ terra-projectname dev init | ||
| + | $ terra-projectname dev plan | ||
| + | etc.. | ||
| </ | </ | ||