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.
If you're running on windows, you can use this powershell one-liner:
$region=(Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").region
ec2-metadata --availability-zone | sed 's/.$//'
For debian based systems, the command is without dash.
ec2metadata --availability-zone | sed 's/.$//'
If you're able to use the AWS Java SDK, there is now a method that will return the current region name (such as "us-east-1", "eu-west-1"):
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/Regions.html#getCurrentRegion()
2 liner that works as long as you are using ec2.internal as your search domain:
az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone)
region=${az:0:${#az} - 1}
For the sed and curl solution it looks like format has changed a bit. For me works
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | sed -n 's/ "region" : "\(.*\)"[,]/\1/p'
Use JQ:
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region