Learn All Terraform Commands: A Complete Guide

Learn All Terraform Commands: A Complete Guide

In the previous blog, I discussed How to set up Terraform on a Linux server. You can check it here: Terraform installation.

Terraform, a powerful Infrastructure as Code (IaC) tool, allows engineers to manage cloud infrastructure through configuration files. In this blog, we’ll walk through some of the most fundamental Terraform commands every DevOps engineer should know.

a) Terraform init :

The first command is required to run after Terraform configuration and before starting any cluster deployment. This sets up the backend where Terraform keeps its state and installs any necessary provider plugins.

terraform init
  • What It Does: Downloads provider plugins (e.g., AWS, Azure, GCP) and sets up the local environment for managing infrastructure.

  • When to Use: Run this command every time you set up a new configuration or modify an existing configuration's backend.

b) Terraform Plan :

This command lets you preview the changes that Terraform will make before applying them. It’s crucial to understand what resources will be created, changed, or destroyed.

terraform plan
  • What It Does: Compares the current state of your infrastructure with your configuration and shows a list of actions Terraform will take (create, modify, or delete resources).

  • When to Use: Use it to ensure changes are as expected before running terraform apply.

c) Terraform Apply :

This command is used to execute the plan and create or modify resources.

terraform apply
  • What It Does: Applies the changes defined in the configuration and updates the cloud infrastructure accordingly.

  • When to Use: After reviewing the plan output and confirming that the planned changes are correct.

d) Terraform destroy:

This command is used to delete the entire configuration (infrastructure) that is configured in the terraform configuration file.

terraform destroy
  • What It Does: Removes all the resources managed by the current Terraform state.

  • When to Use: Use it when you want to remove infrastructure, such as when you’re done testing or need to decommission a project.

e) Terraform show

This command displays the state of your infrastructure as defined in the last run or after a terraform apply.

terraform show
  • What It Does: Outputs a human-readable view of the Terraform state file, showing the resources and their configurations.

  • When to Use: To inspect the current state of your infrastructure or the details of specific resources.

f) Terraform Output :

Once Terraform has provisioned infrastructure, this command shows the values of any output variables defined in your configuration.

terraform output
  • What It Does: Displays output values, such as instance IP addresses or resource IDs, that were defined in the configuration.

  • When to Use: Use it to access key information from the state file after deployment, for tasks like integrating infrastructure with other systems.

Note: command execution is correct, only I defined any variable that will give value so it’s giving a warning.

g) Terraform fmt:

This command formats your Terraform configuration files to ensure consistency in style and readability.

terraform fmt
  • What It Does: Automatically formats your .tf files, correcting any alignment or spacing issues.

  • When to Use: Run it regularly to maintain clean and readable configuration files.

h) Terraform validate:

Use this command to check if your Terraform configuration files are syntactically correct and valid.

terraform validate
  • What It Does: Validates that your configuration files are properly structured and that the code is error-free.

  • When to Use: Run it before applying any changes to catch syntax errors and ensure the configuration is valid.

i) Terraform state:

This command helps manage and inspect the Terraform state file, which tracks resources and their current states.

terraform state <subcommands>

Subcommands:
    list                List resources in the state
    mv                  Move an item in the state
    pull                Pull current state and output to stdout
    push                Update remote state from a local state file
    replace-provider    Replace provider in the state
    rm                  Remove instances from the state
    show                Show a resource in the state
  • What It Does: Provides various options for interacting with the state, such as listing resources, moving them, or removing them from state management.

  • When to Use: Use it when you need to manually adjust or inspect the state, especially during troubleshooting or migrations.

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning 💥🙌**!!**