简体   繁体   English

如何让 CDK 仅创建一个弹性 IP 地址以用于单个 EC2 实例 (AWS CDK)

[英]How to have the CDK create only one Elastic IP address for use with a single EC2 instance (AWS CDK)

I'm trying to create a Stack using the AWS CDK that will deploy a single EC2 instance, create an Elastic IP and then associate it with that instance.我正在尝试使用将部署单个 EC2 实例的 AWS CDK 创建一个堆栈,创建一个弹性 IP,然后将其与该实例相关联。 (MVP) (MVP)

For reasons I don't understand, an elastic IP is created for each public subnet of the VPC my EC2 instance is within.由于我不明白的原因,为我的 EC2 实例所在的 VPC 的每个公共子网创建了一个弹性 IP。 I expect that only one should be created, not three.我希望只创建一个,而不是三个。

Below is my simplified code:下面是我的简化代码:

const vpc = new Vpc(this, 'VPC');
const securityGroup = new SecurityGroup(this, 'SecurityGroup', {
  vpc,
});
const ec2Instance = new Instance(this, 'EC2Instance', {
  vpc,
  instanceType: new InstanceType('t2.small'),
  machineImage: ubuntuImage, // searched for elsewhere
  keyName: 'keynamehere',
  vpcSubnets: { subnets: [vpc.publicSubnets[0]] },
});
const eip = new CfnEIP(this, 'Server IP', {
  instanceId: ec2Instance.instanceId,
});

I've tried to use a CfnEIPAssociation instance of the 'instanceID' property within the CfnEIP but still have the same problem.我尝试在 CfnEIP 中使用“instanceID”属性的 CfnEIPAssociation 实例,但仍然遇到同样的问题。

Any suggestions?有什么建议么?

For me, I have to create the ec2 using the CfnInstance then use the ec2Instance.ref inside the CfnEIPAssociation .对我来说,我必须使用CfnInstance创建 ec2 然后在ec2Instance.ref中使用CfnEIPAssociation

    const instance = new ec2.CfnInstance(this, 'CccgEc2', {
      imageId: 'ami-07d0cf3af28718ef8',
      instanceType: 't2.micro',
      keyName: 'id_rsa',
      monitoring: false,
      securityGroupIds: [
        mySecurityGroup.securityGroupId
      ],
      subnetId: vpc.publicSubnets[0].subnetId,
      // iamInstanceProfile: 'ec2-role'
    });

    const eip = new ec2.CfnEIP(this, 'Server IP', {});

    new ec2.CfnEIPAssociation(this, 'ea', {
      eip: eip.ref,
      instanceId: instance.ref
    });

(Apologies, this is months after solving it) (抱歉,这是解决它几个月后)

I believe the issue was the vpcSubnets field, below is the working code I have today我相信问题是 vpcSubnets 字段,下面是我今天的工作代码

const ec2Instance = new Instance(this, "Instance", {
      vpc: supportStack.vpc,
      instanceType: new InstanceType("t2.nano"),
      machineImage: ubuntuImage,
      securityGroup: supportStack.securityGroup,
      vpcSubnets: { subnetType: SubnetType.PUBLIC }, // 
      userData,
    });
    supportStack.bucket.grantRead(ec2Instance);
    new CfnEIPAssociation(this, "ElasticIpAssociation", {
      eip: eip.ref,
      instanceId: ec2Instance.instanceId,
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS CDK 将 EC2 卷附加到 EC2 实例 - How to attach an EC2 volume to an EC2 instance using AWS CDK 无法将 EC2 实例附加到 AWS CDK 中的经典负载均衡器 - Unable to attach EC2 instance to a classic load balancer in AWS CDK 我可以使用什么 CDK 资源来创建 ec2 Image Builder ContainerRecipe? 我只看到一种通过 CDK 创建 ImageRecipe 的方法 - What CDK resource can I use to create an ec2 Image Builder ContainerRecipe? I only see a way to create an ImageRecipe via CDK 如何从 AWS CDK 开始在 ASG EC2 实例上运行命令 - How do you run commands on ASG EC2 instance start from AWS CDK Typescript CDK,如何将文件添加到 ec2 实例? - Typescript CDK, how to add files to ec2 instance? 使用 aws cdk 创建 elasticbeanstalk 实例 - Using aws cdk to create elasticbeanstalk instance AWS CDK ec2.Instance userData ....不持久:( - AWS CDK ec2.Instance userData.... not persisting :( 有没有办法在 CDK 上重启 RDS 和 EC2 实例? - Is there a way to restart RDS and EC2 instances on CDK? 使用 AWS CDK 创建数据库实例后如何运行 Fargate 任务? - How to run Fargate task after database instance is created with AWS CDK? 无法在 CDK 应用程序中为 AWS S3 Glacier 创建 CDK 堆栈? - Cannot create CDK stack for AWS S3 Glacier in CDK app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM