def __init__(self): resource_specs = ParseYAML(resource_type).getSpecs() aws_subnet_id = Subnets.SubnetId() aws_eip_id = ElasticIPs.ElasticIPId() for natgw_name, natgw_configuration in resource_specs.items(): # AWS NAT Gateway Variables resource_name = natgw_name resource_subnet = natgw_configuration["subnet"] resource_eip = natgw_configuration["elastic_ip"] resource_tags = None resource_tags = natgw_configuration[ "tags"] if "tags" in natgw_configuration else None # Getting list of tags from configuration file tags_list = {} if resource_tags is not None: for each_tag_name, each_tag_value in resource_tags.items(): tags_list.update({each_tag_name: each_tag_value}) # Adding mandatory tags tags_list.update({"Name": resource_name}) tags_list.update({ "Project/Stack": pulumi.get_project() + "/" + pulumi.get_stack() }) tags_list.update(resource_mandatory_tags) this_subnet = aws_subnet_id[str(resource_subnet)] this_eip = aws_eip_id[str(resource_eip)] aws_natgw = net.NatGateway(resource_name, subnet_id=this_subnet, allocation_id=this_eip, tags=tags_list) # Update resource dictionaries natgw_ids_dict.update({aws_natgw._name: aws_natgw.id}) # Export pulumi.export(aws_natgw._name, aws_natgw.id)
def _create_private_subnet_route_table(self, public_subnet_id, vpcid): eip_name = "%s-nat-eip" % self.name nat_name = "%s-nat" % self.name eip = ec2.Eip(eip_name, __opts__=ResourceOptions(parent=self)) nat_gateway = ec2.NatGateway(nat_name, subnet_id=public_subnet_id, allocation_id=eip.id, tags=self.vpc_tags, __opts__=ResourceOptions(parent=self)) rt_name = "%s-private-rt" % self.name private_route_table = ec2.RouteTable( rt_name, vpc_id=vpcid, routes=[{ "cidrBlock": "0.0.0.0/0", "gatewayId": nat_gateway.id }], __opts__=ResourceOptions(parent=self)) return private_route_table.id
def __init__(self, name: str, args: VpcArgs, opts: pulumi.ResourceOptions = None): """ Constructs a Vpc. :param name: The Pulumi resource name. Child resource names are constructed based on this. :param args: A VpcArgs object containing the arguments for VPC constructin. :param opts: A pulumi.ResourceOptions object. """ super().__init__('Vpc', name, None, opts) # Make base info available to other methods self.name = name self.description = args.description self.base_tags = args.base_tags # Create VPC and Internet Gateway resources self.vpc = ec2.Vpc(f"{name}-vpc", cidr_block=args.base_cidr, enable_dns_hostnames=True, enable_dns_support=True, tags={ **args.base_tags, "Name": f"{args.description} VPC" }, opts=pulumi.ResourceOptions(parent=self, )) self.internet_gateway = ec2.InternetGateway( f"{name}-igw", vpc_id=self.vpc.id, tags={ **args.base_tags, "Name": f"{args.description} VPC Internet Gateway" }, opts=pulumi.ResourceOptions(parent=self.vpc, )) # Calculate subnet CIDR blocks and create subnets subnet_distributor = SubnetDistributor( args.base_cidr, len(args.availability_zone_names)) self.public_subnets = [ ec2.Subnet(f"{name}-public-subnet-{i}", vpc_id=self.vpc.id, cidr_block=cidr, availability_zone=args.availability_zone_names[i], map_public_ip_on_launch=True, tags={ **args.base_tags, "Name": f"${args.description} Public Subnet {i}" }, opts=pulumi.ResourceOptions(parent=self.vpc, )) for i, cidr in enumerate(subnet_distributor.public_subnets) ] self.private_subnets = [ ec2.Subnet(f"{name}-private-subnet-{i}", vpc_id=self.vpc.id, cidr_block=cidr, availability_zone=args.availability_zone_names[i], tags={ **args.base_tags, "Name": f"${args.description} Private Subnet {i}" }, opts=pulumi.ResourceOptions(parent=self.vpc, )) for i, cidr in enumerate(subnet_distributor.private_subnets) ] # Adopt the default route table for this VPC and adapt it for use with public subnets self.public_route_table = ec2.DefaultRouteTable( f"{name}-public-rt", default_route_table_id=self.vpc.default_route_table_id, tags={ **args.base_tags, "Name": f"${args.description} Public Route Table" }, opts=pulumi.ResourceOptions(parent=self.vpc, )) ec2.Route(f"{name}-route-public-sn-to-ig", route_table_id=self.public_route_table.id, destination_cidr_block="0.0.0.0/0", gateway_id=self.internet_gateway.id, opts=pulumi.ResourceOptions(parent=self.public_route_table)) for i, subnet in enumerate(self.public_subnets): ec2.RouteTableAssociation( f"{name}-public-rta-{i + 1}", subnet_id=subnet.id, route_table_id=self.public_route_table, opts=pulumi.ResourceOptions(parent=self.public_route_table)) self.nat_elastic_ip_addresses: [ec2.Eip] = list() self.nat_gateways: [ec2.NatGateway] = list() self.private_route_tables: [ec2.RouteTable] = list() # Create a NAT Gateway and appropriate route table for each private subnet for i, subnet in enumerate(self.private_subnets): self.nat_elastic_ip_addresses.append( ec2.Eip(f"{name}-nat-{i + 1}", tags={ **args.base_tags, "Name": f"{args.description} NAT Gateway EIP {i + 1}" }, opts=pulumi.ResourceOptions(parent=subnet))) self.nat_gateways.append( ec2.NatGateway( f"{name}-nat-gateway-{i + 1}", allocation_id=self.nat_elastic_ip_addresses[i].id, subnet_id=self.public_subnets[i].id, tags={ **args.base_tags, "Name": f"{args.description} NAT Gateway {i + 1}" }, opts=pulumi.ResourceOptions(parent=subnet))) self.private_route_tables.append( ec2.RouteTable(f"{name}-private-rt-{i + 1}", vpc_id=self.vpc.id, tags={ **args.base_tags, "Name": f"{args.description} Private RT {i + 1}" }, opts=pulumi.ResourceOptions(parent=subnet))) ec2.Route(f"{name}-route-private-sn-to-nat-{i + 1}", route_table_id=self.private_route_tables[i].id, destination_cidr_block="0.0.0.0/0", nat_gateway_id=self.nat_gateways[i].id, opts=pulumi.ResourceOptions( parent=self.private_route_tables[i])) ec2.RouteTableAssociation( f"{name}-private-rta-{i + 1}", subnet_id=subnet.id, route_table_id=self.private_route_tables[i].id, opts=pulumi.ResourceOptions( parent=self.private_route_tables[i])) # Create S3 endpoint if necessary if args.create_s3_endpoint: ec2.VpcEndpoint(f"{name}-s3-endpoint", vpc_id=self.vpc.id, service_name=f"com.amazonaws.{config.region}.s3", route_table_ids=[ self.public_route_table.id, *[rt.id for rt in self.private_route_tables] ], opts=pulumi.ResourceOptions(parent=self.vpc)) # Create DynamoDB endpoint if necessary if args.create_dynamodb_endpoint: ec2.VpcEndpoint( f"{name}-dynamodb-endpoint", vpc_id=self.vpc.id, service_name=f"com.amazonaws.{config.region}.dynamodb", route_table_ids=[ self.public_route_table.id, *[rt.id for rt in self.private_route_tables] ], opts=pulumi.ResourceOptions(parent=self.vpc)) super().register_outputs({})
'Creator': 'timc' }) nat_eip = ec2.Eip( resource_name='new-nat-eip', # not using `associate_with_private_ip` because I don't have access to the private IP vpc=True, tags={ 'Name': 'infra nat eip (front-back-autoscaling)', 'Creator': 'timc' }) nat_gw = ec2.NatGateway(resource_name='new-nat-gw', allocation_id=nat_eip.id, subnet_id=public_subnet_1.id, tags={ 'Name': 'infra nat gw (front-back-autoscaling)', 'Creator': 'timc' }) private_subnet_rta_1 = ec2.RouteTableAssociation( resource_name='new-private-subnet-rta-1', route_table_id=private_subnet_rt.id, subnet_id=private_subnet_1.id) private_subnet_rta_2 = ec2.RouteTableAssociation( resource_name='new-private-subnet-rta-2', route_table_id=private_subnet_rt.id, subnet_id=private_subnet_2.id) # TODO remove reference to internet gateway ; can't have two default routes anyway
def _create_subnet(self, network_name, public_route_table, az_id): """ Creates a subnet in the network with the given name and using the given public route table to access the internet. The new subnet is created in the AZ with the given AZ index. :param network_name: The name of the network being created :param public_route_table: The route table to connect this subnet to the internet :param az_id: The AZ ID for this new subnet :return: A tuple of a public route table that this subnet will use to connect to the internet and the new subnet itself. If this subnet is private, the route table will point to a public NAT gateway that is connected to the new private subnet. """ # type: (str, ec2.RouteTable, int) -> (ec2.RouteTable, ec2.Subnet) subnet_name = "%s-%d" % (network_name, az_id) # Create the subnet for this AZ - either public or private subnet = ec2.Subnet( subnet_name, vpc_id=self.vpc_id, availability_zone=get_aws_az(az_id), cidr_block="10.10.%d.0/24" % az_id, map_public_ip_on_launch=not self.use_private_subnets, # Only assign public IP if we are exposing public subnets tags={ "Name": subnet_name, }, __opts__=ResourceOptions(parent=self)) # We will use a different route table for this subnet depending on # whether we are in a public or private subnet if self.use_private_subnets: # We need a public subnet for the NAT Gateway nat_name = "%s-nat-%d" % (network_name, az_id) nat_gateway_public_subnet = ec2.Subnet( nat_name, vpc_id=self.vpc_id, availability_zone=get_aws_az(az_id), # Use top half of the subnet space cidr_block="10.10.%d.0/24" % (az_id + 64), # Always assign a public IP in NAT subnet map_public_ip_on_launch=True, tags={"Name": nat_name}, __opts__=ResourceOptions(parent=self)) # And we need to route traffic from that public subnet to the Internet Gateway nat_gateway_routes = ec2.RouteTableAssociation( nat_name, subnet_id=nat_gateway_public_subnet.id, route_table_id=public_route_table.id, __opts__=ResourceOptions(parent=self)) self.public_subnet_ids.append(nat_gateway_public_subnet.id) # We need an Elastic IP for the NAT Gateway eip = ec2.Eip(nat_name, __opts__=ResourceOptions(parent=self)) # And we need a NAT Gateway to be able to access the Internet nat_gateway = ec2.NatGateway( nat_name, subnet_id=nat_gateway_public_subnet.id, allocation_id=eip.id, tags={"Name": nat_name}, __opts__=ResourceOptions(parent=self, depends_on=[nat_gateway_routes])) nat_route_table = ec2.RouteTable( nat_name, vpc_id=self.vpc_id, routes=[{ "cidrBlock": "0.0.0.0/0", "natGatewayId": nat_gateway.id }], tags={"Name": network_name}, __opts__=ResourceOptions(parent=self)) # Route through the NAT gateway for the private subnet return nat_route_table, subnet self.public_subnet_ids.append(subnet.id) return public_route_table, subnet
def setup_vpc(): # Create a VPC vpc_config = Config().require_object("vpc_config") vpc = ec2.Vpc("chatapp-vpc", cidr_block=vpc_config['cidr'], enable_dns_hostnames=True, enable_dns_support=True) # Create public subnet to place NGW public_subnet = ec2.Subnet("PublicSubnet", vpc_id=vpc.id, cidr_block=vpc_config['public_subnet_cidr'], availability_zone="ap-southeast-1a") # Create private subnets 1 and 2 for rds and redis clusters private_subnet_1 = ec2.Subnet( "PrivateSubnet1", vpc_id=vpc.id, cidr_block=vpc_config['private_subnet_1_cidr'], availability_zone="ap-southeast-1b") private_subnet_2 = ec2.Subnet( "PrivateSubnet2", vpc_id=vpc.id, cidr_block=vpc_config['private_subnet_2_cidr'], availability_zone="ap-southeast-1c") # Create internet gateway inet_gw = ec2.InternetGateway( "inet-gateway", vpc_id=vpc.id, ) # create NAT gateway elastic_ip = ec2.Eip("eip1", opts=ResourceOptions(delete_before_replace=True)) nat_gw = ec2.NatGateway("nat-gateway", subnet_id=public_subnet.id, allocation_id=elastic_ip.id) # Create private routed route-table private_subnet_route_table = ec2.RouteTable("privatesubnetroutetable", routes=[{ "cidr_block": "0.0.0.0/0", "gateway_id": nat_gw.id }], vpc_id=vpc.id) # Create public routed route-table public_subnet_route_table = ec2.RouteTable("publicsubnetroutetable", routes=[{ "cidr_block": "0.0.0.0/0", "gateway_id": inet_gw.id }], vpc_id=vpc.id) # Attach route tables to subnets ec2.RouteTableAssociation("PrivateSubnetRT1", subnet_id=private_subnet_1.id, route_table_id=private_subnet_route_table.id) ec2.RouteTableAssociation("PrivateSubnetRT2", subnet_id=private_subnet_2.id, route_table_id=private_subnet_route_table.id) ec2.RouteTableAssociation("PublicSubnetRT", subnet_id=public_subnet.id, route_table_id=public_subnet_route_table.id) return dict(vpc=vpc, private_subnets=[private_subnet_1, private_subnet_2])
rtb_privs = [] if not ha: eip = ec2.Eip( f"{APP}-eip0", vpc=True, tags={ 'Name': f"{APP}-eip0", 'Environment': _env } ) ngw = ec2.NatGateway( f"{APP}-ngw0", allocation_id=eip.id, subnet_id=subnets["public"][0], tags={ 'Name': f"{APP}-ngw0", 'Environment': _env } ) rtb_priv = ec2.RouteTable( f"{APP}-rtb-priv0", vpc_id=vpc.id, tags={ 'Name': 'rtb-priv', 'Environment': _env } ) rtb_privs.append(rtb_priv) rte_priv = ec2.Route( f"{APP}-rte-priv0",
#### private stuff private_subnet = ec2.Subnet( f"pulumi-private-subnet-{zone}", assign_ipv6_address_on_creation=False, vpc_id=vpc.id, map_public_ip_on_launch=False, cidr_block=private_subnet_cidr, availability_zone=zone, tags={"Name": f"pulumi-private-subnet-{zone}"}, ) eip = ec2.Eip(f"pulumi-eip-{zone}", tags={"Name": f"pulumi-eip-{zone}"}) nat_gateway = ec2.NatGateway( f"pulumi-natgw-{zone}", subnet_id=public_subnet.id, allocation_id=eip.id, tags={"Name": f"pulumi-natgw-{zone}"}, ) private_rt = ec2.RouteTable( f"pulumi-private-rt-{zone}", vpc_id=vpc.id, routes=[{ "cidr_block": "0.0.0.0/0", "gateway_id": nat_gateway.id }], tags={"Name": f"pulumi-private-rt-{zone}"}, ) ec2.RouteTableAssociation( f"pulumi-private-rta-{zone}", route_table_id=private_rt.id, subnet_id=private_subnet.id,
def __init__( self, name, opts=None, ): super().__init__("nuage:aws:DevelopmentEnvironment:VPC", f"{name}VpcEnvironment", None, opts) vpc = ec2.Vpc( f"{name}Vpc", cidr_block="172.32.0.0/16", enable_dns_hostnames=True, enable_dns_support=True, ) subnet_1 = ec2.Subnet( f"{name}VpcSubnetA", availability_zone="eu-west-1a", vpc_id=vpc.id, cidr_block="172.32.0.0/20", opts=ResourceOptions(depends_on=[vpc]), ) subnet_2 = ec2.Subnet( f"{name}VpcSubnetB", availability_zone="eu-west-1b", vpc_id=vpc.id, cidr_block="172.32.16.0/20", opts=ResourceOptions(depends_on=[vpc]), ) subnet_3 = ec2.Subnet( f"{name}VpcSubnetC", availability_zone="eu-west-1c", vpc_id=vpc.id, cidr_block="172.32.32.0/20", opts=ResourceOptions(depends_on=[vpc]), ) private_subnet_1 = ec2.Subnet( f"{name}VpcPrivateSubnetA", availability_zone="eu-west-1a", vpc_id=vpc.id, cidr_block="172.32.48.0/20", opts=ResourceOptions(depends_on=[vpc]), ) security_group = ec2.SecurityGroup( f"{name}SecurityGroup", vpc_id=vpc.id, opts=ResourceOptions(depends_on=[vpc]), ) security_group_rule = ec2.SecurityGroupRule( f"{name}SSHRule", security_group_id=security_group.id, type="ingress", protocol="tcp", from_port=22, to_port=22, cidr_blocks=["0.0.0.0/0"], ) security_group_rule = ec2.SecurityGroupRule( f"{name}InboundRule", security_group_id=security_group.id, type="ingress", protocol="all", from_port=0, to_port=65535, source_security_group_id=security_group.id, ) security_group_rule = ec2.SecurityGroupRule( f"{name}OutboundRule", security_group_id=security_group.id, type="egress", protocol="all", from_port=0, to_port=65535, cidr_blocks=["0.0.0.0/0"], ) subnets = [subnet_1, subnet_2, subnet_3] gateway = ec2.InternetGateway( f"{name}InternetGateway", vpc_id=vpc.id, opts=ResourceOptions(depends_on=[vpc]), ) gateway_route = ec2.Route( f"{name}GatewayRoute", destination_cidr_block="0.0.0.0/0", gateway_id=gateway.id, route_table_id=vpc.default_route_table_id, ) elastic_ip = ec2.Eip(f"{name}Eip", vpc=True, opts=ResourceOptions(depends_on=[gateway])) nat_gateway = ec2.NatGateway( f"{name}NatGateway", subnet_id=subnet_1.id, allocation_id=elastic_ip.id, opts=ResourceOptions(depends_on=[subnet_1, elastic_ip]), ) private_route_table = ec2.RouteTable( f"{name}PrivateRouteTable", routes=[ { "cidr_block": "0.0.0.0/0", "nat_gateway_id": nat_gateway.id, }, ], vpc_id=vpc.id, opts=ResourceOptions(depends_on=[private_subnet_1]), ) private_route_table_assoc = ec2.RouteTableAssociation( f"{name}PrivateRouteTableAssoc", route_table_id=private_route_table.id, subnet_id=private_subnet_1.id, ) outputs = { "vpc": vpc, "security_group": security_group, "public_subnets": [subnet_1, subnet_2, subnet_3], "private_subnet": private_subnet_1, "nat_gateway": nat_gateway, } self.set_outputs(outputs)