如何从名称或 cidr 获取子网 ID
How to get subnet id from name or cidr
我需要从子网名称或 cidr 获取子网 ID 以部署 nat 网关。如何获取子网 ID?或者有人有使用打字稿功能的最佳实践吗?不好意思,我是打字菜鸟
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 } ]
});
}
}
我们可以捕获创建的子网并引用它(相当于 Cloudformation 中的 !Ref)
const myPublicSubnetOne: ec2.CfnSubnet = this.subnet_creation(
this.availabilityZones[0],
"public-01-a",
".0.0/20"
);
我们需要通过此方法return子网
subnet_creation(
availability_zone: string,
subnet_name: string,
subnet_cidr: string
) {
const subnet = 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 },
],
});
return subnet;
}
将输入添加到您的 nat_creation 函数并引用为 subnetId: myPubSubnet.ref,
nat_creation(az: string, myPubSubnet: ec2.CfnSubnet) {
const natgw_eip = new ec2.CfnEIP(this, "natgw-eip-" + az, {
domain: "vpc",
});
new ec2.CfnNatGateway(this, "natgw-" + az, {
allocationId: natgw_eip.attrAllocationId,
subnetId: myPubSubnet.ref,
tags: [{ key: "Name", value: this.svc + "-" + this.env + "-natgw" + az }],
});
}
传递子网本身,而不是字符串。
this.nat_creation("a", myPublicSubnetOne);
我需要从子网名称或 cidr 获取子网 ID 以部署 nat 网关。如何获取子网 ID?或者有人有使用打字稿功能的最佳实践吗?不好意思,我是打字菜鸟
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 } ]
});
}
}
我们可以捕获创建的子网并引用它(相当于 Cloudformation 中的 !Ref)
const myPublicSubnetOne: ec2.CfnSubnet = this.subnet_creation(
this.availabilityZones[0],
"public-01-a",
".0.0/20"
);
我们需要通过此方法return子网
subnet_creation(
availability_zone: string,
subnet_name: string,
subnet_cidr: string
) {
const subnet = 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 },
],
});
return subnet;
}
将输入添加到您的 nat_creation 函数并引用为 subnetId: myPubSubnet.ref,
nat_creation(az: string, myPubSubnet: ec2.CfnSubnet) {
const natgw_eip = new ec2.CfnEIP(this, "natgw-eip-" + az, {
domain: "vpc",
});
new ec2.CfnNatGateway(this, "natgw-" + az, {
allocationId: natgw_eip.attrAllocationId,
subnetId: myPubSubnet.ref,
tags: [{ key: "Name", value: this.svc + "-" + this.env + "-natgw" + az }],
});
}
传递子网本身,而不是字符串。
this.nat_creation("a", myPublicSubnetOne);