Doradus
Doradus

Reputation: 1036

Determine availability zone programmatically from EC2 instance

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.

Upvotes: 1

Views: 1265

Answers (1)

Marcin
Marcin

Reputation: 238249

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.

Upvotes: 6

Related Questions