bash 如何从 AWS 中的实例获取实例名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18338585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:04:51  来源:igfitidea点击:

How to get the instance Name from the instance in AWS?

bashamazon-web-servicesamazon-ec2

提问by numb3rs1x

I'm trying to set up a means to register an instance in route53 automatically when the instance is created, using salt and this article: http://cantina.co/2012/01/25/automated-dns-for-aws-instances-using-route-53/

我正在尝试设置一种在创建实例时自动在 route53 中注册实例的方法,使用盐和这篇文章:http://cantina.co/2012/01/25/automated-dns-for-aws-实例使用路由 53/

The article uses ec2-metadatato get the instance-id and and the hostname. I'm wondering if there is a way, using bash within the instance, to get the instance Name instead. ec2-metadataonly seems to show the instance-id. Thanks in advance.

文章用于ec2-metadata获取实例 ID 和主机名。我想知道是否有办法在实例中使用 bash 来获取实例名称。ec2-metadata似乎只显示实例 ID。提前致谢。

回答by Steffen Opel

First and foremost, the Amazon EC2Instance Metadata Servicealso provides quite some other Namesbesides the instance-id, if these might be what you are looking for - see Instance Metadata Categories:

首先,在亚马逊EC2实例元数据服务也提供了相当长的一段其它名称之外的instance-id,如果这些可能是你在找什么-看实例元数据类别

  • hostname- The private hostname of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which the device number is 0).
  • local-hostname- The private DNS hostname of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which the device number is 0).
  • public-hostname- The instance's public DNS. If the instance is in a VPC, this category is only returned if the enableDnsHostnames attribute is set to true.
  • hostname-实例的私有主机名。在存在多个网络接口的情况下,这是指 eth0 设备(设备编号为 0 的设备)。
  • local-hostname-实例的私有 DNS 主机名。在存在多个网络接口的情况下,这是指 eth0 设备(设备编号为 0 的设备)。
  • public-hostname-实例的公共 DNS。如果实例在 VPC 中,则仅当 enableDnsHostnames 属性设置为 true 时才返回此类别。

If you are looking for the Nameas exposed in the AWS Management Consolethough, you would indeed need to resort to using one of the Tools for Amazon Web Servicesto retrieve it - that Nameis in fact just a regular tag with the key Name(see Tagging Your Amazon EC2 Resources), which happens to be used across most AWS services for the obvious purpose.

但是,如果您要查找在AWS 管理控制台中公开的名称,则确实需要使用Amazon Web Services 工具之一来检索它 - 该名称实际上只是一个带有键名称的常规标签(请参阅标记您的 Amazon EC2 资源),它恰好用于大多数 AWS 服务,用于明显的目的。

Here's how to get it with the AWS Command Line Interfacefor example (skipping region and credentials):

例如,以下是使用AWS 命令​​行界面获取它的方法(跳过区域和凭证):

aws ec2 describe-tags \
--filters Name=resource-id,Values=i-abcd1234 Name=key,Values=Name \
--query Tags[].Value --output text

回答by Jayesh Dhandha

First, you need to get the instance-id.

首先,您需要获取instance-id.

AWS_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`

Than you can get the ec2 instance nameusing below command.

比你可以ec2 instance name使用下面的命令。

EC2_NAME=$(aws ec2 describe-tags --region $REGION --filters "Name=resource-id,Values=$AWS_INSTANCE_ID" "Name=key,Values=Name" --output text | cut -f5)

Please ensure that you have AWS CliInstalled.

请确保您已安装AWS Cli

I hope this helps. Thanks!

我希望这有帮助。谢谢!

回答by BrianJakovich

Not sure what it would look like with bash, but you could use an SDK from the instance itself if you can get the instance id. You would query the ec2 recourse and pass in the ec2 instance id. Using the ruby sdk it would look like:

不确定使用 bash 会是什么样子,但如果您可以获得实例 ID,则可以使用实例本身的 SDK。您将查询 ec2 资源并传入 ec2 实例 ID。使用 ruby​​ sdk 它看起来像:

i = ec2.instances["i-12345678"]
puts i.dns_name

回答by odynplus

Found that describe-tags not working in my config, failed with 'UnauthorizedOperation' error. Got this working with describe-instances:

发现描述标签在我的配置中不起作用,因“UnauthorizedOperation”错误而失败。得到这个与描述实例一起工作:

aws ec2 describe-instances --filters Name=instance-id,Values=$(wget -qO- http://instance-data/latest/meta-data/instance-id) --query Reservations[].Instances[].Tags[].Value --output text

Command using region and access keys from current user's aws config file's [default] section: ~/.aws/config . If need to use another user's region/keys (can be found at AWS console's IAM dashboard), you can add them to another section in that file, for example [user2] and use in command like this:

使用区域和访问密钥的命令来自当前用户的 aws 配置文件的 [default] 部分: ~/.aws/config 。如果需要使用其他用户的区域/密钥(可以在 AWS 控制台的 IAM 仪表板中找到),您可以将它们添加到该文件的另一个部分,例如 [user2] 并在命令中使用,如下所示:

aws --profile user2 ec2 describe-instances --filters Name=instance-id,Values=$(wget -qO- http://instance-data/latest/meta-data/instance-id) --query Reservations[].Instances[].Tags[].Value --output text