如何获取已创建的EC2实例的实例ID

时间:2019-07-06 17:51:32

标签: aws-cdk

尝试获取由CF创建(由AWS CDK生成)的EC2实例的实例ID,该实例ID至少在v0.28中可用:

    const natInstance1 = new CfnInstance(this, 'NatInstance1', {
      imageId: NAT_AMI_ID,
      instanceType: NAT_INSTANCE_TYPE,
      subnetId: vpc.publicSubnets[0].subnetId,
      keyName: NAT_SSH_KEY,
      sourceDestCheck: false,
      securityGroupIds: [ 
        publicSecurityGroup.securityGroupId
      ]
    });

    new ec2.CfnEIPAssociation(this, 'NatEip1', {
      eip: NAT_EIP_POOL[0],
      instanceId: natInstance1.instanceId
    });

此功能在v0.37.0中不再起作用,出现错误:Property 'instanceId' does not exist on type 'CfnInstance'.ts(2339)

1 个答案:

答案 0 :(得分:0)

问题被提出为aws-cdk issue并得到回答。这里是从链接的答案中摘录的代码段:

new ec2.CfnEIPAssociation(this, 'NatEip1', {
  eip: NAT_EIP_POOL[0],
  instanceId: natInstance1.ref
});