获取 EIP 作为 aws-cdk 生成的 VPC 定义的输出

lan*_*wen 0 aws-cloudformation aws-cdk

有一个 vpc 定义

 const vpc = new ec2.Vpc(this, 'SomeVPC', {
        cidr: '10.0.0.0/16',
        maxAzs: 2,
 });
Run Code Online (Sandbox Code Playgroud)

它在底层为 NAT 网关创建 2 个 EIP

"SomeVPCPublicSubnet1EIP58E3D6C5": {
  "Type": "AWS::EC2::EIP",
  "Properties": {
    "Domain": "vpc"
  }
}
Run Code Online (Sandbox Code Playgroud)

如何获取对它们的引用并通过导出CfnOutput?像这样的东西:

new CfnOutput(this, "ExternalIPOutput", {value: <some magic call to get SomeVPCPublicSubnet1EIP58E3D6C5.ref()>})
Run Code Online (Sandbox Code Playgroud)

Ami*_*nes 7

已经有一段时间了,但我今天遇到了这个问题,这就是我设法处理它并获取 EIP 的方法 -

代码片段:


// Create new VPC
const vpc = new ec2.Vpc(this, 'VPC', {
    cidr: props.customCidr,
    maxAzs: 2,
    subnetConfiguration: [
        {
            name: 'Private',
            subnetType: ec2.SubnetType.PRIVATE
        },
        {
            name: 'Public',
            subnetType: ec2.SubnetType.PUBLIC
        }
    ]
});

// Get Elastic IP 
vpc.publicSubnets.forEach((subnet, index) => {
  // Find the Elastic IP
  const EIP = subnet.node.tryFindChild('EIP') as ec2.CfnEIP
  new cdk.CfnOutput(this, `output-eip-${index}`, { value: EIP.ref });
})

Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述