Terraform commands Cheatsheet
Acknowledgements for sources for content on this site
https://cloudinfrastructureservices.co.uk/terraform-commands-cheat-sheet/
Initialize a new or existing Terraform working directory
- created initial files
- loads any remote state
- downloads modules and providers
- idempotent: This command is always safe to run multiple times.
terraform init # #initialize directory, pull down providers
terraform init -backend-config=dev.backend.tfvars # To use a file to describe backend configuration
terraform init -upgrade # Installs the latest modules and providers based on version configuration
# some of frequently used options
-input=true #It is used for requesting input if it is required or will show an error if the information was needed.
-lock=false #It unlocks the state when any state operation is going on.
-lock-timeout= n #timeout number, If another process has kept the lock, then it will generate an error. The default is zero seconds.
-no-color #It is used to boost the modules.
Generate execution plan
Showing what actions terraform would take to apply the current configuration.
- This command will not actually perform the planned actions
- idempotent: This command is always safe to run multiple times.
terraform [global options] plan [options]
# For example
terraform plan -out=tfplan . #generate terraform plan file named as `tfplan` in current directory.
terraform plan -var-file=dev.terraform.tfvars #
# some of frequently used options
-refresh=false
-target=path_to_module
-var 'NAME=VALUE'
-no-color
Apply modifications:
terraform apply -var-file=dev.terraform.tfvars -auto-approve
Apply modifications on a specific resource:
terraform apply -target="aws_eks_node_group.eks-worker[\"node-group\"]" -var-file=dev.terraform.tfvars -auto-approve
Delete Terraform resources:
terraform destroy -force [options]
Exposing the content of a file as a viarable
locals {
config = yamldecode(file("./config.yaml"))
}
Terraform Cloud
Generate user token and put it in the ~/.terraformrc
file like that:
credentials "app.terraform.io" {
token = "$TF_CLOUD_TOKEN"
}
To debug
Set TF_LOG env variable:
export TF_LOG=TRACE
Test the output of an internal Terraform function:
# Drops you in an interactive shell
terraform console
Bonus
terraform fmt #Format all your file following the same pattern:
terraform validate #validate code for syntax
terraform validate -backend=false #validate code skip backend validation
Install autocompletion:
terraform -install-autocomplete
If you use TF in a CI system, set this env variable to adjusts TFs output to avoid suggesting specific commands to run next:
TF_IN_AUTOMATION: "true"
Last modified July 6, 2023: selfmade-engineer init commit (2b4e47b)