Most used Terraform commands for daily operations

DevOps Engineer
Initialization and Setup
terraform initUse Case: Initializes a working directory containing Terraform configuration files. This command downloads the necessary provider plugins and sets up the backend configuration for state management.
Example:
terraform init
Planning and Validation
terraform validateUse Case: Validates the configuration files in the directory, ensuring that the syntax is correct and all required variables are defined.
Example:
terraform validate
terraform planUse Case: Creates an execution plan, showing what actions Terraform will take to reach the desired state defined in the configuration files. It’s a dry run, so no changes are actually applied.
Example:
terraform plan
Applying Changes
terraform applyUse Case: Applies the changes required to reach the desired state of the configuration. It creates, updates, or deletes resources as necessary.
Example:
terraform apply
Destroying Infrastructure
terraform destroyUse Case: Destroys the infrastructure defined in the configuration files. It’s useful for tearing down environments that are no longer needed.
Example:
terraform destroy
State Management
terraform state listUse Case: Lists all resources in the Terraform state. This is useful for checking which resources are being managed by Terraform.
Example:
terraform state list
terraform state showUse Case: Shows detailed information about a specific resource in the Terraform state.
Example:
terraform state show aws_instance.example
terraform state rmUse Case: Removes a specific resource from the Terraform state. This command does not destroy the resource itself, but removes it from Terraform’s management.
Example:
terraform state rm aws_instance.example
terraform state mvUse Case: Moves a resource from one location in the state file to another. This can be useful for renaming resources or moving them into a module.
Example:
terraform state mv aws_instance.example module.new_example.aws_instance.example
Importing Existing Resources
terraform importUse Case: Imports existing infrastructure resources into Terraform’s state so that they can be managed by Terraform.
Example:
terraform import aws_instance.example i-1234567890abcdef0
Workspace Management
terraform workspace newUse Case: Creates a new workspace. Workspaces allow you to manage multiple environments (e.g., dev, staging, prod) within a single Terraform configuration.
Example:
terraform workspace new dev
terraform workspace selectUse Case: Switches to an existing workspace.
Example:
terraform workspace select dev
terraform workspace listUse Case: Lists all the workspaces in the current configuration.
Example:
terraform workspace list
Taint and Untaint Resources
terraform taintUse Case: Marks a resource for recreation during the next
terraform apply. This is useful if a resource needs to be replaced without changing the configuration.Example:
terraform taint aws_instance.example
terraform untaintUse Case: Removes the taint from a resource, preventing it from being recreated during the next
terraform apply.Example:
terraform untaint aws_instance.example
Formatting and Documentation
terraform fmtUse Case: Formats the configuration files to the canonical format and style. This helps maintain consistency and readability in the codebase.
Example:
terraform fmt
terraform showUse Case: Displays the current state or a saved plan. It’s useful for reviewing the current infrastructure state or a proposed set of changes.
Example:
terraform show
Automation and Scripting
terraform outputUse Case: Extracts the output values defined in the configuration files. This is often used in scripts to pass information between Terraform and other tools.
Example:
terraform output instance_ip
terraform refreshUse Case: Updates the state file with the actual resource values. This can be useful if the infrastructure changes outside of Terraform.
Example:
terraform refresh
Versioning
terraform versionUse Case: Displays the current version of Terraform. Useful for ensuring compatibility and debugging.
Example:
terraform version
By understanding these commands and their use cases, you can effectively manage infrastructure using Terraform and automate the provisioning and maintenance of resources.
terraform getUse Cases The
terraform getcommand is used in Terraform to download and update modules referenced in the configuration files. It ensures that all modules specified in the configuration are present and up-to-date, making it a crucial command when working with reusable Terraform modules.Steps to use terraform get:
Initialize the Working Directory: Run
terraform initto initialize the working directory, which will set up the backend and provider plugins.terraform initDownload and Update Modules: Run
terraform getto download the module specified in the configuration.terraform getUpdate Modules if Necessary: If you want to ensure that the latest version of the module is being used, run
terraform get -update.terraform get -update

