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
已提问 8 个月前413 查看次数
2 回答
1
已接受的回答

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
专家
已回答 8 个月前
profile picture
专家
已审核 15 天前
  • 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
专家
James_S
已回答 8 个月前
  • 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 :)

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则