Basic Terraform Commands Cheat Sheet: Terraform CLI Commands 

terraform commands

Terraform is an open-source infrastructure as code (IaC) tool that allows you to define and provision data center infrastructure using a declarative configuration language. Developed by HashiCorp, Terraform provides a robust and efficient way to manage cloud resources. 

This blog post will explore essential Terraform commands, functionalities, and best practices for using them effectively. Thus, keep reading the blog till the end to understand better.

What is Terraform?

Terraform allows you to manage your infrastructure through configuration files written in HashiCorp Configuration Language (HCL) or JSON. Collaborate with teams and automate infrastructure management using version control with these configuration files to track changes.

One of the key features of Terraform is its ability to work with multiple providers, such as AWS, Azure, Google Cloud, and more. This flexibility makes it a popular choice among DevOps professionals for managing cloud resources efficiently.

How to set up Terraform?

To get started with Terraform, follow the given steps-

  • Install Terraform: Download the latest version of Terraform from the official website and follow the installation instructions for your operating system.
  • Create a Configuration File: Create a directory for your project, and inside it, create a .tf file where you will define your infrastructure resources.

For example, if you are working with AWS, your configuration might look like this-

provider “aws” {

  region = “us-east-1”

}

resource “aws_instance” “example” {

  ami = “ami-12345678”

  instance_type = “t2.micro”

}

NOTE: Join the DevOps Master’s Program by Network Kings to begin today!

What are the core Terraform commands?

The core Terraform commands you need to know are as follows-

  • terraform init

The terraform init command initializes a Terraform configuration directory. This command performs several tasks, such as

  • Downloads the necessary provider plugins specified in your configuration
  • Sets up the backend for storing the state file
  • Prepares the working directory for other commands

This command runs before any other commands and ensures your environment is ready for deployment.

Its Usage:

terraform init

  • terraform plan

The terraform plan command creates an execution plan, showing how Terraform can change the current infrastructure to match the desired state defined in your configuration files. This command allows you to review changes before applying them, which helps prevent accidental modifications.

Its Usage:

terraform plan

The output will provide a detailed list of actions that will occur, including additions, modifications, and deletions of resources.

  • terraform apply

The terraform apply command executes the actions proposed in the execution plan, creating or modifying infrastructure resources as defined in your configuration files. It reviews the output of the plan command before executing this command.

Its Usage:

terraform apply

You can also use-

terraform apply -auto-approve

to skip the approval prompt and automatically apply changes.

  • terraform destroy

The terraform destroy command deletes all the resources defined in your configuration files. This command cleans up resources no longer needed or for tearing down environments after testing.

Its Usage:

terraform destroy

Make sure to review the list of resources destroyed before confirming the action.

  • terraform validate

The terraform validate command checks whether the configuration files are syntactically valid and internally consistent. It does not check if the created resources but ensures that your code is free from syntax errors.

Its Usage:

terraform validate

  • terraform fmt

The terraform fmt command formats your configuration files to canonical format and style. This command helps maintain consistency across your codebase, making it easier to read and understand for anyone who may work on it.

Its Usage:

terraform fmt

  • terraform output

The terraform output command retrieves the output values from your state file. These outputs display information about your infrastructure after applying changes. For instance, use this command to reference an output value in another module or script.

Its Usage:

terraform output

You can also specify an output name:

terraform output <output_name>

  • terraform state

The terraform state command advances state management tasks. It allows you to inspect, manipulate, or query the current state file. This command is essential when troubleshooting or modifying the state of your resources.

Its Usage:

terraform state list # List all resources in the state

terraform state show <resource_id> # Show details of a specific resource

NOTE: Join the DevOps Master’s Program by Network Kings to begin today!

What are the advanced Terraform commands?

The advanced Terraform commands are as follows-

  • terraform import

The terraform import command brings existing infrastructure under Terraform management. This command imports resources created outside of Terraform into your state file.

Its Usage:

terraform import <resource_type>.<resource_name> <resource_id>

For example:

terraform import aws_instance.example i-12345678

  • terraform taint

The terraform taint command marks a resource for recreation during the next operation. One requires it if a resource is in an unknown or corrupted state and needs replacement.

Its Usage:

terraform taint <resource_type>.<resource_name>

  • terraform workspace

Terraform workspaces allow you to manage different states for different environments (e.g., development, staging, production) within the same configuration. You can create new workspaces or switch between them easily.

Its Usage:

terraform workspace new <workspace_name>

terraform workspace select <workspace_name>

What are the best practices for using Terraform commands?

The best practices for using Terraform commands are as follows-

  • Use Version Control: Store your Terraform configuration files in a version control system like Git. This practice helps track changes and collaborate with others.
  • Organize Your Code: Structure your code into modules for better reusability and maintainability. Each module should encapsulate a specific resource or set of resources.
  • Environment Isolation: Use separate workspaces or directories for different environments (development, staging, production) to avoid accidental changes in critical environments.
  • State Management: Use remote backends like AWS S3 or HashiCorp Consul to store your state file securely. This practice prevents state file corruption and allows team collaboration.
  • Regularly Update Providers: Keep your provider plugins up to date to take advantage of new features and bug fixes.
  • Document Your Code: Include comments and documentation within your configuration files to explain complex logic or resource configurations.
  • Run Automated Tests: Use tools like terratest or other testing frameworks to automate validation of your infrastructure code before applying changes.

NOTE: Join the DevOps Master’s Program by Network Kings to begin today!

Wrapping Up!

Terraform commands are fundamental tools that enable efficient infrastructure management. By mastering these commands and following best practices, you can streamline your workflow and enhance collaboration within your team. Start experimenting with these commands today to unlock the full potential of Terraform!

With continued practice and exploration of its capabilities, you will find that Terraform can enhance your DevOps processes and improve the overall efficiency of your infrastructure management tasks.

Join the DevOps Master’s Program by Network Kings to begin today! Feel free to reach out to us for assistance and details.

HAPPY LEARNING!

1 thought on “Basic Terraform Commands Cheat Sheet: Terraform CLI Commands ”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Attend Your First Free Demo Class

Fill out the form now to experience live classes with us. Learn Directly from Engineers working in big tech giants.

Attend Your First Free Demo Class

Fill out the form now to experience live classes with us. Learn Directly from Engineers working in big tech giants.