Skip to main content

Terraform CheatSheet for Developers

Format and Validate Terraform code

CommandDescription
terraform fmtFormat code per HCL canonical standard
terraform validateValidate code for syntax
terraform validate -backend=falseValidate code skip backend

Initialize your Terraform working directory

CommandDescription
terraform initInitialize directory, pull down providers
terraform init -get-plugins=falseInitialize directory, do not download plugins
terraform init -verify-plugins=falseInitialize directory, do not verify plugins for Hashicorp signature

Plan, Deploy and Cleanup Infrastructure

CommandDescription
terraform apply --auto-approveApply changes without being prompted to enter “yes”
terraform destroy --auto-approveDestroy/cleanup deployment without being prompted for “yes”
terraform plan -out plan.outOutput the deployment plan to plan.out
terraform apply plan.outUse the plan.out plan file to deploy infrastructure
terraform plan -destroyOutputs a destroy plan
terraform apply -target=aws_instance.my_ec2Only apply/deploy changes to the targeted resource
terraform apply -var my_region_variable=us-east-1 Pass a variable via command-line while applying a configuration
terraform apply -lock=trueLock the state file so it can’t be modified by any other Terraform apply or modification action
terraform apply refresh=falseDo not reconcile state file with real-world resources
terraform apply --parallelism=5Number of simultaneous resource operations
terraform refreshReconcile the state in Terraform state file with real-world resources
terraform providersGet information about providers used in current configuration

Terraform Workspace

CommandDescription
terraform workspace new mynewworkspaceCreate a new workspace
terraform workspace select defaultChange to the selected workspace
terraform workspace listList out all workspaces

Terraform State Manipulation

CommandDescription
terraform state show aws_instance.my_ec2Show details stored in Terraform state for the resource
terraform state pull > terraform.tfstateDownload and output terraform state to a file
terraform state mv aws_iam_role.my_ssm_role module.custom_moduleMove a resource tracked via state to different module
terraform state replace-provider hashicorp/aws registry.custom.com/awsReplace an existing provider with another
terraform state listList out all the resources tracked via the current state file
terraform state rm aws_instance.myinstaceUnmanage a resource, delete it from Terraform state file

Terraform Import And Outputs

CommandDescription
terraform import aws_instance.new_ec2_instance i-abcd1234Import EC2 instance with id i-abcd1234 into the Terraform resource named “new_ec2_instance” of type “aws_instance”
terraform outputList all outputs as stated in code
terraform output instance_public_ipList out a specific declared
terraform output -jsonList all outputs in JSON format

Terraform Miscelleneous commands

CommandDescription
terraform versiondisplay Terraform binary version, also warns if version is old
terraform get -update=truedownload and update modules in the “root” module.