Most used Terraform commands for daily operations

Poonam ParatePoonam Parate
4 min read

Initialization and Setup

  1. terraform init

    • Use 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

  1. terraform validate

    • Use Case: Validates the configuration files in the directory, ensuring that the syntax is correct and all required variables are defined.

    • Example:

        terraform validate
      
  2. terraform plan

    • Use 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

  1. terraform apply

    • Use 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

  1. terraform destroy

    • Use 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

  1. terraform state list

    • Use Case: Lists all resources in the Terraform state. This is useful for checking which resources are being managed by Terraform.

    • Example:

        terraform state list
      
  2. terraform state show

    • Use Case: Shows detailed information about a specific resource in the Terraform state.

    • Example:

        terraform state show aws_instance.example
      
  3. terraform state rm

    • Use 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
      
  4. terraform state mv

    • Use 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

  1. terraform import

    • Use 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

  1. terraform workspace new

    • Use 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
      
  2. terraform workspace select

    • Use Case: Switches to an existing workspace.

    • Example:

        terraform workspace select dev
      
  3. terraform workspace list

    • Use Case: Lists all the workspaces in the current configuration.

    • Example:

        terraform workspace list
      

Taint and Untaint Resources

  1. terraform taint

    • Use 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
      
  2. terraform untaint

    • Use 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

  1. terraform fmt

    • Use Case: Formats the configuration files to the canonical format and style. This helps maintain consistency and readability in the codebase.

    • Example:

        terraform fmt
      
  2. terraform show

    • Use 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

  1. terraform output

    • Use 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
      
  2. terraform refresh

    • Use 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

  1. terraform version

    • Use 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.

  1. terraform get

    Use Cases The terraform get command 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:

    1. Initialize the Working Directory: Run terraform init to initialize the working directory, which will set up the backend and provider plugins.

       terraform init
      
    2. Download and Update Modules: Run terraform get to download the module specified in the configuration.

       terraform get
      
    3. Update 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
      
0
Subscribe to my newsletter

Read articles from Poonam Parate directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Poonam Parate
Poonam Parate

DevOps Engineer