Terraform Cheat Cheet
Terraform is a popular infrastructure as code (IaC) tool used for building, changing, and versioning infrastructure efficiently. Here's a cheat sheet
Initializing Terraform
Initialize a Terraform working directory:
terraform init
This command initializes a working directory containing Terraform configuration files.
Building Infrastructure
Create or update infrastructure:
terraform apply
This command creates or updates the infrastructure according to the configuration files.
Inspecting State
Show the current state:
terraform show
Displays the current state of the infrastructure.
Output the current state in a more human-readable format:
terraform output
Shows the values of outputs from the current state.
Planning Changes
Preview changes before applying:
terraform plan
This command shows the changes that will be made to the infrastructure.
Destroying Infrastructure
Destroy the infrastructure:
terraform destroy
Destroys the Terraform-managed infrastructure.
Managing Workspaces
List available workspaces:
terraform workspace list
Lists all available workspaces.
Create a new workspace:
terraform workspace new <workspace_name>
Creates a new workspace.
Switch to a different workspace:
terraform workspace select <workspace_name>
Switches to the specified workspace.
Miscellaneous
Validate the configuration files:
terraform validate
Checks the syntax and configuration of Terraform files.
Output the execution plan as JSON:
terraform show -json
Displays the execution plan in JSON format.
Force unlock the state:
terraform force-unlock <lock_id>
Manually unlock the Terraform state.