如何自动创建具有公共IP的Ec2实例**而不在**中声明弹性IP?

时间:2015-01-04 18:42:24

标签: amazon-web-services amazon-cloudformation

在AWS Cloudformation中,有没有办法在VPC中使用公共IP 声明EC2实例,而不需要声明弹性IP并附加到它?

在AWS :: AutoScaling :: LaunchConfiguration中,您可以添加属性" AssociatePublicIpAddress"说实例将自动接受公共IP。我正在寻找AWS :: EC2 :: Instance

的等价物

以下是我用于创建EC2实例的cloudformation代码段。我无法提及如何添加公共IP,而无需预先声明弹性IP。

"MyEc2Instance": {
    "Type": "AWS::EC2::Instance",
    "Properties": {
        "IamInstanceProfile": {
            "Ref": "MyEc2InstanceProfile"
        },
        "ImageId": {
            "Fn::FindInMap": [
                "MyEc2Box",
                {
                    "Ref": "Region"
                },
                "ImageId"
            ]
        },
        "InstanceType": {
            "Fn::FindInMap": [
                "MyEc2Box",
                {
                    "Ref": "Region"
                },
                "InstanceType"
            ]
        },
        "KeyName": {
            "Ref": "DefaultKeyPair"
        },
        "Monitoring": "true",
        "SecurityGroupIds": [
            {
                "Ref": "MyEc2SecurityGroup"
            }
        ],
        "SubnetId": {
            "Ref": "MyBoxSubnet"
        },
        "Tags": [
            {
                "Key": "Name",
                "Value": "MyBox"
            },
        ]
    }
},

2 个答案:

答案 0 :(得分:15)

假设您在VPC公有子网中启动实例(即具有路由表的子网,包括将流量发送到0.0.0.0/0到Internet网关的规则),只需在NetworkInterfaces组中定义AssociatePublicIpAddress属性您的EC2资源:

            "NetworkInterfaces" : [{
                 "AssociatePublicIpAddress" : "True",
                 "DeleteOnTermination" : "True",
                 "SubnetId" : { "Ref" : "PublicSubnet" },
                 "DeviceIndex" : "0",
                 "GroupSet" : [ { "Ref" : "SecurityGroup" } ]
            }],

请参阅http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-network-iface-embedded.html

上的文档

如果您在EC2 Classic网络(非VPC)中启动实例,它将自动收到公共IP地址。

答案 1 :(得分:4)

我看到这是一个老帖子,但我发布答案无论如何它可能会有所帮助。 在子网中,您可以设置:" MapPublicIpOnLaunch"为True,因此该子网的所有实例都将具有公共IP。

MapPublicIpOnLaunch

Indicates whether instances that are launched in this subnet receive a public IP address. By default, the value is false.

Required: No

Type: Boolean

Update requires: No interruption.