Find EC2 region an instance resides in

Quite often you'll find yourself needing to know the region your instance resides in. I've seen this done a couple of ways:

$ curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/\([1-9]\).$/\1/g'

This is pretty ugly, so I prefer to use this method instead:

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "accountId" : "112233445566",
  "instanceId" : "i-12345678",
  "billingProducts" : null,
  "instanceType" : "t2.small",
  "imageId" : "ami-12345678",
  "kernelId" : null,
  "ramdiskId" : null,
  "pendingTime" : "2015-06-22T11:47:49Z",
  "architecture" : "x86_64",
  "region" : "eu-west-1",
  "version" : "2010-08-31",
  "availabilityZone" : "eu-west-1b",
  "privateIp" : "10.0.102.114",
  "devpayProductCodes" : null
}

Now you can use the excellent jq to grab the region:

$ curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region
eu-west-1