As asked
Write Terraform that provisions a VPC across three availability zones with public and private subnets and a NAT gateway per AZ. Explain the cost tradeoffs.
Sample answer outline
Use a module that takes a CIDR and a count of AZs and emits subnet pairs per AZ. One NAT per AZ keeps egress availability isolated to the AZ but triples the NAT bill. A single shared NAT is cheaper but means an AZ outage on the NAT side takes down outbound traffic for everyone. Tag everything with owner and cost-centre so the bill is debuggable. Use remote state with locking, plan in CI, apply manually for shared environments.
Reference implementation (hcl)
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "prod"
cidr = "10.0.0.0/16"
azs = ["eu-west-2a", "eu-west-2b", "eu-west-2c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = true
single_nat_gateway = false
one_nat_gateway_per_az = true
tags = { owner = "platform", env = "prod" }
}Expect these follow-ups
- How would you swap the NAT gateway for a NAT instance and when is that worth it?
- Where do VPC endpoints fit into the cost story?
- How do you handle drift when someone edits the console by hand?