Practical, copy-paste Terraform examples for AWS — each one is a small, self-contained
configuration you can init, plan, and apply in minutes. Every example is terraform fmt
and terraform validate clean and pinned to current provider versions.
New to Terraform? Start with how to install Terraform on any OS, then come back and run any example below.
| Example | What it builds | Full walkthrough |
|---|---|---|
| aws-ec2-instance | A single EC2 instance with a security group, in the default VPC | Deploy an EC2 instance with Terraform |
| aws-s3-bucket | A private, versioned, encrypted S3 bucket | S3 bucket best practices with Terraform |
| aws-vpc | A VPC with public + private subnets, IGW, and NAT gateway | Terraform AWS VPC complete example |
# 1. Install Terraform (see the install guide linked above)
terraform -version # 1.5+ recommended
# 2. Pick an example
cd examples/aws-ec2-instance
# 3. Configure AWS credentials (env vars or `aws configure`)
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_REGION=us-east-1
# 4. Provision
terraform init
terraform plan
terraform apply
# 5. Tear down when you're done (avoid surprise bills)
terraform destroy- Pinned versions —
required_versionand providerversionconstraints soapplyis reproducible. - Variables with sane defaults — override via
terraform.tfvarsor-var. - Outputs — every example prints the IDs/IPs you actually need.
- Tags — resources are tagged so they're easy to find and clean up.
- No secrets in code — credentials come from the environment or your AWS profile.
Hitting an error? The most common Terraform failures (state locks, provider auth,
Error creating ... conflicts, dependency cycles) are documented with fixes in the
Terraform troubleshooting guide.
- Install Terraform on any OS
- Terraform alternatives compared (OpenTofu, Pulumi, CDK)
- Getting started with Terraform on AWS
MIT — use these examples freely in your own projects.