As a developer or system administrator, you may have heard of Terraform, a popular infrastructure as code (IaC) tool that allows you to manage your cloud infrastructure using code. Terraform is a powerful tool that can help you automate your infrastructure deployment and management, but it can also be complex and overwhelming to use at times.
That’s where a Terraform cheat sheet comes in handy. A Terraform cheat sheet is a quick reference guide that provides you with the most commonly used Terraform commands, syntax, and best practices. It can help you save time and avoid mistakes by providing you with a quick and easy way to look up the commands and syntax you need.
This cheat sheet provides a quick reference for Terraform commands, syntax, and best practices.
Table of Contents
Installation
Command Description brew install terraform
Install Terraform using Homebrew on macOS choco install terraform
Install Terraform using Chocolatey on Windows terraform --version
Check the installed version of Terraform
Configuration
Command Description terraform init
Initialize a new Terraform project terraform plan
Preview the changes to be made by Terraform terraform apply
Apply the changes to the infrastructure terraform destroy
Destroy the infrastructure created by Terraform terraform validate
Validate the syntax of the Terraform configuration terraform fmt
Format the Terraform configuration for consistency terraform state
Inspect and modify the Terraform state file
Commands
Command Description terraform workspace
Manage multiple workspaces for a Terraform project terraform import
Import existing infrastructure into Terraform terraform output
Display the output values of a Terraform configuration terraform graph
Generate a visual representation of the Terraform dependency graph terraform taint
Mark a resource as tainted, forcing it to be recreated terraform untaint
Remove the tainted state from a resource terraform refresh
Refresh the state of the Terraform-managed infrastructure
Resources
Command Description resource
Define a new resource in the Terraform configuration data
Define a data source to retrieve information from an external system locals
Define local variables within the Terraform configuration output
Define output values to be displayed after applying the Terraform configuration
Variables
Command Description variable
Define a variable to be used in the Terraform configuration var
Reference a variable in the Terraform configuration terraform.tfvars
Define default values for variables in a file named terraform.tfvars
-var-file
Specify a file containing variable values with the -var-file
flag
Modules
Command Description module
Define a module to encapsulate a set of resources source
Specify the source of a module, which can be a local directory or a remote repository terraform get
Download and install modules from the specified sources terraform init -upgrade
Upgrade the modules used in a Terraform project
Providers
Command Description provider
Define a provider to interact with an external system terraform init
Download and install the required providers for a Terraform project terraform providers
List the providers used in a Terraform project terraform state replace-provider
Replace the provider used by a resource in the Terraform state file
Best Practices
Use version control to manage Terraform configurations and state files.
Use modules to encapsulate reusable sets of resources.
Use variables to parameterize Terraform configurations.
Use data sources to retrieve information from external systems.
Use output values to display important information after applying a Terraform configuration.
Use the terraform plan
command to preview changes before applying them.
Use the terraform destroy
command to clean up resources when they are no longer needed.
Use the terraform fmt
command to ensure consistent formatting of Terraform configurations.
Use the terraform validate
command to check the syntax of Terraform configurations.
Use the terraform state
command to inspect and modify the Terraform state file.
Reference:
https://terraform-docs.io/