Find region from within an EC2 instance

前端 未结 28 1441
谎友^
谎友^ 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 07:08

    A method using only egrep, which should work on most any linux instance spun up without having to install any extra tooling. I tested this against a list of all current AWS regions and they all match.

    curl http://169.254.169.254/latest/meta-data/placement/availability-zone | egrep -o '(\w)+-(\w)+-[0-9]'

    Explanation of the REGEX:

    • "(\w)+" This matches any number of letters
    • "-" matches only a single dash
    • "[0-9]" matches any 1 number

    If you want this into a variable do:

    region=$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone | egrep -o '(\w)+-(\w)+-[0-9]')

提交回复
热议问题