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 :)

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

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

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

関連するコンテンツ