Terraform Basics for AWS

Terraform is a popular multi-cloud IaC tool with a clean plan-then-apply workflow.

Terraform is a widely-used IaC tool that works across many clouds. You declare resources in HCL (HashiCorp Configuration Language), and Terraform figures out how to reach that state.

The core workflow

  • terraform init — download the AWS provider.
  • terraform plan — preview the changes.
  • terraform apply — make them real.
  • terraform destroy — tear it all down.

State

Terraform records what it created in a state file. On a team, store that state remotely (an S3 bucket with locking) so everyone shares one source of truth and no one clobbers another's changes.

Example

Example · bash
# main.tf (Terraform HCL)
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "web" {
  bucket = "myapp-prod-web-2026"
}

resource "aws_s3_bucket_public_access_block" "web" {
  bucket                  = aws_s3_bucket.web.id
  block_public_acls       = true
  restrict_public_buckets = true
}

# Then: terraform init && terraform plan && terraform apply

When to use it

  • A team uses Terraform to provision AWS infrastructure and Azure networking in the same plan because their product runs on both clouds.
  • An engineer runs terraform plan in CI so every pull request shows exactly which infrastructure changes the code will produce.
  • A company stores Terraform state in S3 with DynamoDB locking so multiple engineers can run plans and applies safely at the same time.

More examples

Terraform AWS provider config

Configures the AWS provider pinned to version 5.x and stores state in S3 with DynamoDB for concurrency locking.

Example · hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  backend "s3" {
    bucket         = "my-tf-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "tf-locks"
  }
}

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

Declare an S3 bucket resource

Declares an S3 bucket whose name includes the environment variable, making the same code reusable across stages.

Example · hcl
resource "aws_s3_bucket" "site" {
  bucket = "${var.env}-my-site"

  tags = {
    Environment = var.env
    ManagedBy   = "terraform"
  }
}

variable "env" {
  description = "Deployment environment"
  default     = "dev"
}

Plan, apply, and destroy lifecycle

Shows the full Terraform lifecycle: initialise, plan, apply, and optionally destroy — the core workflow for any change.

Example · bash
terraform init        # download providers and modules
terraform plan        # preview changes
terraform apply -auto-approve  # apply changes
# To tear down:
terraform destroy -auto-approve

Discussion

  • Be the first to comment on this lesson.
Terraform Basics for AWS — AWS Deployment | SoundsCode