User Tools

Site Tools


cheatsheet:terraform

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cheatsheet:terraform [2023/02/14 09:36] – created kamaradskicheatsheet: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 +<code bash>
-alias terra-prod="AWS_PROFILE=prod terraform" +
-alias terra-stage="AWS_PROFILE=stage terraform" +
- +
-# 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
 +</code>
  
-# 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='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[@]}" 
 +
 +</code> 
 + 
 +==== on the cli from inside your terraform folder ==== 
 + 
 +<code bash> 
 +$ terra-projectname dev init 
 +$ terra-projectname dev plan 
 +etc..
 </code> </code>
cheatsheet/terraform.1676367400.txt.gz · Last modified: 2023/02/14 09:36 by kamaradski