Learn All Terraform Commands: A Complete Guide

Unnati GuptaUnnati Gupta
4 min read

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 💥🙌**!!**

10
Subscribe to my newsletter

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

Written by

Unnati Gupta
Unnati Gupta

👨‍💻 DevOps Engineer at 6D Technology Passionate about bridging the gap between development and operations, I'm a dedicated DevOps Engineer at 6D Technology. With a strong belief in the power of automation, continuous integration, and continuous delivery, I thrive in optimizing software development pipelines for efficiency and reliability. 🚀 Exploring the DevOps Universe In my articles, I delve into the fascinating world of DevOps, where I share insights, best practices, and real-world experiences. From containerization and orchestration to CI/CD pipelines and infrastructure as code, I'm here to demystify the complex and empower fellow developers and ops enthusiasts. 📝 Blogging for Knowledge Sharing As a tech enthusiast and a lifelong learner, I'm committed to sharing knowledge. My articles aim to simplify complex concepts and provide practical tips that help teams and individuals streamline their software delivery processes. 🌐 Connect with Me Let's connect and explore the ever-evolving landscape of DevOps together. Feel free to reach out, comment, or share your thoughts on my articles. Together, we can foster a culture of collaboration and innovation in the DevOps community. 🔗 Social Links LinkedIn: https://www.linkedin.com/in/unnati-gupta-%F0%9F%87%AE%F0%9F%87%B3-a62563183/ GitHub: https://github.com/DevUnnati 📩 Contact Have questions or looking to collaborate? You can reach me at unnatigupta527@gmail.com Happy Learning!!