首页 > 解决方案 > 使用 Cloudformation 脚本创建 ec2 时如何分配特定的私有 ip(基于我的 CIDR 和子网的预先确定的 ip)

问题描述

这是我使用的代码,仍然无法正常工作。我正在尝试使用 CloudFormation 模板创建即时,并使用特定的私有 IP 地址启动。请帮助我缺少什么:我会在哪里提到我需要将其分配给 CloudFormation 模板中的即时的私有 ip?

"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
    "KeyName" : { "Ref" : "KeyName" },
    "NetworkInterfaces": [ {
      "DeviceIndex": "0",
      "PrivateIpAddress" : String,
    } ]
  }
}```

标签: amazon-web-servicesamazon-ec2automationamazon-cloudformation

解决方案


PrivateIpAddress是您指定私有IP的地方。最好还指定SubnetId您的 IP 必须与您放置实例的子网的 CIDR 范围相匹配。

例子:

  InstanceWithFixedPirivateIp:
    Type: AWS::EC2::Instance
    Properties:                
      ImageId: ami-07ebfd5b3428b6f4d
      InstanceType: t2.micro        
      Monitoring: false            
      NetworkInterfaces:
        - DeviceIndex: 0
          PrivateIpAddress: 172.31.80.12
          SubnetId: subnet-a9e16487 # CIDR 172.31.80.0/20

推荐阅读