Skip to content

Instantly share code, notes, and snippets.

@clstokes

clstokes/vpc.py Secret

Created March 27, 2020 04:32
  • 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
Embed
What would you like to do?
from pulumi_aws import ec2
class VPCOptions:
def __init__(self,cidr_block, subnets):
self.cidr_block = cidr_block
self.subnets = subnets
class VPC:
def __init__(self,
name: str,
options: VPCOptions):
self.name = name
self._options = options
self._manage()
self._manage_igw()
self._manage_subnets()
self._manage_natgw()
def _manage(self):
self.vpc = ec2.Vpc(self.name,
cidr_block = self._options.cidr_block)
def _manage_igw(self):
name = f'{self.name}-igw'
print(f'VPC ID before ec2.InternetGateway: {self.vpc.id}')
ec2.InternetGateway(name,
vpc_id=self.vpc.id)
def _manage_natgw(self):
name = f'{self.name}-natgw'
print(f'VPC ID before deriving subnets for ec2.NatGateway: {self.vpc.id}')
for i, subnet in enumerate(self.subnets):
eip = ec2.Eip(f'{i}',
vpc=True)
ec2.NatGateway(f'{i}',
subnet_id=subnet.id,
allocation_id=eip.id)
def _manage_subnets(self):
self.subnets = []
for subnet in self._options.subnets: # TODO: change this back to subnet_generator
name = subnet.get('name')
self.subnets.append(ec2.Subnet(name,
availability_zone=subnet.get('availability_zone'),
cidr_block=subnet.get('cidr_block'), # TODO: change this back to cidr_mask
vpc_id=self.vpc.id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment