How to Create a VPC with Terraform - Complete AWS Networking Guide
Build a production-ready AWS VPC with Terraform. Covers subnets, route tables, NAT gateways, security groups, and network ACLs step by step.
Terraform
Compare Terraform vs AWS CloudFormation — multi-cloud support, state management, language, ecosystem, and when to use each for infrastructure as code.
Choose Terraform if you use multiple cloud providers or want provider-agnostic IaC. Choose CloudFormation if you're 100% AWS and want native integration with no state management overhead.
| Feature | Terraform | CloudFormation |
|---|---|---|
| Multi-cloud | ✅ AWS, Azure, GCP, 3000+ providers | ❌ AWS only |
| Language | HCL (HashiCorp Configuration Language) | JSON or YAML |
| State management | You manage (S3, Terraform Cloud) | AWS manages automatically |
| Drift detection | terraform plan (manual) | Built-in (automatic) |
| Rollback | Manual (apply previous config) | Automatic on failure |
| Pricing | Free (OSS) / Paid (Cloud) | Free (included with AWS) |
| Modules | Terraform Registry (15,000+) | Nested stacks, CDK constructs |
| Preview changes | terraform plan | Change Sets |
| Import existing | terraform import | resource import (since 2023) |
| Learning curve | Moderate (HCL) | Moderate (YAML/JSON) |
resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket-${var.environment}"
tags = {
Environment = var.environment
ManagedBy = "terraform"
}
}
resource "aws_s3_bucket_versioning" "data" {
bucket = aws_s3_bucket.data.id
versioning_configuration {
status = "Enabled"
}
}Resources:
DataBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "my-data-bucket-${Environment}"
VersioningConfiguration:
Status: Enabled
Tags:
- Key: Environment
Value: !Ref Environment
- Key: ManagedBy
Value: cloudformation
Parameters:
Environment:
Type: String
Default: devYou're responsible for state:
terraform {
backend "s3" {
bucket = "my-tf-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}AWS manages state automatically. No backend configuration needed. Drift detection is built in:
aws cloudformation detect-stack-drift --stack-name my-stack# 1. List CloudFormation resources
aws cloudformation list-stack-resources --stack-name my-stack
# 2. Write Terraform config for each resource
# 3. Import into Terraform state
terraform import aws_s3_bucket.data my-data-bucket
# 4. Verify
terraform plan # Should show no changes
# 5. Delete CloudFormation stack with DeletionPolicy: Retain# 1. terraform state rm each resource (keeps cloud resources)
# 2. Create CloudFormation template
# 3. Import resources into CloudFormation stack
aws cloudformation create-stack --stack-name my-stack \
--template-body file://template.yaml \
--parameters ParameterKey=...,ParameterValue=...| Myth | Reality |
|---|---|
| "Terraform is always better" | CloudFormation has better rollback and zero state overhead |
| "CloudFormation is simpler" | YAML templates get verbose fast; Terraform HCL is more concise |
| "You can't use both" | Many teams use both — CFN for foundational AWS, Terraform for app infra |
| "Terraform can't do X on AWS" | AWS provider covers 99%+ of services |
Both are production-ready IaC tools. Terraform wins on multi-cloud, ecosystem, and language expressiveness. CloudFormation wins on zero state management, automatic rollback, and native AWS integration. Many enterprises use both — CloudFormation for foundational AWS infrastructure and Terraform for application-level resources and multi-cloud services.
Build a production-ready AWS VPC with Terraform. Covers subnets, route tables, NAT gateways, security groups, and network ACLs step by step.
Deploy an AWS EC2 instance with Terraform step by step. Complete guide with VPC, security groups, key pairs, user data, and production-ready configuration.
Learn to use Terraform data sources to query existing infrastructure. Covers AWS AMI lookup, VPC discovery, AZ listing, and cross-state data access patterns.
Learn the AWS services essential for Terraform — IAM for authentication, S3 for state storage, DynamoDB for state locking. Complete setup guide.