While working with Terraform and AWS recently I ran into an error that did not seem to have much information about it. After about a day of research and troubleshooting I was able to solve it.
The Error
Error: Error applying plan:
1 error(s) occurred:
* module.web_app.aws_db_instance.rds: 1 error(s) occurred:
* aws_db_instance.rds: Error creating DB Instance: InvalidVPCNetworkStateFault: Cannot create a db.t2.micro database instance because no subnets exist in availability zones with sufficient capacity for VPC and storage type : gp2 for db.t2.micro. Please first create at least one new subnet; choose from these availability zones: us-west-1c, us-west-1b.
status code: 400, request id: ea5f04be-8510-4cfc-9bb2-606c0e00d007
The key takeaways here are RDS, subnets, and availability. So I checked the VPC AZ’s, the subnets assigned to them, CIDR ranges, etc. At one point I even compared the VPC configuration to a working zone. From what I could tell no differences existed.
The Cause
After some digging around I noticed the default VPC’s subnets had been deleted. This causes the VPC and associated AZ subnets to be invalid in the default DB security group. The only way to recreate default subnets in a region is via the CLI, no web console ability for this action.
The Fix
The fix was to go into RDS subnet group configuration (https://us-west-1.console.aws.amazon.com/rds/home?region=us-west-1#db-subnet-groups) and re-assign the new two new default subnets to the RDS group. After that Terraform ‘plan’ and ‘apply’ returned to working as expected.