Skip to content

Instantly share code, notes, and snippets.

@mirzawaqasahmed
Last active December 31, 2021 16:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mirzawaqasahmed/819a32cedf73808e57fd4b0ab431d642 to your computer and use it in GitHub Desktop.
"""A Python Pulumi program"""
import pulumi
import pulumi_aws as aws
from netaddr import IPNetwork
from helper import vpc, subnet, _get_ipv4_subnets_of, _get_ipv6_subnets_of
vars = dict(
vpc_name="mwa-vpc1",
cidr="10.16.0.0/16",
)
resource_vpc = vpc(vars['vpc_name'], vars['cidr'], dns_support=True, dns_hostnames=True)
ipv4_subnets_generator = _get_ipv4_subnets_of(vars['cidr'], 20)
vpc_info = aws.ec2.get_vpc(id=resource_vpc.id, opts=pulumi.ResourceOptions(depends_on=[resource_vpc]))
ipv6_subnets_generator = _get_ipv6_subnets_of(vpc_info.ipv6_cidr_block, 64)
subnet_names = [
{0:["sn-reserved-A", "sn-db-A", "sn-app-A", "sn-web-A"]},
{1:["sn-reserved-B", "sn-db-B", "sn-app-B", "sn-web-B"]},
{2:["sn-reserved-C", "sn-db-C", "sn-app-C", "sn-web-C"]},
]
az = aws.get_availability_zones(state="available")
for i in subnet_names:
for az_idx, subnets in i.items():
for idx, name in enumerate(subnets):
vpc_id = resource_vpc.id
cidr = str(next(ipv4_subnets_generator))
az_id = az.zone_ids[az_idx]
ipv6_cidr = str(next(ipv6_subnets_generator))
# Create subnet
resource_subnet = subnet(name, vpc_id, cidr, az_id, ipv6_cidr)
pulumi.export("availability_zones_ids", az.zone_ids)
pulumi.export("availability_zones_names", az.names)
igw_name = f"{vars['vpc_name']}-igw"
resource_igw = igw(
igw_name, resource_vpc.id, opts=pulumi.ResourceOptions(depends_on=[resource_vpc])
)
rt_name = f"{vars['vpc_name']}-rt-web"
igw_routes = [
aws.ec2.RouteTableRouteArgs(
cidr_block="0.0.0.0/0",
gateway_id=resource_igw.id,
),
aws.ec2.RouteTableRouteArgs(
ipv6_cidr_block="::/0",
gateway_id=resource_igw.id,
),
]
resource_rt = route_table(
rt_name,
resource_vpc.id,
igw_routes,
opts=pulumi.ResourceOptions(depends_on=[resource_vpc]),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment