G
GuideDevOps
Lesson 10 of 14

Terraform CLI

Part of the Terraform tutorial series.

The Terraform CLI is the primary way you interact with your infrastructure. Most of your daily work will revolve around four main commands.

1. terraform init

This is the first command you run. It initializes the working directory by downloading the necessary Providers (like AWS or Azure).

Action:

terraform init

Result:

Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching ">= 4.0.0"...
- Installing hashicorp/aws v4.67.0...
Terraform has been successfully initialized!

2. terraform plan

This command shows you exactly what Terraform intends to do before making any real changes. It's a "dry run."

Action:

terraform plan

Result:

Terraform will perform the following actions:
  # aws_instance.web will be created
  + resource "aws_instance" "web" {
      + ami                          = "ami-0c55b159cbfafe1f0"
      + instance_type                = "t2.micro"
      + ...
    }
 
Plan: 1 to add, 0 to change, 0 to destroy.

3. terraform apply

This command executes the plan and makes real changes to your cloud provider.

Action:

terraform apply -auto-approve

Result:

aws_instance.web: Creating...
aws_instance.web: Creation complete after 30s [id=i-0a1b2c3d4e5f6g7h8]
 
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

4. terraform destroy

Use this to tear down all infrastructure managed by this configuration. Be careful!

Action:

terraform destroy -auto-approve

Result:

aws_instance.web: Destroying... [id=i-0a1b2c3d4e5f6g7h8]
aws_instance.web: Destruction complete after 20s
 
Destroy complete! Resources: 0 added, 0 changed, 1 destroyed.

Summary

  • init: Prepares the directory.
  • plan: Previews changes.
  • apply: Makes the changes.
  • destroy: Deletes everything.
  • fmt: Bonus command to format your HCL code beautifully.