0
0
Terraformcloud~5 mins

Terraform's declarative approach - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform helps you create and manage cloud resources by describing what you want, not how to do it. This way, you write a simple plan, and Terraform figures out the steps to make it real.
When you want to set up a virtual server in the cloud without manually clicking buttons.
When you need to create a network and storage for your app and want to keep track of it easily.
When you want to update your cloud setup safely and see what changes will happen before applying them.
When you want to share your cloud setup with teammates so everyone uses the same settings.
When you want to delete all your cloud resources cleanly without leaving leftovers.
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 = "my-example-bucket-terraform-12345"
  acl    = "private"
}

This file tells Terraform to use the AWS provider in the us-east-1 region.

It declares a resource: an S3 bucket named "my-example-bucket-terraform-12345" with private access.

Terraform will create this bucket when you apply the configuration.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. You run it first to prepare your workspace.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.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 to reach the desired state. It helps you check before making any changes.
Terminal
terraform plan
Expected OutputExpected
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 = "my-example-bucket-terraform-12345" + force_destroy = false + id = (known after apply) + region = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create the S3 bucket as described. The -auto-approve flag skips the confirmation step.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=my-example-bucket-terraform-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual confirmation to apply changes immediately
This command deletes the S3 bucket and cleans up the resources Terraform created. The -auto-approve flag skips confirmation.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... [id=my-example-bucket-terraform-12345] aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skip manual confirmation to destroy resources immediately
Key Concept

If you remember nothing else from this pattern, remember: you declare what you want, and Terraform figures out how to make it happen.

Common Mistakes
Running terraform apply before terraform init
Terraform needs to download provider plugins first; without init, apply fails.
Always run terraform init first to prepare your workspace.
Changing resources manually in the cloud without updating Terraform files
Terraform's state will not match reality, causing errors or overwriting changes.
Make all changes through Terraform configuration and apply commands.
Not running terraform plan before apply
You might apply unintended changes without seeing what will happen.
Run terraform plan to review changes before applying.
Summary
terraform init prepares your folder by downloading needed plugins.
terraform plan shows what changes Terraform will make to match your desired setup.
terraform apply creates or updates resources to reach the declared state.
terraform destroy removes all resources declared in your configuration.