Get instance id by metadata

0

I'm trying to get this [https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/instance-configuration/cron-leaderonly-linux.config] to work to avoid the cron competition between instances with Elastic Beanstalk. I noticed that on the instances that the instances created by eb does not print anything if I launch curl http://169.254.169.254/latest/meta-data/instance-id. The instances created from eb appear to have aws-elasticbeanstalk-ec2-role IAM role and this seems to be correct. As specified in the comments on the file it is necessary to add permissions to the default instance, I therefore added AmazonS3FullAccess, but this did not solve the problem. However, if I launch the curl http://169.254.169.254/latest/meta-data/instance-id command on an instance previously created directly on EC2 and not created by eb, without any IAM role, it correctly prints the id of the instance.

sonnets
asked 8 months ago396 views
2 Answers
1
Accepted Answer

Is it possible that the instance where this doesn't work is using IMDSv2? If so, see this other answer: https://repost.aws/questions/QUu_EQXMmAS8e3AA36LPeDyg/get-region-from-cli-in-cape-town#ANL7-jwi8ESge3axL-gTRHqA

profile pictureAWS
EXPERT
answered 7 months ago
profile picture
EXPERT
reviewed 3 days ago
  • Thanks, that was exactly the cause! I refactored the bash code adding the token, all ok now, no errors. Unfortunately it doesn't seem to solve the cron competition problem, I will eventually open another specific post.

  • I was doing the wrong way of testing auto scaling with elastic beanstalk. The bash script with the token change for IMDSv2 manages to check crons correctly with the primary instance. Thanks again to everyone for the help.

0

The value returned from IMDS doesn't have a "newline" character at the end, which can cause the shell prompt to merge with or over-print the result in your terminal. You can try adding an echo immediately after the curl command to print a newline and make the value show up properly:

curl http://169.254.169.254/latest/meta-data/instance-id; echo
profile pictureAWS
EXPERT
James_S
answered 8 months ago
  • Thanks James for the quick reply. Unfortunately it's not a newline problem. As I was saying the same string on an ec2 server without IAM roles prints the instance id correctly.

  • Thanks for confirming. Note that you don't need any IAM permissions to fetch this information from IMDS though, so I'd discount that line of enquiry :)

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions