Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an Ec2 Instance with a Public IP automatically **without** declaring an Elastic IP in cloudformation?

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"
            },
        ]
    }
},
like image 845
MechaStorm Avatar asked Jan 04 '15 18:01

MechaStorm


People also ask

How do I assign a public IP to EC2 instance CloudFormation?

Public IP is assigned automatically when create ec2 instance. You needn't manually add it.

How do I create an EC2 instance with elastic IP?

Click the Elastic IPs link in the EC2 Dashboard. Click Allocate New Address and choose VPC or EC2 from the drop-down list, depending whether you're going to associate this IP with an instance in Amazon EC2-Virtual Private Cloud (VPC) or Amazon EC2-Classic, respectively. Click Yes, Allocate to confirm your choice.


2 Answers

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.

like image 145
Sébastien Stormacq Avatar answered Oct 17 '22 14:10

Sébastien Stormacq


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.
like image 6
K. Mounir Avatar answered Oct 17 '22 14:10

K. Mounir