Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CDK How to reference subnets just created using CDK

Tags:

aws-cdk

I'm trying to reference a couple of subnets to create ec2 instances but I'm sort of stuck. Let's say I have this:

   const vpc = new Vpc(this, 'gwc-vpc', {
      cidr: "10.20.0.0/16",
      maxAzs: 3,
      subnetConfiguration: [
        {
          subnetType: SubnetType.PRIVATE,
          name: 'gossipSubnet',
          cidrMask: 24
        },
        {
          subnetType: SubnetType.PRIVATE,
          name: 'dbSubnet',
          cidrMask: 24
        },
        {
          subnetType: SubnetType.PUBLIC,
          name: 'extSubnet',
          cidrMask: 24
        }
      ]
    })

How can I create an instance in the dbSubnet for example? Even better, what if I want to create an EC2 instance with 2 interfaces each of one is sitting in a different subnet (dbSubnet and gossipSubnet)? Any ideas?

Thanks!

like image 895
alf Avatar asked Dec 22 '22 21:12

alf


1 Answers

Not sure if this is relevant here, but couldn't you also use the built-ins to select/target the subnet you are interested in?

const vpc = new Vpc(this, 'ProductionVPC', {
            cidr: '10.10.0.0/16',
            enableDnsHostnames: true,
            enableDnsSupport: true,
            defaultInstanceTenancy: DefaultInstanceTenancy.DEFAULT,
            maxAzs: 2,
            natGateways: 1,
            subnetConfiguration: [
                {
                    cidrMask: 24,
                    name: 'Public',
                    subnetType: SubnetType.PUBLIC
                },
                {
                    cidrMask: 24,
                    name: 'Private - Application',
                    subnetType: SubnetType.PRIVATE,
                },
                {
                    cidrMask: 24,
                    name: 'Private - Database',
                    subnetType: SubnetType.ISOLATED,
                },
            ]
        });
        Tag.add(vpc, 'Name', 'Production VPC');

        const subnets = vpc.selectSubnets({subnetType: SubnetType.Public)).subnets;
like image 85
Niklaus König Avatar answered Jan 06 '23 02:01

Niklaus König