Terraform on AWS
Manage AWS with Terraform: providers, resources, variables, remote state in S3 with locking, modules and safe plan/apply workflows.
Intermediate · about 12 hours · difficulty 3/5
Learn first: Infrastructure as Code Concepts
Terraform (and its open-source fork OpenTofu) is the most common IaC tool in job postings because one language (HCL) works across AWS, Azure, GCP and hundreds of SaaS providers.
Core building blocks
- Provider: the AWS provider, configured with a region such as ap-south-1.
- Resources: aws_vpc, aws_subnet, aws_instance and so on.
- Data sources: read existing things, like the latest Amazon Linux AMI.
- Variables, locals and outputs for inputs, computed values and results.
- Modules: folders of reusable code; the public registry has well-tested modules such as terraform-aws-modules/vpc.
The workflow
terraform init, fmt, validate, plan, apply, and destroy when the lab is over. Read every plan line marked to destroy or replace before you type yes.
State
- Local state is fine for learning. Teams use an S3 backend with versioning and encryption, plus state locking (S3 native lockfile in recent versions, or DynamoDB in older setups).
- State can contain secrets: never commit it, restrict access to the bucket.
- terraform import and import blocks bring hand-made resources under management.
Good practices
- Pin provider and module versions.
- Separate state per environment and per component (network, data, apps).
- Run plan in CI on pull requests; apply from the pipeline.
- Use default_tags in the provider so every resource gets project and owner tags.
Pitfalls
- Committing terraform.tfstate or .terraform to Git.
- Renaming a resource block and accidentally destroying and recreating a database.
You are ready to move on when you can rebuild your VPC lab entirely in Terraform with remote state, then destroy it with one command.
Key concepts
- Providers and resources
- Variables, locals, outputs
- init, plan, apply, destroy
- Remote state in S3 with locking
- Modules and the registry
- Data sources
- Import and moved blocks
- Workspaces vs separate state
- OpenTofu
Other options for this step
- CloudFormation and AWS CDK: Use AWS-native IaC: CloudFormation templates and stacks, change sets and drift detection, and the CDK to write infrastructure in TypeScript or Python.
Mini project
Write Terraform for a two-AZ VPC (public and private subnets, no NAT), a security group and one free-tier EC2 instance with a user data web server, using an S3 remote backend and default_tags. Apply, verify the page loads, then terraform destroy and confirm in the console that nothing is left except the small state bucket.
Check yourself
Your plan shows an RDS instance will be destroyed and recreated after you renamed its resource block. What happened and how do you fix it safely?
Free resources
- Terraform documentation (HashiCorp)
- Get Started with Terraform on AWS (HashiCorp)
- AWS provider documentation (Terraform Registry)
- Terraform roadmap (roadmap.sh)