Terraform on AWS

💤0
Lv 10 XP
← 🟧 Amazon Web Services · Infrastructure as Code

Terraform on AWS

Intermediate ⭐ 80 XP ⏱ 18 min #aws#terraform#iac

Provision AWS with Terraform's aws provider, with remote state in S3.

📖Theory

Terraform manages AWS through the aws provider. You configure a region and authenticate via the AWS CLI credentials, environment variables, or — best for CI — OIDC role assumption (no static keys). The workflow is the same everywhere: initplanapply.

For teams, store remote state in S3 with a DynamoDB table for locking, so state is shared and concurrent applies can’t corrupt it. Compared to CloudFormation, Terraform is multi-cloud and uses HCL; CloudFormation is AWS-native.

🌍Real-World Example
terraform {
  backend "s3" {
    bucket         = "my-tfstate"
    key            = "prod/terraform.tfstate"
    region         = "eu-west-1"
    dynamodb_table = "tf-locks"
    encrypt        = true
  }
}

provider "aws" {
  region = "eu-west-1"
}

resource "aws_s3_bucket" "uploads" {
  bucket = "my-app-uploads-12345"
}
terraform init && terraform plan && terraform apply
✍️Hands-On Exercise
  1. Write a provider "aws" block with a region.
  2. Configure an S3 backend with DynamoDB locking.
  3. Add a resource for an S3 bucket and run terraform plan.
  4. Compare Terraform and CloudFormation in two sentences.
🧾Cheat Sheet
ItemDetail
Providerprovider "aws" { region = … }
Auth (CI)OIDC role assumption
Remote statebackend "s3"
LockingDynamoDB table
Workflowinitplanapply
vs CloudFormationMulti-cloud, HCL vs AWS-native YAML
💬Common Interview Questions
How do you store Terraform state for an AWS team?

In an S3 bucket (versioned, encrypted) with a DynamoDB table for state locking, so everyone shares one authoritative state and concurrent applies are serialized.

Terraform vs CloudFormation — when would you pick each?

Terraform for multi-cloud or a single toolchain across providers, with HCL and a large module ecosystem. CloudFormation when staying fully AWS-native with deep service integration and no extra tooling.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type