Terraform

Terraform Cheatsheet #

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/OptionExampleDescription
terraform initterraform initInitialize a Terraform configuration directory
terraform planterraform planGenerate and show an execution plan
terraform applyterraform applyBuild or change infrastructure
terraform destroyterraform destroyDestroy Terraform-managed infrastructure

Working with Configuration Files #

Command/OptionExampleDescription
terraform fmtterraform fmtReformat your configuration in the standard style
terraform validateterraform validateCheck whether the configuration is valid
terraform showterraform showShow the current state or a saved plan

Managing State #

Command/OptionExampleDescription
terraform state listterraform state listList resources in the state file
terraform state showterraform state show <resource>Show the attributes of a single resource in the state file
terraform state rmterraform state rm <resource>Remove a resource from the state file
terraform state mvterraform state mv <source> <destination>Move an item in the state file

Provisioning and Outputs #

Command/OptionExampleDescription
terraform outputterraform outputRead an output from a state file
terraform output -jsonterraform output -jsonShow output in JSON format
terraform taintterraform taint <resource>Manually mark a resource for recreation
terraform untaintterraform untaint <resource>Manually unmark a resource as tainted

Resource Targeting #

Command/OptionExampleDescription
terraform apply -targetterraform apply -target=aws_instance.my_instanceApply changes to a specific resource
terraform plan -targetterraform plan -target=aws_instance.my_instanceGenerate a plan for a specific resource

Workspaces #

Command/OptionExampleDescription
terraform workspace listterraform workspace listList available workspaces
terraform workspace newterraform workspace new <name>Create a new workspace
terraform workspace selectterraform workspace select <name>Switch to another workspace
terraform workspace deleteterraform workspace delete <name>Delete a workspace

Modules #

Command/OptionExampleDescription
terraform getterraform getDownload and update modules mentioned in the configuration
terraform module listterraform module listList all modules in the configuration
terraform module showterraform module showShow 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.

Terraform

Explore our comprehensive cheatsheets to enhance your knowledge and efficiency. Each cheatsheet provides detailed command options, examples, and descriptions to help you master various tools and technologies.