Is there a way to look up the region of an instance from within the instance?
I\'m looking for something similar to the method of finding the instance id.
Or don't make Ubuntu or this tool a requirement and simply do:
: "${EBS_VOLUME_AVAILABILITY_ZONE:=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)}"
: ${EBS_VOLUME_REGION:="${EBS_VOLUME_AVAILABILITY_ZONE%%*([![:digit:]])}"}
If you work with json - use right tools. jq much powerful in this case.
# curl -s curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region'
eu-west-1
For anyone wanting to do this with good ol powershell
$var = (curl http://169.254.169.254/latest/dynamic/instance-identity/document | Select-String-Pattern "Zone" | ConvertFrom-Json | Select-Object -ExpandProperty "region")
echo $var
For finding out information about the EC2 you are logged into, you can use the ec2-metadata tool.
You can install the tool by following this link. After installing the tool, you can run
# ec2-metadata -z
to find out the region.
This tools comes installed with the latest (10.10) Ubuntu AMIs,
A method using only egrep, which should work on most any linux instance spun up without having to install any extra tooling. I tested this against a list of all current AWS regions and they all match.
curl http://169.254.169.254/latest/meta-data/placement/availability-zone | egrep -o '(\w)+-(\w)+-[0-9]'
region=$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone | egrep -o '(\w)+-(\w)+-[0-9]')
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