Find region from within an EC2 instance

前端 未结 28 1403
谎友^
谎友^ 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:52

    If you're running on windows, you can use this powershell one-liner:

    $region=(Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").region
    
    0 讨论(0)
  • 2020-12-02 06:53
    ec2-metadata --availability-zone | sed 's/.$//'
    

    For debian based systems, the command is without dash.

    ec2metadata --availability-zone | sed 's/.$//'
    
    0 讨论(0)
  • 2020-12-02 06:53

    If you're able to use the AWS Java SDK, there is now a method that will return the current region name (such as "us-east-1", "eu-west-1"):

    http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/Regions.html#getCurrentRegion()

    0 讨论(0)
  • 2020-12-02 06:53

    2 liner that works as long as you are using ec2.internal as your search domain:

    az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone)
    region=${az:0:${#az} - 1}
    
    0 讨论(0)
  • 2020-12-02 06:53

    For the sed and curl solution it looks like format has changed a bit. For me works

    curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | sed -n 's/ "region" : "\(.*\)"[,]/\1/p'
    
    0 讨论(0)
  • 2020-12-02 06:54

    Use JQ:

    curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region
    
    0 讨论(0)
提交回复
热议问题