如何使用AWS CLI获取最近启动的EC2实例? [英] How to get the most recently launched EC2 instance with AWS CLI?

查看:139
本文介绍了如何使用AWS CLI获取最近启动的EC2实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用以下CLI命令来获取给定实例 Name 标签'myInstanceName'的实例 PublicIPAddress LaunchTime

I'm currently using the following CLI command to get the instance PublicIPAddress and LaunchTime for a given instance Name tag, 'myInstanceName':

aws ec2 describe-instances --filters 'Name=tag:Name,Values=myInstanceName' \ 
    --region us-east-1 \
    --query 'Reservations[*].Instances[*].{PublicIpAddress: PublicIpAddress, LaunchTime: LaunchTime}'

结果如下:

[
    {
        "LaunchTime": "2019-01-25T11:49:06.000Z",
        "PublicIpAddress": "11.111.111.11"
    }
]

这很好,但是如果有两个同名实例,我将在结果JSON中得到两个结果。我需要找到一种获取给定名称的最新实例的方法。

This is fine, but if there are two instances with the same name I will get two results in my result JSON. I need to find a way to get the most recent instance for a given name.

解决方案更新

此问题非常特定于EC2实例。可以使用两种不同的方法来解决该问题,请在下面回答:

使用jq解析结果

使用JMESPath

This question is quite specific to EC2 instances. The issue can be resolved using two different methods, answered below:
Parsing Result with jq
Using JMESPath

请参见<一个href = https://stackoverflow.com/questions/42414156/sort-by-date-with-jmespath>相关问题,以便使用JMESPath按日期进行更常规的排序,并进行进一步的阅读。

Please see this related question for more general sorting by date with JMESPath, and for further reading.

推荐答案

尝试使用 jq 实用程序。这是一个命令行JSON解析器。如果您不熟悉它,建议您使用 jq游乐场进行试验。

Try using the jq utility. It's a command-line JSON parser. If you're not familiar with it then I'd recommend the jq playground for experimentation.

首先展平awcli结果,如下:

First flatten the awcli results, as follows:

aws ec2 describe-instances  \
    --query 'Reservations[].Instances[].{ip: PublicIpAddress, tm: LaunchTime}' \
    --filters 'Name=tag:Name,Values= myInstanceName'

请注意,我已经将 LaunchTime 别名为 tm 为简洁起见。这样将导致(未排序的)输出,如下所示:

Note that I've aliased LaunchTime to tm for brevity. That will result in (unsorted) output like this:

[
  {
    "ip": "54.4.5.6",
    "tm": "2019-01-04T19:54:11.000Z"
  },
  {
    "ip": "52.1.2.3",
    "tm": "2019-03-04T20:04:00.000Z"
  }
]

接下来,将此结果通过管道传递到 jq 并按 tm 降序进行排序 LaunchTime ),如下所示:

Next, pipe this result into jq and sort by descending tm (the alias for LaunchTime), as follows:

jq 'sort_by(.tm) | reverse'

这将导致如下输出:

[
  {
    "ip": "52.1.2.3",
    "tm": "2019-03-04T20:04:00.000Z"
  },
  {
    "ip": "54.4.5.6",
    "tm": "2019-01-04T19:54:11.000Z"
  }
]

最后,使用 jq 过滤掉除第一个结果以外的所有结果,如下所示:

Finally, use jq to filter out all but the first result, as follows:

jq 'sort_by(.tm) | reverse | .[0]'

这将产生一个结果,即最近启动的实例:

This will yield one result, the most recently launched instance:

{
  "ip": "52.1.2.3",
  "tm": "2019-03-04T20:04:00.000Z"
}

将它们放在一起,最终命令是:

Putting it all together, the final command is:

aws ec2 describe-instances  \
    --query 'Reservations[].Instances[].{ip: PublicIpAddress, tm: LaunchTime}' \
    --filters 'Name=tag:Name,Values= myInstanceName' | \
    jq 'sort_by(.tm) | reverse | .[0]'

这篇关于如何使用AWS CLI获取最近启动的EC2实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆