Find region from within an EC2 instance
There is one more way of achieving that:
REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
echo $REGION
us-east-1
If you are OK with using jq
, you can run the following:
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r
I guess it's the cleanest way.
That URL (http://169.254.169.254/latest/dynamic/instance-identity/document) doesn't appear to work anymore. I get a 404 when I tried to use it. I have the following code which seems to work though:
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"