bash - EC2 instance region is not populated in user-data script -


i want fill tags of ec2 spot instance, impossible directly in spot request, via user data script. going fine when specify region statically, not universal approach. when try detect current region instance userdata, region variable empty. in following way:

#!/bin/bash region=$(ec2-metadata -z | awk '{print $2}' | sed 's/[a-z]$//') aws ec2 create-tags \     --region $region \     --resources `wget -q -o - http://169.254.169.254/latest/meta-data/instance-id` \     --tags key=sometag,value=somevalue key=sometag,value=somevalue 

i tried made delay before region populating

/bin/sleep 30 

but had no result.

however, when run script manually after start, tags added fine. going on?

why in aws-cli doesn't default region profile? have aws configure configured inside instance, without --region clause throws error region not specified.

i suspect ec2-metadata command not available when userdata script executed. try getting region metadata server directly (which ec2-metadata anyway)

region=$(curl -fsq http://169.254.169.254/latest/meta-data/placement/availability-zone |  sed 's/[a-z]$//') 

aws cli use region default profile.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -