0
0
Terraformcloud~5 mins

Terraform CLI overview - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform CLI helps you create and manage cloud resources by running simple commands. It solves the problem of manually setting up infrastructure by automating the process with code.
When you want to create a virtual server in the cloud quickly and repeatedly.
When you need to update your cloud resources safely without breaking anything.
When you want to see what changes will happen before applying them.
When you want to keep track of your infrastructure setup in files.
When you want to destroy cloud resources you no longer need to save costs.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-cli-bucket-12345"
  acl    = "private"
}

This file tells Terraform to use the AWS provider in the us-east-1 region. It defines one resource: an S3 bucket with a unique name and private access. The terraform block sets the provider version and Terraform version requirements.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. It prepares your environment to run Terraform commands.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do if you apply the changes. It helps you check before making any real changes.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "example-terraform-cli-bucket-12345" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes and creates the S3 bucket. The flag skips the confirmation prompt to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=example-terraform-cli-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval prompt
This command deletes the S3 bucket and cleans up your cloud resources. The flag skips the confirmation prompt.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skip manual approval prompt
Key Concept

If you remember nothing else from this pattern, remember: Terraform CLI commands let you safely create, preview, change, and delete cloud resources using simple steps.

Common Mistakes
Running terraform apply before terraform init
Terraform needs to download provider plugins first; without init, apply will fail.
Always run terraform init once before any other Terraform commands in a new folder.
Not running terraform plan before apply
You might apply unexpected changes without reviewing them first.
Run terraform plan to see what will change before applying.
Using terraform apply without -auto-approve and expecting no prompt
Terraform will pause and ask for confirmation, which can interrupt automation.
Use -auto-approve flag to skip confirmation in scripts or when you want faster apply.
Summary
terraform init prepares your folder by downloading needed plugins.
terraform plan shows what changes Terraform will make without applying them.
terraform apply creates or updates resources as defined in your files.
terraform destroy removes resources you no longer need.