Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision data center infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform manages external resources (such as public cloud infrastructure, private cloud infrastructure, network appliances, software as a service, and platform as a service) with a provider, and has over 100 providers for various services.
Basic Commands
#
Command/Option | Example | Description |
---|
terraform init | terraform init | Initialize a Terraform configuration directory |
terraform plan | terraform plan | Generate and show an execution plan |
terraform apply | terraform apply | Build or change infrastructure |
terraform destroy | terraform destroy | Destroy Terraform-managed infrastructure |
Working with Configuration Files
#
Command/Option | Example | Description |
---|
terraform fmt | terraform fmt | Reformat your configuration in the standard style |
terraform validate | terraform validate | Check whether the configuration is valid |
terraform show | terraform show | Show the current state or a saved plan |
Managing State
#
Command/Option | Example | Description |
---|
terraform state list | terraform state list | List resources in the state file |
terraform state show | terraform state show <resource> | Show the attributes of a single resource in the state file |
terraform state rm | terraform state rm <resource> | Remove a resource from the state file |
terraform state mv | terraform state mv <source> <destination> | Move an item in the state file |
Provisioning and Outputs
#
Command/Option | Example | Description |
---|
terraform output | terraform output | Read an output from a state file |
terraform output -json | terraform output -json | Show output in JSON format |
terraform taint | terraform taint <resource> | Manually mark a resource for recreation |
terraform untaint | terraform untaint <resource> | Manually unmark a resource as tainted |
Resource Targeting
#
Command/Option | Example | Description |
---|
terraform apply -target | terraform apply -target=aws_instance.my_instance | Apply changes to a specific resource |
terraform plan -target | terraform plan -target=aws_instance.my_instance | Generate a plan for a specific resource |
Workspaces
#
Command/Option | Example | Description |
---|
terraform workspace list | terraform workspace list | List available workspaces |
terraform workspace new | terraform workspace new <name> | Create a new workspace |
terraform workspace select | terraform workspace select <name> | Switch to another workspace |
terraform workspace delete | terraform workspace delete <name> | Delete a workspace |
Modules
#
Command/Option | Example | Description |
---|
terraform get | terraform get | Download and update modules mentioned in the configuration |
terraform module list | terraform module list | List all modules in the configuration |
terraform module show | terraform module show | Show information about a specific module |
This cheatsheet covers the most commonly used Terraform commands and options, helping you to manage configurations, state, resources, workspaces, and modules effectively.