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
전문가
검토됨 14일 전
  • 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 :)

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠