Terraform
-
Install Terraform: Terraform Installation Guide (opens in a new tab)
-
Configure AWS CLI: AWS CLI Installation Guide (opens in a new tab)
-
Clone the Repository:
git clone https://github.com/yourusername/your-repo.git -
Environment Variables with
TF_VAR_If you set environment variables that start withTF_VAR_, you don't need to pass them in via the-var-fileoption or define them in theaws.tfvarsfile. Terraform will automatically pick up those variables.
For example:
export TF_VAR_region=us-west-1
export TF_VAR_instance_type=t2.microThis allows you to manage configuration via environment variables, offering flexibility in how you handle variable values across different environments or workflows.
- Terraform Commands:
terraform init # Initializes Terraform terraform workspace select <workspace-name> # Switch to an Existing Workspace terraform fmt --recursive ./ # corrects the formatting of the script terraform plan -out=plan.tfplan # Creates a Plan terraform apply plan.tfplan # Applies the Plan terraform destroy # Destroyes the Infrastructure # Extra Commands terraform validate # validates the terraform script terraform fmt -check --recursive ./ # checks the formatting of the terraform script terraform plan -out=plan.tfplan -var-file="aws.tfvars" # Creates a Plan using .tfvars file terraform destroy -var-file="aws.tfvars" # Destroyes the Infrastructure using .tfvars file - Terraform Workspace Commands:
terraform workspace list # List Workspaces terraform workspace show # Show Current Workspace: terraform workspace new <workspace-name> # Create a New Workspace: terraform workspace select <workspace-name> # Switch to an Existing Workspace
In Terraform, a workspace is an environment within a single Terraform configuration that allows you to manage multiple sets of infrastructure. Each workspace has its own state, meaning you can use the same configuration code to manage different resources or environments (like development, staging, and production) without having them interfere with each other.
Key Features:
- Isolated State: Each workspace has its own state file, which means changes made in one workspace won't affect another.
- Same Code, Different Resources: You can use the same Terraform configuration but deploy different sets of resources based on the active workspace.
- Default Workspace: When you initialize a new Terraform project, you start in the
defaultworkspace. - Common Use Cases: Managing different environments (e.g., dev, stage, prod), handling multiple regions, or separating infrastructure by teams or departments.
This feature is useful for avoiding the need to duplicate configuration files for different environments, streamlining infrastructure management.
Notes
-
echo "workspaceName=${WORKSPACE_NAME,,}" >> $GITHUB_OUTPUTis used in a GitHub Actions workflow to create a lowercase version of the WORKSPACE_NAME environment variable and store it in the special GITHUB_OUTPUT file -
terraform fmt -check --recursive ./checks all the files related to terraform in the present repository