Terraform Commands:
Basic Terraform Commands
terraform init– Initializes a new or existing Terraform configuration.terraform plan– Generates and shows an execution plan.terraform apply– Builds or changes infrastructure.terraform destroy– Destroys Terraform-managed infrastructure.terraform fmt– Formats Terraform configuration files to a canonical style.terraform validate– Validates the configuration files.terraform output– Extracts and displays output values from the Terraform state.terraform show– Provides human-readable output from a state or plan file.terraform version– Displays the current Terraform version.terraform providers– Lists Terraform providers required by the configuration.
Terraform State Management
terraform state list– Lists all resources in the Terraform state.terraform state show <resource>– Shows the attributes of a resource in the Terraform state.terraform state pull– Downloads and outputs the Terraform state in JSON format.terraform state push– Uploads a local state file to a remote backend.terraform state rm <resource>– Removes a resource from the Terraform state.terraform state mv <source> <destination>– Moves resources within the state.terraform state replace-provider <old-provider> <new-provider>– Replaces a provider in the state.terraform refresh– Updates local state with real-world infrastructure.terraform state pull > terraform.tfstate– Downloads and saves the state locally.
Terraform Workspaces
terraform workspace list– Lists all Terraform workspaces.terraform workspace new <name>– Creates a new Terraform workspace.terraform workspace select <name>– Switches to a different workspace.terraform workspace delete <name>– Deletes a Terraform workspace.terraform workspace show– Displays the current workspace.
Terraform Import
terraform import <resource_type>.<resource_name> <id>– Imports existing infrastructure into Terraform.terraform import module.<module_name>.<resource_type>.<resource_name> <id>– Imports infrastructure into a specific module.
Terraform Graph and Visualization
terraform graph– Outputs a visual representation of the dependency graph.terraform graph | dot -Tpng > graph.png– Converts the graph into a PNG file using Graphviz.
Terraform Plan and Apply Options
terraform apply -auto-approve– Applies changes without confirmation.terraform apply -var="key=value"– Applies changes with overridden variables.terraform apply -target=<resource>– Applies only the targeted resource.terraform apply <plan_file>– Applies a saved plan file.terraform plan -out=<plan_file>– Saves the execution plan to a file.terraform plan -var="key=value"– Creates a plan with overridden variables.terraform plan -target=<resource>– Generates a plan for the targeted resource.terraform plan -destroy– Shows a plan to destroy all resources.terraform plan -refresh=false– Skips refreshing the state when generating a plan.terraform apply -refresh-only– Updates the state without changing any resources.
Variable Management
terraform apply -var-file=<file.tfvars>– Uses variables from a file.terraform plan -var-file=<file.tfvars>– Creates a plan using variables from a file.terraform apply -var="key=value"– Applies with a single variable override.terraform plan -var="key=value"– Plans with a single variable override.terraform apply -lock=false– Applies changes without locking the state.terraform plan -input=false– Runs the plan without interactive input for variables.
Resource Taint and Untaint
terraform taint <resource>– Marks a resource for recreation in the next apply.terraform untaint <resource>– Removes the tainted mark from a resource.
Remote State Management
terraform remote config– Configures remote state storage.terraform backend -config=backend.tf– Specifies the backend configuration.terraform state push– Uploads a local state file to the configured backend.terraform state pull– Downloads the current state from the remote backend.
Provider Management
terraform providers schema– Displays the schema of providers used in the configuration.terraform providers lock– Generates a lock file for providers.terraform providers mirror <directory>– Copies providers to a specified directory.terraform providers install– Installs the required providers locally.terraform init -upgrade– Upgrades provider plugins to the latest versions.
Locking and Unlocking
terraform force-unlock <lock-id>– Manually unlocks the state for recovery.terraform apply -lock-timeout=5m– Specifies a timeout for acquiring the state lock.
Terraform State Manipulation
terraform state push <file>– Pushes the given state file to the remote backend.terraform state pull > file.tfstate– Pulls the current state to a local file.terraform state replace-provider– Replaces the provider for resources in the state.terraform state mv <old> <new>– Moves items within the state.terraform state rm <resource>– Removes a specific resource from the state.
Debugging and Logging
TF_LOG=DEBUG terraform apply– Enables debug-level logging.TF_LOG=INFO terraform plan– Enables info-level logging.TF_LOG_PATH=<file> terraform apply– Redirects logging output to a file.terraform validate -json– Validates the configuration and outputs in JSON format.terraform console– Opens an interactive console for evaluating expressions.terraform providers schema -json– Outputs the provider schema in JSON format.
Experimental Commands
terraform apply -replace=<resource>– Forces Terraform to replace a specific resource.terraform plan -refresh=false– Skips refreshing the state before planning.terraform destroy -target=<resource>– Destroys a specific resource.
Working with Modules
terraform get– Downloads and installs modules for the configuration.terraform get -update– Updates modules to the latest versions.terraform init -get=true– Re-fetches the required modules.terraform init -get-plugins– Downloads necessary provider plugins.
Backups and Rollbacks
terraform state snapshot– Creates a backup of the current state.terraform state restore <backup-file>– Restores the state from a backup file.terraform apply -backup=<backup-file>– Specifies a backup file before applying changes.
Automation and Scripting
terraform apply -auto-approve– Runsapplywithout confirmation.terraform plan -detailed-exitcode– Returns a detailed exit code for use in CI/CD.terraform apply -parallelism=<N>– Limits the number of parallel resource operations.
Workspace Commands
terraform workspace list– Lists available workspaces.terraform workspace new <name>– Creates a new workspace.terraform workspace select <name>– Switches to a specific workspace.terraform workspace delete <name>– Deletes a workspace.terraform workspace show– Displays the current workspace.
Remote Backend and Collaboration
terraform login– Authenticates to Terraform Cloud/Enterprise.terraform logout– Logs out of Terraform Cloud/Enterprise.terraform init -backend-config=<config>– Initializes the backend with a specific configuration.
Resource Manipulation
terraform apply -replace=<resource>– Forces a specific resource to be replaced.terraform taint <resource>– Marks a resource as tainted for recreation.terraform untaint <resource>– Removes the tainted status from a resource.
Miscellaneous
terraform init -force-copy– Forces copying of state data on reinitialization.terraform init -reconfigure– Reconfigures backend settings on initialization.terraform plan -compact-warnings– Hides warnings during the plan.terraform state show <resource>– Shows the state of a specific resource.terraform refresh– Updates the state to match real-world infrastructure.terraform login– Authenticates to Terraform Cloud/Enterprise.terraform fmt -recursive– Recursively formats all.tffiles in a directory.terraform force-unlock <lock-id>– Manually unlocks the state for recovery.
About the author
👋 Welcome to my Hashnode blog! I'm a tech enthusiast and cloud advocate with expertise in IT, backup solutions, and cloud technologies. Currently, I’m diving deep into Python and exploring its applications in automation and data management. I share insights, tutorials, and tips on all things tech, aiming to help fellow developers and enthusiasts. Join me on this journey of learning and innovation!
Comments
Join the discussionNo comments yet. Be the first to comment.