Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix NAT Gateway NotFound errors when Elastic IP is missing or subnet is private. Covers public vs private NAT, EIP allocation, and VPC routing configuration.
The Elastic IP doesn't exist, the subnet is incorrect, or you're mixing public and private NAT Gateway types. Public NAT Gateways need an EIP and a public subnet. Private NAT Gateways don't need an EIP.
Error: creating EC2 NAT Gateway: InvalidElasticIpID.NotFound:
The elastic-ip ID 'eipalloc-xxx' does not existError: creating EC2 NAT Gateway: InvalidSubnetID.NotFound:
The subnet ID 'subnet-xxx' does not existError: creating EC2 NAT Gateway: InvalidParameterCombination:
Connectivity type 'private' does not support allocation IDsThe allocation_id references an EIP that was deleted, in a different region, or not yet created.
A public NAT Gateway must be in a public subnet (one with a route to an Internet Gateway).
Using allocation_id with connectivity_type = "private", or omitting it with the default public type.
The VPC has no Internet Gateway, so the public subnet can't actually route to the internet.
# 1. Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}
# 2. Public subnet with IGW route
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = { Name = "public-subnet" }
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
# 3. Elastic IP for NAT
resource "aws_eip" "nat" {
domain = "vpc"
tags = { Name = "nat-eip" }
}
# 4. NAT Gateway in public subnet
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public.id # Must be PUBLIC
tags = { Name = "main-nat" }
depends_on = [aws_internet_gateway.main]
}
# 5. Private subnet routes through NAT
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}
}For VPC-to-VPC traffic without internet access:
resource "aws_nat_gateway" "private" {
connectivity_type = "private"
subnet_id = aws_subnet.private.id
# No allocation_id needed for private NAT
tags = { Name = "private-nat" }
}variable "availability_zones" {
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
resource "aws_eip" "nat" {
for_each = toset(var.availability_zones)
domain = "vpc"
tags = { Name = "nat-${each.key}" }
}
resource "aws_nat_gateway" "main" {
for_each = toset(var.availability_zones)
allocation_id = aws_eip.nat[each.key].id
subnet_id = aws_subnet.public[each.key].id
depends_on = [aws_internet_gateway.main]
tags = { Name = "nat-${each.key}" }
}| Feature | Public | Private |
|---|---|---|
| Internet access | ✅ Yes | ❌ No |
| Elastic IP | ✅ Required | ❌ Not allowed |
| Subnet type | Public (IGW route) | Private |
| Use case | Private instances → internet | VPC-to-VPC routing |
| Cost | ~$0.045/hr + data | ~$0.045/hr + data |
aws ec2 describe-addresses --allocation-ids eipalloc-xxx)depends_on ensure the IGW exists before the NAT?depends_on = [aws_internet_gateway.main] on NAT GatewaysNAT Gateway NotFound errors are typically about EIP/subnet mismatches. Public NAT Gateways need an EIP and a public subnet with an Internet Gateway route. Private NAT Gateways don't use EIPs. Always create the EIP and IGW in the same config and use depends_on to enforce creation order.
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
Fix terraform import errors when a resource already exists in state. Covers state rm, state show, reimport workflow, import blocks
Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan
Fix terraform invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.