How to pass Private Ip of multiple EC2 to eachother in AWS Cloudformation.

0

Hi there, I have a use case where, I am initializing 3 EC2 instances in Cloudformation template. And in each of these EC2 instances, I need to use the PrivateIp of all 3 EC2 instances created in template. How can I achieve this?

Any help would be appreciated. Thanks in advance!

質問済み 9ヶ月前508ビュー
3回答
1

Hi,

To achieve what you want you must one Fn::GetAtt with attribute PrivateIP of EC2 instance. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html

So, let's say your 3 EC2 instances have logical name Instance1, Instance2 and Instance3.

To refer to the private ip of each from any location in your template, you need to code (YAML): !GetAtt InstanceN.PrivateIp (short form) or Fn::GetAtt: [ InstanceN, PrivateIp ]

Best.

Didier

profile pictureAWS
エキスパート
回答済み 9ヶ月前
  • Thanks for the response! Didier,

    I think, I was unable to put up my use case clearly. Actually I want to pass the private IP of all 3 ec2 in the user data section of each EC2 instance. For example: Under userdata of 1st EC2, I want to pass IP of Instance1,2,and 3.

  • You can still do still by obtaining InstanceN.PrivateIP as a CFN pseudo variable that you inject in the UserData of the instance via !Sub to substitute the corresponding CFN variable with its effective value. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html

    "If you specify template parameter names or resource logical IDs, such as ${InstanceTypeParameter}, CloudFormation returns the same values as if you used the Ref intrinsic function. If you specify resource attributes, such as ${MyInstance.PublicIp}, CloudFormation returns the same values as if you used the Fn::GetAtt intrinsic function."

    With this addition, you will achieve what you want in the UserData of your instances

0

If it were me, this is what I would do:

In the CloudFormation template, add the instance private IP addresses to the Outputs section:

Outputs:
  InstanceAPrivateIP:
    Value: !GetAtt InstanceA.PrivateIp
  ...etc...

Next, when you launch the containers you can query the outputs of the stack by using the CloudFormation DescribeStacks API. You don't say what language you're using but from the CLI you could do this:

InstanceAIP=$(aws cloudformation describe-stacks --stack-name STACKNAMEHERE --query "Stacks[0].Outputs[?OutputKey=='InstanceAPrivateIP'].OutputValue" --output text)
  ...etc...
docker run -e 1st_EC2_IP=${InstaceAIP} -e 2nd_EC2_IP=${InstanceBIP} -e 3rd_EC2_IP=${InstanceCIP}

Note that you can query the stack output from within the container as well if that works for you. The requirement here is that you know the stack name when doing the query.

Alternately, you could store the IP addresses in ParameterStore and then retrieve them at runtime.

profile pictureAWS
エキスパート
回答済み 9ヶ月前
0

Hi.

If you need the private IP address in the user data script there is no need to pass it via CloudFormation. Instead use the Instance metadata endpoint to retrieve it.

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
IP = `curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/local-ipv4`

You can read more here and here

profile picture
エキスパート
回答済み 9ヶ月前
  • Thanks for the response jimmy! I actually want the private Ip of all 3 ec2 in cloud formation template and need to pass them in the docker run command. Like this:- "docker run -e 1st_EC2_IP=<ip> -e 2nd_EC2_IP=<ip> -e 3rd_EC2_IP=<ip>" And same command I need to run in all 3 ec2 instances. Please help me here. I am stuck here for quite long now.

    Thanks in advance!

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ