导出CDK生成的InternetGateway资源[AWS Cloudformation]



所以我在CDK上有了一个基本的VPC设置,在运行cdk synth之后,我看到我的模板在CDK.out下生成了一个AWS::EC2::InternetGateway构造。问题是我需要将这个资源导出到另一个堆栈,但我无法在我的主.ts CDK文件中引用this.vpc.internetGatewayId来导出。

我知道这个资源已经创建,所以我不会使用CfnInternetGateway来创建另一个,但同时this.vpc.internetGatewayId似乎返回了未定义的值,即使我已经创建了一个公共子网,并且生成的资源在模板文件中。

以下是我的VPC结构:

this.vpc = new ec2.Vpc(this, 'VPC', {
cidr: CONFIG.REGIONAL_CONFIG[region].VPC_CIDR,
maxAzs: CONFIG.REGIONAL_CONFIG[region].AVAILABILITY_ZONES,
subnetConfiguration: [
{
name: 'public',
subnetType: ec2.SubnetType.PUBLIC,
},
{
name: 'private',
subnetType: ec2.SubnetType.PRIVATE_WITH_NAT,
},
{
name: 'isolated',
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
},
],
});

使用CfOutput:在导出堆栈上创建CloudFormation输出

// MyExportingStack.ts
// Cause the synth to fail if the ID is undefined.  Alternatively, throw an error.
if (!vpc.internetGatewayId)
cdk.Annotations.of(this).addError('An Internet Gateway ID is required!');
// Create the CloudFormation Output
new cdk.CfnOutput(this, 'IgwId', { value: vpc.internetGatewayId ?? 'MISSING', exportName: 'my-igw-id',});

最新更新