I am seeking help on retrieving the subnet id based on subnet name or cidr in order to deploy a nat gateway. Can someone provide guidance on how to obtain the subnet id? Alternatively, does anyone have any best practices for utilizing typescript functions? Apologies, as I am relatively new to typescript.
export class VpcTestStack extends cdk.Stack {
svc = 'common';
env = 'test';
cidr = '10.10';
vpc: ec2.CfnVPC;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.vpc = new ec2.CfnVPC(this, 'vpc', {
...
});
this.subnet_creation(this.availabilityZones[0], 'public-01-a', '.0.0/20');
this.subnet_creation(this.availabilityZones[2], 'public-01-c', '.16.0/20');
...
this.subnet_creation(this.availabilityZones[0], 'private-03-a', '.192.0/20');
this.subnet_creation(this.availabilityZones[2], 'private-03-c', '.208.0/20');
this.nat_creation('a', 'public-02-a')
this.nat_creation('c', 'public-02-c')
}
subnet_creation(availability_zone: string, subnet_name: string, subnet_cidr: string)
{
new ec2.CfnSubnet(this, 'subnet-' + subnet_name, {
availabilityZone: availability_zone,
cidrBlock: this.cidr + subnet_cidr,
vpcId: this.vpc.ref,
tags: [ { key: 'Name', value: this.svc + '-' + this.env + '-' + subnet_name } ]
});
}
nat_creation(az: string, subnet_name: string)
{
const natgw_eip = new ec2.CfnEIP(this, 'natgw-eip-' + az, {
domain: 'vpc'
});
new ec2.CfnNatGateway(this, 'natgw-' + az, {
allocationId: natgw_eip.attrAllocationId,
subnetId: ???, <---------------------------------------------------------------------- Here
tags: [ { key: 'Name', value: this.svc + '-' + this.env + '-natgw' + az } ]
});
}
}