1

I have a CloudFormation template with LaunchConfig. In UserData I need to reference public IP of the instance being created. I wanted to use Fn::GetAtt, but it requires logical name and I'm not sure how I can define it when using LaunchConfig.

"LaunchConfig": {
  "Type" : "AWS::AutoScaling::LaunchConfiguration",
  "Metadata" : {"..."},
  "Properties": {
    "ImageId" : "...",
    "InstanceType" : { "Ref" : "InstanceType" },
    "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
    "KeyName" : { "Ref" : "KeyName" },
    "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
      "..."
    ]]}}
  }
},
4
  • did you solve it?
    – Aliya
    Sep 25, 2017 at 17:20
  • Yes. As accepted answer explains, the way to do it is through instance metadata service
    – arnaslu
    Sep 27, 2017 at 6:39
  • @arnaslu, did you have to reference the IP within the same resource, or in a different resource ? If in a difference resource, how did you "export" the value of the IP? Thanks
    – TheRock3t
    Nov 14, 2017 at 9:34
  • @TheRock3t I wanted to reference public IP in "UserData" script, so just internally. I think CloudFormation supports exporting PublicIP using "Fn::GetAtt", this should be fairly straightforward when you provision using "AWS::EC2::Instance", but not sure if it's possible using "AWS::AutoScaling::LaunchConfiguration". Keep in mind, that LaunchConfiguration can create multiple EC2 instances.
    – arnaslu
    Nov 15, 2017 at 12:39

2 Answers 2

5

If your UserData contains a shell script and all you need to do is reference the instance's own public IP within that script, you can get this value by querying the public-ipv4 category of the instance metadata with curl or any other http utility:

curl http://169.254.169.254/latest/meta-data/public-ipv4
2

It's not possible to directly retrieve the Public IP address of an EC2 instance generated by an Auto Scaling group. The reason being that the EC2 instance is not managed by the CloudFormation template. The template has no knowledge of the EC2 instance.

There are a couple of workarounds though.

Option 1: Allocate and assign an Elastic IP address.

Rather than detecting the auto-assigned IP address, allocate an Elastic IP address and assign it into the User Data of your EC2 instance and have the EC2 instance assign it to itself via it's startup script.

It's next-to-simple to do if your Auto Scaling group is only creating a single EC2 instance. It can still be done with 2 or more EC2 instances, but you'll have to create a pool of Elastic IP addresses to pull from.

Option 2: Use a WaitCondition with custom data that includes your EC2 instance's IP address.

Create a WaitCondition that your CloudFormation template waits on. In your EC2 instance, have it retrieve it's own public IP address, then signal the WaitCondition when it's ready. Have it include the IP address inside the signal's "Data" field.

References: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-waitcondition.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .