从 EC2 实例内查找区域
有没有办法从实例内部查找实例的区域?
我正在寻找类似于 查找方法的方法实例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
该 URL (http://169.254.169.254/latest/dynamic/instance-identity/document< /a>) 似乎不再起作用。当我尝试使用它时,我得到了 404。我有以下代码似乎可以工作:
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:
还有一种方法可以实现这一目标:
There is one more way of achieving that:
对于基于 Debian 的系统,该命令不带破折号。
For debian based systems, the command is without dash.
AWS 托管一个元数据端点,其中包含有关您的 EC2 实例的大量有用信息。长期以来,您必须间接解析出区域信息,但现在 AWS 为区域信息提供了专用路由:
latest/meta-data/placement/region
。不过,他们还引入了一种新机制,用于使用 IMDSv2 对元数据端点进行身份验证。您首先需要从服务器获取元数据令牌,可以通过以下方式获取该令牌:
获得令牌后,只需将其与区域端点的请求一起传递即可:
有关更多信息,请查看 AWS 文档。
编辑(2021 年 12 月):可能还值得一提的是,此端点在 2019 年 10 月 1 日发布的元数据 API 中可用。在使用此版本之前,请检查
http://169.254.169.254/
,确保您的实例支持该版本或更高版本。编辑(2024 年 6 月):此答案曾经使用 IMDSv1,因为这是唯一的选择。它已更新以反映新的 IMDSv2 模式。最初的答案如下供后代使用。
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:
Once you have a token, just pass it along with your request for the region endpoint:
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.
如果您可以使用
jq
,您可以运行以下命令:我想这是最干净的方法。
If you are OK with using
jq
, you can run the following:I guess it's the cleanest way.
如果您想避免使用正则表达式,可以使用 Python 执行以下操作:
If you want to avoid regular expression, here's a one-liner you can do with Python:
迄今为止我发现的最简单的
Easiest I found so far
您可以使用 ec2 元数据:
You can use ec2-metadata:
非常简单的一行
very simple one liner
从可用区中获取区域,去掉最后一个字母。
Get the region from the availability zone, strip off the last letter of it.
2024 - 有两种类型的调用
IMDSv2(推荐)
您必须在获取元数据之前检索身份验证令牌。以下调用将设置一个环境变量供您重用。
使用身份验证令牌获取区域(也包含其他 API 调用)
IMDSv1
v1 中不需要令牌。仅当 v2 设置为可选时才有效(请参见下面的屏幕截图)。 如果您在仅限 v2 的环境中尝试 v1,您将看到空字符串响应。
我如何知道要使用哪个版本?
如果可以的话使用 v2。如果您有旧实例并且现在需要此数据,请使用 v1 和迁移路线图。
区域与可用区(us-east-1 与 us-east-1a)
文档: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.
Use the auth token to get the region (with other APIs calls too)
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.
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.
Region vs Availability Zone (us-east-1 vs us-east-1a)
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
如果您使用 json - 使用正确的工具。 jq 在这种情况下非常强大。
If you work with json - use right tools. jq much powerful in this case.
如果您能够使用 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()
这是我找到的最干净的解决方案:
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')
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')
感谢 https://unix.stackexchange.com/a/144330/135640,使用 bash 4.2+ 我们可以只需从可用区域中删除最后一个字符:
这假设 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:
This assumes AWS continues to use a single character for availability zones appended to the region.
只要您使用 ec2.internal 作为搜索域,2 班轮就可以工作:
2 liner that works as long as you are using ec2.internal as your search domain:
对于任何想要使用良好的 ol powershell 执行此操作的人
For anyone wanting to do this with good ol powershell
或者不要求 Ubuntu 或此工具,只需执行以下操作:
Or don't make Ubuntu or this tool a requirement and simply do:
这适用于 eu-central-1 以及各种字母区域。 (我没有足够的代表来回复上面的 sed 答案)
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)
如果您在 Windows 上运行,则可以使用此 powershell 单行代码:
If you're running on windows, you can use this powershell one-liner:
要查找有关您登录的 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,
如果您希望使用 JS 获取区域,这应该可行:
这是从 AWS DOCS 找到的映射,响应元数据 API 调用,只需修剪最后一个字符就可以了。
If you are looking to get region using JS, this should work :
This was the mapping found from AWS DOCS, in response to metadata API call, just trim the last character should work.
还在寻找从实例中查找区域的解决方案,这是我的纯 Bash 解决方案:
除非有些区域 AZ 具有两个以上字母,但我不知道。
Was also looking for a solution to find region from the instance and here is my pure Bash solution:
unless there are regions where AZ has more than two letters, which I'm not aware of.
如果您正在寻找更简单的方法,可以查看 /etc/resolv.conf 并找到类似“search us-west-2.compute.internal”的行。例如:
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:
所有这些都不再适用于 AMI Linux 2...我发现了这种离线(未记录)方法:
All this no longer works on AMI Linux 2... I found this offline (undocumented) approach:
如果您使用 IMDSv2,则首先需要令牌。
这是一个使用 bash 的示例,它也依赖于curl:
这会获取一个非常短暂的令牌并使用它来获取区域。
If you are using IMDSv2, you'll need the token first.
Here's an example using bash, which also depends on curl:
This gets a very short-lived token and uses it to get the region.
根据最新的IMDSv2,我们需要获取令牌并在检索元数据时使用令牌。
According to the latest IMDSv2, we need to get the token and use the token while retrieving the metadata.
更新 03-25-2024
对于使用 Amazon Linux 2023 实例类型的用户,该命令
将收到 401 错误。下面是我获取 AZ 信息的命令
Update 03-25-2024
For those who use Amazon Linux 2023 instance type, the command
will get 401 error. Below is my command to get AZ info
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.)一种仅使用egrep的方法,它应该适用于大多数启动的Linux实例,而无需安装任何额外的工具。我针对当前所有 AWS 区域的列表对此进行了测试,它们全部匹配。
卷曲 http://169.254.169.254/latest/meta-data/placement/availability-zone | egrep -o '(\w)+-(\w)+-[0-9]'
正则表达式说明:
如果您希望将其放入变量中,请执行以下操作:
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:
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]')