14
votes

In AWS Cloudformation, is there any way to declare an EC2 instance in VPC with a Public IP without the need to declare an Elastic IP and attach to it?

In AWS::AutoScaling::LaunchConfiguration, you could add a property "AssociatePublicIpAddress" to say the instances will accept a Public IP automatically. I am looking for the equivalent for AWS::EC2::Instance

Below is my cloudformation snippet for creating an EC2 instance. I can't any doc that mentions how to add a public IP without having to declare an Elastic IP beforehand.

"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
Public IP is assigned automatically when create ec2 instance. You needn't manually add it. Do you require to add send public IP?BMW
There's a setting in the subnet settings: "Auto-assign Public IP" have you tried that?Edwin
Should my answer be the correct one, can you mark it as such to help other to sort out valid answers ? thanksSébastien Stormacq

2 Answers

20
votes

Assuming you are starting your instance in a VPC public subnet (i.e. a subnet that has a routing table incl. a rule to send traffic to 0.0.0.0/0 to the Internet Gateway), just define AssociatePublicIpAddress property in the NetworkInterfaces group of your EC2 resource:

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

See documentation at http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-network-iface-embedded.html

If you are starting your instance in EC2 Classic networking (non VPC), it will receive a public IP address automatically.

6
votes

I see that this is an old post but i post the answer anyway it can be helpful. In the subnet you can set : "MapPublicIpOnLaunch" to True so all the instance of this subnet will have a public 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.