Terraform, developed by HashiCorp, is a powerful Infrastructure as Code (IaC) tool that enables the management of infrastructure through simple, human-readable configuration files. To effectively use Terraform, it's essential to understand its command-line interface (CLI) commands. In this article, we will explore the main Terraform CLI commands that are crucial for managing your infrastructure.

Basic Terraform CLI Commands

These basic commands are fundamental to working with Terraform and managing your infrastructure resources.

1. terraform init

The terraform init command initializes a Terraform configuration. This command sets up the working directory containing Terraform configuration files by downloading and installing the necessary provider plugins.

terraform init

Example output:


Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.27.0...
- Installed hashicorp/aws v3.27.0 (signed by HashiCorp)
    

2. terraform plan

The terraform plan command creates an execution plan. It shows the changes that will be made to your infrastructure based on the current state and your configuration files. This command is essential for understanding what Terraform will do before you actually apply the changes.

terraform plan

Example output:


Refreshing Terraform state in-memory prior to plan...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami           = "ami-0c55b159cbfafe1f0"
      + instance_type = "t2.micro"
      + tags          = {
          + "Name" = "example-instance"
        }
    }
    

3. terraform apply

The terraform apply command applies the changes required to reach the desired state of the configuration. It creates, updates, or deletes infrastructure resources as necessary to match the configuration files.

terraform apply

Example output:


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami           = "ami-0c55b159cbfafe1f0"
      + instance_type = "t2.micro"
      + tags          = {
          + "Name" = "example-instance"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.example: Creating...
aws_instance.example: Creation complete after 42s [id=i-0c1234567890abcdef]
    

4. terraform destroy

The terraform destroy command is used to destroy the infrastructure managed by Terraform. This command is useful for cleaning up resources that are no longer needed.

terraform destroy

Example output:


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.example will be destroyed
  - resource "aws_instance" "example" {
      - ami           = "ami-0c55b159cbfafe1f0" -> null
      - instance_type = "t2.micro" -> null
      - tags          = {
          - "Name" = "example-instance"
        } -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.example: Destroying... [id=i-0c1234567890abcdef]
aws_instance.example: Destruction complete after 38s
    

Advanced Terraform CLI Commands

Beyond the basic commands, Terraform provides several advanced commands to enhance your infrastructure management workflow.

5. terraform validate

The terraform validate command checks whether the configuration files are syntactically valid and internally consistent. It is useful for catching errors early in the development process.

terraform validate

Example output:


Success! The configuration is valid.
    

6. terraform output

The terraform output command extracts the outputs from the state file and displays them. Outputs are often used to get information about the infrastructure that was created.

terraform output

Example output:


example_instance_public_ip = "3.123.456.789"
    

7. terraform state

The terraform state command is used to view and manage the state file. This command allows you to inspect the current state of your infrastructure and make low-level adjustments.

terraform state list

Example output:


aws_instance.example
    

8. terraform fmt

The terraform fmt command formats the Terraform configuration files to a canonical format and style. This command helps ensure consistency and readability in your codebase.

terraform fmt

Example output:


Formatting .tf files in the current directory...
    

9. terraform import

The terraform import command imports existing infrastructure into your Terraform state. This command is useful for bringing existing resources under Terraform management.

terraform import aws_instance.example i-0c1234567890abcdef

Example output:


aws_instance.example: Importing from ID "i-0c1234567890abcdef"...
aws_instance.example: Import prepared!
  Prepared aws_instance for import
aws_instance.example: Refreshing state... [id=i-0c1234567890abcdef]

Import successful!
    

Understanding and utilizing the main Terraform CLI commands is essential for effective infrastructure management. These commands provide the necessary tools to initialize, plan, apply, and manage your infrastructure configurations. Whether you are new to Terraform or an experienced user, mastering these commands will enhance your ability to automate and optimize your infrastructure operations.