Tuesday, October 26, 2021

[SOLVED] Find My Instance Region in AWS

Issue

Given these parameters:

  1. An Ubuntu instance running anywhere (any region and any availability zone in that region)
  2. Only the AWS PHP SDK2 installed on the instance (no other EC2 Command line tools, etc.)
  3. CURL and WGET available

What is the most elegant way for an instance script to explicitly determine what Region it is running in?

* **Instance ID:** wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
* **Availability Zone:** wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone

The meta-data does not currently provide this information.

We are aware that currently, the trailing character can be stripped off the availability zone in order to naively determine the Region, however, there is no guarantee that Amazon will not change that in the future.

The ultimate goal is that our cron jobs and other custom services can figure out who and where they are, so that they can interact with other services and instances appropriately.


Solution

This should work for you:

REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`

echo $REGION

It's from this thread:

Find region from within an EC2 instance



Answered By - jszobody