1

Can a running EC2 instance determine what Availability Zone it is running in, without using the AWS API? Perhaps there is a way to have EC2 pass this information in an environment variable or similar?

I'm just hoping to build in a small amount of AZ awareness without adding a dependency on the AWS client library.

Doradus
  • 938
  • 1
  • 9
  • 15

1 Answers1

6

Yes you can, using instance metadata. For example,

az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
echo ${az}

Example output:

us-east-1e

Depending on your programing language, you can do same. For python, you could use for example requests library or native python libraries for calling urls.

Marcin
  • 215,873
  • 14
  • 235
  • 294