Find region from within an EC2 instance

前端 未结 28 1440
谎友^
谎友^ 2020-12-02 06:24

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.

28条回答
  •  伪装坚强ぢ
    2020-12-02 06:59

    This is the cleanest solution I found:

    curl -s http://169.254.169.254/latest/dynamic/instance-identity/document |sed -n 's/  "region" : "\(.*\)"/\1/p'

    E.g.,

    export REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document |sed -n 's/  "region" : "\(.*\)"/\1/p')

    • Doesn't make an API call, uses EC2 instance meta-data
    • Only uses curl, and basic sed, so no dependencies on SDKs or tools not likely to be installed.
    • Doesn't attempt to parse the Availability Zone name, so no worries if AWS changes AZ/Region name format

提交回复
热议问题