[小ネタ] jq コマンドでEC2インスタンスの情報を取得する

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは、川原です。

今回は(今回も?)小ネタです。
AWS CLIを使い始めると、コマンドレスポンスがJSON形式であるため必然的にJSON整形フィルタであるjqコマンドと仲良くせざるを得ません。 (JMESPath と仲良くするという手もありますが、jqコマンドと仲良くなるのとそんなに変わらないかな、と感じています)
ただ、jqコマンドをそんなに頻繁に使わないので、どうやってフィルタリングするんだっけ? と、調べることが多々あります。

jq 使用例

ということで、jqの(知っていると便利そうな)エッセンスを含んだjqコマンドの使用例が以下になります。

>aws ec2 describe-instances | jq -r '.Reservations | sort_by(.Instances[].Tags[] | select(.Key == "Name").Value) | .[].Instances[] | [(.Tags[] | select(.Key == "Name").Value), .InstanceId, .PublicIpAddress, .PrivateIpAddress, .InstanceType, .LaunchTime, .State.Name] | @csv'
"12345678901234567890","2000000000",,"172.31.31.10","t2.nano","2016-02-09T01:24:57.000Z","stopped"
"elasticsearch-test","i-zzzzbbbbcccxxxxxx","52.ii.jj.kk","172.31.25.172","t2.micro","2016-06-15T07:22:07.000Z","stopped"
"long-id","i-10000000","54.xxx.yyy.zzz","172.31.18.97","t2.nano","2016-06-23T10:53:36.000Z","running"

この jq ではAWS CLIで取得したEC2インスタンスの情報を以下のように整形して出力しています。

  • EC2における以下のインスタンス情報を抽出し、それらをCSV形式で出力
    • Nameタグの値
    • インスタンスID
    • パブリックIP
    • プライベートIP
    • インスタンスタイプ
    • 起動時刻
    • インスタンスの状態
  • レコード順はNameタグ値の昇順でソート

上記AWS CLIのコマンド aws ec2 describe-instance が出力するJSONデータは本ブログの最下部に記載しておきます。

上記コマンドの解説は下記の通りです。

>aws ec2 describe-instances | jq -r '.Reservations
 ↑Reservationsを剥して、Reservationオブジェクトの配列にして
| sort_by(.Instances[].Tags[] | select(.Key == "Name").Value)
 ↑ReservationオブジェクトからInstancesを剥して、Tags配列を取得して、その配列の中のKey名が"Name"を選択して、Value値を取得して、そのValue値でソートして
| .[].Instances[]
 ↑(ソートした)Reservationオブジェクト配列からInstanceオブジェクトを抽出して
| [(.Tags[] | select(.Key == "Name").Value), .InstanceId, .PublicIpAddress, .PrivateIpAddress, .InstanceType, .LaunchTime, .State.Name] | @csv'
↑抽出したInstanceオブジェクトから各項目を取得して配列を生成して、その配列をCSV形式で出力

sort_byselect あたりの使い方を覚えておくと(構文を理解しておくと)、色々と応用が効くと思います。


上記の aws ec2 describe-instances コマンドが出力するJSONデータ

{
    "Reservations": [
        {
            "OwnerId": "xxxx8467yyyy",
            "ReservationId": "r-ccxxeeee",
            "Groups": [],
            "Instances": [
                {
                    "Monitoring": {
                        "State": "enabled"
                    },
                    "PublicDnsName": "",
                    "RootDeviceType": "ebs",
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2016-02-09T01:24:57.000Z",
                    "PrivateIpAddress": "172.31.31.10",
                    "ProductCodes": [],
                    "VpcId": "vpc-aaaaabbb",
                    "StateTransitionReason": "User initiated (2016-02-10 02:19:06 GMT)",
                    "InstanceId": "2000000000",
                    "ImageId": "ami-1c1b9f1c",
                    "PrivateDnsName": "ip-172-31-31-10.ap-northeast-1.compute.internal",
                    "KeyName": "verification",
                    "SecurityGroups": [
                        {
                            "GroupName": "default",
                            "GroupId": "sg-rrrriiii"
                        },
                        {
                            "GroupName": "Route53_HealthCheck",
                            "GroupId": "sg-rrrrxxxx"
                        }
                    ],
                    "ClientToken": "c99999uuuuuuuuuuuu",
                    "SubnetId": "subnet-zzzzzzyy",
                    "InstanceType": "t2.nano",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "06:48:57:39:ae:3f",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-aaaaabbb",
                            "Description": "",
                            "NetworkInterfaceId": "eni-c0000000",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-31-10.ap-northeast-1.compute.internal",
                                    "Primary": true,
                                    "PrivateIpAddress": "172.31.31.10"
                                }
                            ],
                            "PrivateDnsName": "ip-172-31-31-10.ap-northeast-1.compute.internal",
                            "Attachment": {
                                "Status": "attached",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "eni-attach-zzzddddd",
                                "AttachTime": "2015-09-18T00:55:52.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "default",
                                    "GroupId": "sg-rrrriiii"
                                },
                                {
                                    "GroupName": "Route53_HealthCheck",
                                    "GroupId": "sg-rrrrxxxx"
                                }
                            ],
                            "SubnetId": "subnet-zzzzzzyy",
                            "OwnerId": "xxxx8467yyyy",
                            "PrivateIpAddress": "172.31.31.10"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "ap-northeast-1a"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-eeeexxxx",
                                "AttachTime": "2015-09-18T00:55:56.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "IamInstanceProfile": {
                        "Id": "zzzzuuuuccccccccccccc",
                        "Arn": "arn:aws:iam::xxxx8467yyyy:instance-profile/EC2Admin"
                    },
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "12345678901234567890",
                            "Key": "Name"
                        },
                        {
                            "Value": "foo",
                            "Key": "hoge"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ]
        },
        {
            "OwnerId": "xxxx8467yyyy",
            "ReservationId": "r-zzzzxxxx",
            "Groups": [],
            "Instances": [
                {
                    "Monitoring": {
                        "State": "enabled"
                    },
                    "PublicDnsName": "ec2-54-xxx-yyy-zzz.ap-northeast-1.compute.amazonaws.com",
                    "State": {
                        "Code": 16,
                        "Name": "running"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2016-06-23T10:53:36.000Z",
                    "PublicIpAddress": "54.xxx.yyy.zzz",
                    "PrivateIpAddress": "172.31.18.97",
                    "ProductCodes": [],
                    "VpcId": "vpc-aaaaabbb",
                    "StateTransitionReason": "",
                    "InstanceId": "i-10000000",
                    "ImageId": "ami-2038ae20",
                    "PrivateDnsName": "ip-172-31-18-97.ap-northeast-1.compute.internal",
                    "KeyName": "verification",
                    "SecurityGroups": [
                        {
                            "GroupName": "Route53_HealthCheck",
                            "GroupId": "sg-rrrrxxxx"
                        }
                    ],
                    "ClientToken": "100000009999999999",
                    "SubnetId": "subnet-zzzzzzyy",
                    "InstanceType": "t2.nano",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "06:a7:4c:94:70:85",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-aaaaabbb",
                            "Description": "",
                            "Association": {
                                "PublicIp": "54.xxx.yyy.zzz",
                                "PublicDnsName": "ec2-54-xxx-yyy-zzz.ap-northeast-1.compute.amazonaws.com",
                                "IpOwnerId": "xxxx8467yyyy"
                            },
                            "NetworkInterfaceId": "eni-zzzzyyyy",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-18-97.ap-northeast-1.compute.internal",
                                    "Association": {
                                        "PublicIp": "54.xxx.yyy.zzz",
                                        "PublicDnsName": "ec2-54-xxx-yyy-zzz.ap-northeast-1.compute.amazonaws.com",
                                        "IpOwnerId": "xxxx8467yyyy"
                                    },
                                    "Primary": true,
                                    "PrivateIpAddress": "172.31.18.97"
                                }
                            ],
                            "PrivateDnsName": "ip-172-31-18-97.ap-northeast-1.compute.internal",
                            "Attachment": {
                                "Status": "attached",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "eni-attach-zzzztttt",
                                "AttachTime": "2015-12-17T01:28:05.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "Route53_HealthCheck",
                                    "GroupId": "sg-rrrrxxxx"
                                }
                            ],
                            "SubnetId": "subnet-zzzzzzyy",
                            "OwnerId": "xxxx8467yyyy",
                            "PrivateIpAddress": "172.31.18.97"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "ap-northeast-1a"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-zzzzuuuu",
                                "AttachTime": "2015-12-17T01:28:08.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "IamInstanceProfile": {
                        "Id": "zzzzuuuuccccccccccccc",
                        "Arn": "arn:aws:iam::xxxx8467yyyy:instance-profile/EC2Admin"
                    },
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "long-id",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ]
        },
        {
            "OwnerId": "xxxx8467yyyy",
            "ReservationId": "r-zzzzuuuucccxxxxxx",
            "Groups": [],
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "ec2-52-ii-jj-kkk.ap-northeast-1.compute.amazonaws.com",
                    "RootDeviceType": "ebs",
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2016-06-15T07:22:07.000Z",
                    "PublicIpAddress": "52.ii.jj.kk",
                    "PrivateIpAddress": "172.31.25.172",
                    "ProductCodes": [],
                    "VpcId": "vpc-aaaaabbb",
                    "StateTransitionReason": "User initiated (2016-06-15 09:13:50 GMT)",
                    "InstanceId": "i-zzzzbbbbcccxxxxxx",
                    "ImageId": "ami-29160d47",
                    "PrivateDnsName": "ip-172-31-25-172.ap-northeast-1.compute.internal",
                    "KeyName": "verification",
                    "SecurityGroups": [
                        {
                            "GroupName": "HOME_ACCESS",
                            "GroupId": "sg-ccxxxxxx"
                        }
                    ],
                    "ClientToken": "200000009999999999",
                    "SubnetId": "subnet-zzzzzzyy",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "06:7b:2b:61:c1:db",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-aaaaabbb",
                            "Description": "",
                            "Association": {
                                "PublicIp": "52.ii.jj.kk",
                                "PublicDnsName": "ec2-52-ii-jj-kk.ap-northeast-1.compute.amazonaws.com",
                                "IpOwnerId": "xxxx8467yyyy"
                            },
                            "NetworkInterfaceId": "eni-1c309957",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-25-172.ap-northeast-1.compute.internal",
                                    "Association": {
                                        "PublicIp": "52.ii.jj.kk",
                                        "PublicDnsName": "ec2-52-ii-jj-kk.ap-northeast-1.compute.amazonaws.com",
                                        "IpOwnerId": "xxxx8467yyyy"
                                    },
                                    "Primary": true,
                                    "PrivateIpAddress": "172.31.25.172"
                                }
                            ],
                            "PrivateDnsName": "ip-172-31-25-172.ap-northeast-1.compute.internal",
                            "Attachment": {
                                "Status": "attached",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "eni-attach-ccxxxmmm",
                                "AttachTime": "2016-06-04T13:24:59.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "HOME_ACCESS",
                                    "GroupId": "sg-ccxxxxxx"
                                }
                            ],
                            "SubnetId": "subnet-zzzzzzyy",
                            "OwnerId": "xxxx8467yyyy",
                            "PrivateIpAddress": "172.31.25.172"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "ap-northeast-1a"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-ccxxxddd",
                                "AttachTime": "2016-06-04T13:25:00.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "IamInstanceProfile": {
                        "Id": "zzzzuuuuccccccccccccc",
                        "Arn": "arn:aws:iam::xxxx8467yyyy:instance-profile/EC2Admin"
                    },
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "elasticsearch-test",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}