Skip to content
VirtualLP Virtual Learning Platform

Terraform on AWS

12 min read Topic 19 of 30 1 hands-on lab

Terraform (HashiCorp) is the most widely used multi-cloud infrastructure-as-code tool. You describe resources in HCL; Terraform calls the AWS APIs to make reality match, and records what it created in a state file.

A small configuration

terraform {
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 5.0" }
  }
  backend "s3" {
    bucket         = "acme-terraform-state"
    key            = "notes/dev.tfstate"
    region         = "ap-south-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}

provider "aws" {
  region = var.region
}

variable "region" {
  type    = string
  default = "ap-south-1"
}

resource "aws_s3_bucket" "site" {
  bucket = "notes-site-${var.region}-demo"
  tags   = { Project = "notes", Env = "dev" }
}

resource "aws_dynamodb_table" "notes" {
  name         = "Notes"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "id"
  attribute {
    name = "id"
    type = "S"
  }
}

output "bucket_arn" {
  value = aws_s3_bucket.site.arn
}

The workflow

flowchart LR
    W[Write .tf] --> I["terraform init<br/>download providers, set up backend"]
    I --> P["terraform plan<br/>+ create · ~ update · - destroy"]
    P --> A["terraform apply<br/>call AWS APIs, update state"]
    A --> D["terraform destroy<br/>remove everything"]
Command Does
terraform init Installs providers/modules, configures the backend
terraform fmt / validate Formats and checks syntax
terraform plan Shows what would change – review it like a code diff
terraform apply Makes the changes
terraform destroy Deletes managed resources
terraform state list / import Inspect state, adopt existing resources

State

  • The state file maps configuration to real resource IDs – Terraform needs it to plan.
  • Store it remotely in S3 with DynamoDB locking (or S3 native locking) so a team never runs two applies at once.
  • It can contain secrets – encrypt and restrict access.

Reuse with modules

A module is a folder of Terraform files with inputs and outputs – e.g. a vpc module used by every project, or community modules from the Terraform Registry (terraform-aws-modules/vpc/aws).

Terraform vs CloudFormation

Terraform CloudFormation
Clouds AWS, Azure, GCP, Kubernetes, SaaS… AWS only
Language HCL YAML/JSON (+ CDK)
State You manage the state file Managed by AWS
Preview plan Change sets
Licence BSL (OpenTofu = open-source fork) AWS service
Key point

Always read the plan before apply, keep remote state with locking, and never edit Terraform-managed resources by hand.

Practise it

Virtual lab · Amazon Web Services · 35 min

Terraform on AWS: init, plan, apply, change and destroy from CloudShell

Use the Terraform workflow against your AWS account: initialise the AWS provider, read an execution plan, apply it, use outputs and state, change the configuration and see in-place updates versus additions, and destroy everything cleanly.

Open lab

Quick revision

  1. HCL: terraform block, provider, resources, variables, outputs.
  2. init → plan → apply → destroy.
  3. Remote state in S3 with locking; state may hold secrets.
  4. Modules package reusable infrastructure.
  5. Terraform is multi-cloud; CloudFormation is AWS-native.

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.