def create_vpc_stack(name, cidr, azs):
"""Create a complete VPC with subnets."""
vpc = infra.resource(
name = name + "_vpc",
type = "aws_vpc",
inputs = {"cidr_block": cidr, "enable_dns_hostnames": True},
)
subnets = []
for i, az in enumerate(azs):
subnet = infra.resource(
name = name + "_public_" + str(i),
type = "aws_subnet",
inputs = {
"vpc_id": ":" + name + "_vpc",
"cidr_block": cidr.replace(".0.0/16", "." + str(i) + ".0/24"),
"availability_zone": az,
"map_public_ip_on_launch": True,
},
)
subnets.append(subnet)
return infra.component(
name = name,
resources = [vpc] + subnets,
outputs = {"vpc_id": ":" + name + "_vpc.id"},
)
# Use the component
prod_network = create_vpc_stack("prod", "10.0.0.0/16", ["us-west-2a", "us-west-2b"])
staging_network = create_vpc_stack("staging", "10.1.0.0/16", ["us-west-2a", "us-west-2b"])