0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS CLI で ES2 のインスタンス名をちょろっと json にしたい

Last updated at Posted at 2021-04-23

目的

ちょっとやりたい事があって、その準備運動としてEc2インスタンスから必要な情報をjsonに加工してみた

やってみた

cli コマンドを打つとダラダラとこんな感じ。。。。
欲しい情報はタグで設定されているインスタンス名とIDと状態

~ $ aws ec2 describe-instances
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-xxxxxxxxxx",
                    "InstanceId": "i-xxxxxxxxxx",
                    "InstanceType": "t2.micro",
                    "KeyName": "aws_key_xx",
                    "LaunchTime": "2021-04-23T10:51:23.000Z",
<略>
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
<略>
                    "Tags": [
                        {
                            "Key": "env",
                            "Value": "DEV"
                        },
                        {
                            "Key": "Name",
                            "Value": "dev01"
                        }
                    ],
<略>

こうなった

jqちょろっとしか使ってないから上手く書けなかったけど、jqの出力をまたjqに。。。
とりあえずできた、わーい

~ $ aws ec2 describe-instances | jq '.Reservations[].Instances[] | [{tag: .Tags[] , id: .InstanceId , state: .State}]' | jq '.[] | select(.tag.Key == "Name") | {name: .tag.Value ,id: .id ,state: .state.Name}'
{
  "name": "dev01",
  "id": "i-xxxxxxxxxx",
  "state": "stopped"
}
{
  "name": "dev02",
  "id": "i-yyyyyyyyyy",
  "state": "stopped"
}

おまけ

jq のページ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?