从 EC2 实例内查找区域

发布于 2024-10-03 11:14:14 字数 165 浏览 3 评论 0原文

有没有办法从实例内部查找实例的区域?

我正在寻找类似于 查找方法的方法实例 ID

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

牛↙奶布丁 2024-10-10 11:14:14

该 URL (http://169.254.169.254/latest/dynamic/instance-identity/document< /a>) 似乎不再起作用。当我尝试使用它时,我得到了 404。我有以下代码似乎可以工作:

EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"

That URL (http://169.254.169.254/latest/dynamic/instance-identity/document) doesn't appear to work anymore. I get a 404 when I tried to use it. I have the following code which seems to work though:

EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"
别靠近我心 2024-10-10 11:14:14

还有一种方法可以实现这一目标:

REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`

echo $REGION

us-east-1

There is one more way of achieving that:

REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`

echo $REGION

us-east-1
疯狂的代价 2024-10-10 11:14:14
ec2-metadata --availability-zone | sed 's/.$//'

对于基于 Debian 的系统,该命令不带破折号。

ec2metadata --availability-zone | sed 's/.$//'
ec2-metadata --availability-zone | sed 's/.$//'

For debian based systems, the command is without dash.

ec2metadata --availability-zone | sed 's/.$//'
始终不够爱げ你 2024-10-10 11:14:14

AWS 托管一个元数据端点,其中包含有关您的 EC2 实例的大量有用信息。长期以来,您必须间接解析出区域信息,但现在 AWS 为区域信息提供了专用路由:latest/meta-data/placement/region

不过,他们还引入了一种新机制,用于使用 IMDSv2 对元数据端点进行身份验证。您首先需要从服务器获取元数据令牌,可以通过以下方式获取该令牌:

TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 3600")

获得令牌后,只需将其与区域端点的请求一起传递即可:

curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region

有关更多信息,请查看 AWS 文档

编辑(2021 年 12 月):可能还值得一提的是,此端点在 2019 年 10 月 1 日发布的元数据 API 中可用。在使用此版本之前,请检查 http://169.254.169.254/,确保您的实例支持该版本或更高版本。

编辑(2024 年 6 月):此答案曾经使用 IMDSv1,因为这是唯一的选择。它已更新以反映新的 IMDSv2 模式。最初的答案如下供后代使用。


自从大部分答案发布以来,AWS 做了合理的事情并实施了一条新路径:latest/meta-data/placement/region

这意味着获取区域应该像这样简单

curl http://169.254.169.254/latest/meta-data/placement/region

AWS hosts a metadata endpoint that has tons of useful information regarding your EC2 instance. For a long time, you'd have to parse out the region information indirectly, but now AWS has a dedicated route for region information: latest/meta-data/placement/region.

However, they've also introduced a new mechanism to authenticate to the metadata endpoint with IMDSv2. You first need a metadata token from the server, which you can get with the following:

TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 3600")

Once you have a token, just pass it along with your request for the region endpoint:

curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region

For more information, check out AWS's docs.

EDIT (Dec 2021): It's also probably worth mentioning that this endpoint was made available in the 2019-10-01 release of the metadata API. Make sure your instance supports that version or later before using this by checking http://169.254.169.254/.

EDIT (Jun 2024): This answer used to be using IMDSv1, as that was the only option. It's been updated to reflect the new IMDSv2 pattern. The original answer follows for posterity.


At some point since most of these answers have been posted, AWS did the reasonable thing and implemented a new path: latest/meta-data/placement/region.

This means getting the region should be as simple as

curl http://169.254.169.254/latest/meta-data/placement/region
梦亿 2024-10-10 11:14:14

如果您可以使用jq,您可以运行以下命令:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r

我想这是最干净的方法。

If you are OK with using jq, you can run the following:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r

I guess it's the cleanest way.

幽蝶幻影 2024-10-10 11:14:14

如果您想避免使用正则表达式,可以使用 Python 执行以下操作:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | python -c "import json,sys; print json.loads(sys.stdin.read())['region']"

If you want to avoid regular expression, here's a one-liner you can do with Python:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | python -c "import json,sys; print json.loads(sys.stdin.read())['region']"
_蜘蛛 2024-10-10 11:14:14

迄今为止我发现的最简单的

 curl -s 169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//'

Easiest I found so far

 curl -s 169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//'
单调的奢华 2024-10-10 11:14:14

您可以使用 ec2 元数据:

ec2-metadata -z | grep -Po "(us|sa|eu|ap)-(north|south|central)?(east|west)?-[0-9]+"

You can use ec2-metadata:

ec2-metadata -z | grep -Po "(us|sa|eu|ap)-(north|south|central)?(east|west)?-[0-9]+"
妄想挽回 2024-10-10 11:14:14

非常简单的一行

export AVAILABILITY_ZONE=`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone`
export REGION_ID=${AVAILABILITY_ZONE:0:${#AVAILABILITY_ZONE} - 1}

very simple one liner

export AVAILABILITY_ZONE=`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone`
export REGION_ID=${AVAILABILITY_ZONE:0:${#AVAILABILITY_ZONE} - 1}
圈圈圆圆圈圈 2024-10-10 11:14:14

从可用区中获取区域,去掉最后一个字母。

ec2-metadata -z | awk '{print $2}' | sed 's/[a-z]$//'

Get the region from the availability zone, strip off the last letter of it.

ec2-metadata -z | awk '{print $2}' | sed 's/[a-z]$//'
沒落の蓅哖 2024-10-10 11:14:14

2024 - 有两种类型的调用

IMDSv2(推荐)

您必须在获取元数据之前检索身份验证令牌。以下调用将设置一个环境变量供您重用。

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

使用身份验证令牌获取区域(也包含其他 API 调用)

curl http://169.254.169.254/latest/meta-data/placement/region -H "X-aws-ec2-metadata-token: $TOKEN"

IMDSv1

v1 中不需要令牌。仅当 v2 设置为可选时才有效(请参见下面的屏幕截图)。 如果您在仅限 v2 的环境中尝试 v1,您将看到空字符串响应。

curl http://169.254.169.254/latest/meta-data/placement/region

我如何知道要使用哪个版本?

如果可以的话使用 v2。如果您有旧实例并且现在需要此数据,请使用 v1 和迁移路线图。

v1

区域与可用区(us-east-1 与 us-east-1a)

  • 区域 - 实例的区域位置。返回与 AWS 区域下拉列表中相同的区域数据。示例:us-east-2、ap-southeast-1。
  • 可用区 - 返回区域内的特定区域,由区域末尾的字符表示。这将镜像映射到实例的子网。使用可用区 API 调用而不是区域 API 调用来获取此数据。示例:us-east-2a、us-east-2c、ap-southeast-1b

文档:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html

169.254.169.254 是什么?

这是一个特殊用途的地址。它仅在 EC2 实例内工作。

详细答案:https://serverfault.com/questions/427018 /这是什么-ip-address-169-254-169-254

2024 - There are two types of calls

IMDSv2 (Recommended)

You must retrieve an auth token before getting meta data. The following call will set an environment variable for you to reuse.

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

Use the auth token to get the region (with other APIs calls too)

curl http://169.254.169.254/latest/meta-data/placement/region -H "X-aws-ec2-metadata-token: $TOKEN"

IMDSv1

No token is required in v1. Only works if v2 is set to optional (see screenshot below). You will see an empty string response if you try v1 in a v2-only environment.

curl http://169.254.169.254/latest/meta-data/placement/region

How do I know which version to use?

Use v2 if you can. If you have legacy instances and need this data now, use v1 with a roadmap to migration.

v1

Region vs Availability Zone (us-east-1 vs us-east-1a)

  • Region - Regional location of the instance. Returns the same region data as on AWS's region dropdown. Examples: us-east-2, ap-southeast-1.
  • Availability Zone - Returns the specific zone within a region, denoted by the character at the end of a region. This will mirror the subnet mapped to the instance. Use the availability zone API call instead of the region API call to get this data. Examples: us-east-2a, us-east-2c, ap-southeast-1b

Docs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html

What is 169.254.169.254?

It's a special-use address. It only works within the EC2 instance.

Detailed answer: https://serverfault.com/questions/427018/what-is-this-ip-address-169-254-169-254

写给空气的情书 2024-10-10 11:14:14

如果您使用 json - 使用正确的工具。 jq 在这种情况下非常强大。

# curl -s curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region'
eu-west-1

If you work with json - use right tools. jq much powerful in this case.

# curl -s curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region'
eu-west-1
神仙妹妹 2024-10-10 11:14:14

如果您能够使用 AWS Java SDK,现在有一个方法可以返回当前区域名称(例如“us-east-1”、“eu-west-1”):

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

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()

烟燃烟灭 2024-10-10 11:14:14

这是我找到的最干净的解决方案:

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

例如,

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

  • 不进行API调用,使用EC2实例元数据
  • 仅使用curl和基本的sed,因此不依赖于SDK或工具可能会被安装。
  • 不会尝试解析可用区名称,因此无需担心 AWS 更改可用区/区域名称格式

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
冷心人i 2024-10-10 11:14:14

感谢 https://unix.stackexchange.com/a/144330/135640,使用 bash 4.2+ 我们可以只需从可用区域中删除最后一个字符:

$ region=`curl -s 169.254.169.254/latest/meta-data/placement/availability-zone`
$ region=${region::-1}
$ echo $region
us-east-1

这假设 AWS 继续对附加到该区域的可用区域使用单个字符。

Thanks to https://unix.stackexchange.com/a/144330/135640, with bash 4.2+ we can just strip the last char from the availability zone:

$ region=`curl -s 169.254.169.254/latest/meta-data/placement/availability-zone`
$ region=${region::-1}
$ echo $region
us-east-1

This assumes AWS continues to use a single character for availability zones appended to the region.

顾忌 2024-10-10 11:14:14

只要您使用 ec2.internal 作为搜索域,2 班轮就可以工作:

az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone)
region=${az:0:${#az} - 1}

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}
眼睛会笑 2024-10-10 11:14:14

对于任何想要使用良好的 ol powershell 执行此操作的人

$var = (curl http://169.254.169.254/latest/dynamic/instance-identity/document | Select-String-Pattern "Zone" | ConvertFrom-Json | Select-Object -ExpandProperty "region")
echo $var

For anyone wanting to do this with good ol powershell

$var = (curl http://169.254.169.254/latest/dynamic/instance-identity/document | Select-String-Pattern "Zone" | ConvertFrom-Json | Select-Object -ExpandProperty "region")
echo $var
素衣风尘叹 2024-10-10 11:14:14

或者不要求 Ubuntu 或此工具,只需执行以下操作:

: "${EBS_VOLUME_AVAILABILITY_ZONE:=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)}"
: ${EBS_VOLUME_REGION:="${EBS_VOLUME_AVAILABILITY_ZONE%%*([![:digit:]])}"}

Or don't make Ubuntu or this tool a requirement and simply do:

: "${EBS_VOLUME_AVAILABILITY_ZONE:=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)}"
: ${EBS_VOLUME_REGION:="${EBS_VOLUME_AVAILABILITY_ZONE%%*([![:digit:]])}"}
被你宠の有点坏 2024-10-10 11:14:14

这适用于 eu-central-1 以及各种字母区域。 (我没有足够的代表来回复上面的 sed 答案)

ec2-metadata --availability-zone | sed 's/[a-z]$//'

This works for eu-central-1 as well as the various letter zones. (I don't have enough rep to reply to the sed answer above)

ec2-metadata --availability-zone | sed 's/[a-z]$//'
花开半夏魅人心 2024-10-10 11:14:14

如果您在 Windows 上运行,则可以使用此 powershell 单行代码:

$region=(Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").region

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
远山浅 2024-10-10 11:14:14

要查找有关您登录的 EC2 的信息,您可以使用
ec2 元数据工具。

您可以通过此链接安装该工具。
安装该工具后,您可以运行

#ec2-metadata -z

来查找区域。

该工具随最新 (10.10) Ubuntu AMI 一起安装,

For finding out information about the EC2 you are logged into, you can use the
ec2-metadata tool.

You can install the tool by following this link.
After installing the tool, you can run

# ec2-metadata -z

to find out the region.

This tools comes installed with the latest (10.10) Ubuntu AMIs,

请别遗忘我 2024-10-10 11:14:14

如果您希望使用 JS 获取区域,这应该可行:

meta.request("/latest/meta-data/placement/availability-zone",function(err,data){
        if(err)
                console.log(err);
        else{
                console.log(data);
                str = data.substring(0, data.length - 1);
                AWS.config.update({region:str});
                ec2 = new AWS.EC2();
            }
     });

这是从 AWS DOCS 找到的映射,响应元数据 API 调用,只需修剪最后一个字符就可以了。

  eu-west-1a :eu-west-1
  eu-west-1b :eu-west-1
  eu-west-1c :eu-west-1
  us-east-1a :us-east-1
  us-east-1b :us-east-1
  us-east-1c :us-east-1
  us-east-1d :us-east-1
  ap-northeast-1a :ap-northeast-1
  ap-northeast-1b :ap-northeast-1
  us-west-1a :us-west-1
  us-west-1b :us-west-1
  us-west-1c :us-west-1
  ap-southeast-1a :ap-southeast-1
  ap-southeast-1b :ap-southeast-1

If you are looking to get region using JS, this should work :

meta.request("/latest/meta-data/placement/availability-zone",function(err,data){
        if(err)
                console.log(err);
        else{
                console.log(data);
                str = data.substring(0, data.length - 1);
                AWS.config.update({region:str});
                ec2 = new AWS.EC2();
            }
     });

This was the mapping found from AWS DOCS, in response to metadata API call, just trim the last character should work.

  eu-west-1a :eu-west-1
  eu-west-1b :eu-west-1
  eu-west-1c :eu-west-1
  us-east-1a :us-east-1
  us-east-1b :us-east-1
  us-east-1c :us-east-1
  us-east-1d :us-east-1
  ap-northeast-1a :ap-northeast-1
  ap-northeast-1b :ap-northeast-1
  us-west-1a :us-west-1
  us-west-1b :us-west-1
  us-west-1c :us-west-1
  ap-southeast-1a :ap-southeast-1
  ap-southeast-1b :ap-southeast-1
爱人如己 2024-10-10 11:14:14

还在寻找从实例中查找区域的解决方案,这是我的纯 Bash 解决方案:

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

除非有些区域 AZ 具有两个以上字母,但我不知道。

Was also looking for a solution to find region from the instance and here is my pure Bash solution:

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

unless there are regions where AZ has more than two letters, which I'm not aware of.

荒人说梦 2024-10-10 11:14:14

如果您正在寻找更简单的方法,可以查看 /etc/resolv.conf 并找到类似“search us-west-2.compute.internal”的行。例如:

$ grep "^search" /etc/resolv.conf | sed "s:.* ::; s:\..*::"
us-west-2

If you are looking for a simpler way to do it, you can look at /etc/resolv.conf and find a line like "search us-west-2.compute.internal". For example:

$ grep "^search" /etc/resolv.conf | sed "s:.* ::; s:\..*::"
us-west-2
游魂 2024-10-10 11:14:14

所有这些都不再适用于 AMI Linux 2...我发现了这种离线(未记录)方法:

REGION=`cat /opt/elasticbeanstalk/config/ebenvinfo/region`
echo $REGION

# output example:
us-east-1

All this no longer works on AMI Linux 2... I found this offline (undocumented) approach:

REGION=`cat /opt/elasticbeanstalk/config/ebenvinfo/region`
echo $REGION

# output example:
us-east-1
半窗疏影 2024-10-10 11:14:14

如果您使用 IMDSv2,则首先需要令牌。

这是一个使用 bash 的示例,它也依赖于curl:

function get-aws-region() {
  imdsv2_token="$(
    curl -s -X PUT "http://169.254.169.254/latest/api/token" \
            -H "X-aws-ec2-metadata-token-ttl-seconds: 1"
  )"
  curl -s http://169.254.169.254/latest/meta-data/placement/region \
         -H "X-aws-ec2-metadata-token: $imdsv2_token"
}

这会获取一个非常短暂的令牌并使用它来获取区域。

If you are using IMDSv2, you'll need the token first.

Here's an example using bash, which also depends on curl:

function get-aws-region() {
  imdsv2_token="$(
    curl -s -X PUT "http://169.254.169.254/latest/api/token" \
            -H "X-aws-ec2-metadata-token-ttl-seconds: 1"
  )"
  curl -s http://169.254.169.254/latest/meta-data/placement/region \
         -H "X-aws-ec2-metadata-token: $imdsv2_token"
}

This gets a very short-lived token and uses it to get the region.

请帮我爱他 2024-10-10 11:14:14

根据最新的IMDSv2,我们需要获取令牌并在检索元数据时使用令牌。

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
EC2_AVAIL_ZONE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)

According to the latest IMDSv2, we need to get the token and use the token while retrieving the metadata.

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
EC2_AVAIL_ZONE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)
梦晓ヶ微光ヅ倾城 2024-10-10 11:14:14

更新 03-25-2024

对于使用 Amazon Linux 2023 实例类型的用户,该命令

curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone

将收到 401 错误。下面是我获取 AZ 信息的命令

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone

Update 03-25-2024

For those who use Amazon Linux 2023 instance type, the command

curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone

will get 401 error. Below is my command to get AZ info

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone
旧人哭 2024-10-10 11:14:14

ec2metadata(无破折号)是当前命令,用于为您提供有关 ec2 盒子的所有 aws 托管信息。这是最优雅、最安​​全的方法。 (ec2-metadata 是旧的、不再有效的命令。)

ec2metadata (no dash) is the current command to provide you all the aws hosting info about your ec2 box. this is the most elegant and secure approach. (ec2-metadata is the old, no longer valid command.)

饮惑 2024-10-10 11:14:14

一种仅使用egrep的方法,它应该适用于大多数启动的Linux实例,而无需安装任何额外的工具。我针对当前所有 AWS 区域的列表对此进行了测试,它们全部匹配。

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

正则表达式说明:

  • “(\w)+” 匹配任意数量的字母
  • “-” 只匹配单破折号
  • “[0-9]”匹配任意 1 个数字

如果您希望将其放入变量中,请执行以下操作:

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

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]')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文