11
9

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 5 years have passed since last update.

[AWS]aws-cliでJSONを扱うためにーjq基本編ー

Last updated at Posted at 2016-03-07

###jqを使ってAWSの情報を取得しよう①
コード部分だけ書き出し。Qiitaは色つくからいいね


###JSONの基本

JSONはキーと値のセットで表現されています。
キーと値のセットは以下の様に表されます。
また","で区切って複数のセットを列挙することができます。

JSONの基本形
{"キー":"値"}
{"キー":"値","キー":"値"}

JSONは配列を持ち、配列は[]で表されます。
要素は","で区切って表記されます。

サンプル①(値に配列を保つ場合)
"Tags":[
    {
        "Key": "Name",
        "Value": "test"
    }
]
サンプル①を一列で書くと
"Tags":[{"Key":"Name","Value":"test"}]
サンプル②(配列が複数要素を持つ場合)
"Tags": [
    {
        "Key": "hoge",
        "Value": "***********"
    },
        "Key": "fuga",
        "Value": "***********"
    }
]
サンプル②を一列で書くと
"Tags": [ { "Key": "hoge" , "Value": "*****" } , { "Key": "fuga" , "Value": "****" } ]

####jqでパースする

describe-instances(ec2)
{
    "Reservations": [
        {
            "ReservationId": "r-********",
            "Groups": [],
            "OwnerId": "************",
            "Instances": [
                {
                    "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
                    "EbsOptimized": false,
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "SecurityGroups": [
                        {
                            "GroupName": "RDP",
                            "GroupId": "sg-********"
                        }
                    ],
                    "State": {
                        "Name": "stopped",
                        "Code": 80
                    },
                    "NetworkInterfaces": [
                        {
                            "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
                                    "PrivateIpAddress": "172.**.**.***",
                                    "Primary": true
                                }
                            ],
                            "OwnerId": "************",
                            "Groups": [
                                {
                                    "GroupName": "RDP",
                                    "GroupId": "sg-********"
                                }
                            ],
                            "PrivateIpAddress": "172.**.**.***",
                            "Attachment": {
                                "DeleteOnTermination": true,
                                "AttachTime": "2016-02-24T05:53:59.000Z",
                                "AttachmentId": "eni-attach-*******",
                                "Status": "attached",
                                "DeviceIndex": 0
                            },
                            "Description": "",
                            "SourceDestCheck": true,
                            "MacAddress": "************",
                            "Status": "in-use",
                            "NetworkInterfaceId": "eni-************",
                            "VpcId": "vpc-************",
                            "SubnetId": "subnet-************"
                        }
                    ],
                    "PrivateIpAddress": "172.**.**.***",
                    "RootDeviceType": "ebs",
                    "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)",
                    "Placement": {
                        "GroupName": "",
                        "Tenancy": "default",
                        "AvailabilityZone": "ap-northeast-1a"
                    },
                    "Hypervisor": "xen",
                    "KeyName": "macpro",
                    "RootDeviceName": "/dev/sda1",
                    "ImageId": "ami-************",
                    "LaunchTime": "2016-02-24T05:53:59.000Z",
                    "VpcId": "vpc-************",
                    "BlockDeviceMappings": [
                        {
                            "Ebs": {
                                "DeleteOnTermination": true,
                                "AttachTime": "2016-02-24T05:54:04.000Z",
                                "VolumeId": "vol-************",
                                "Status": "attached"
                            },
                            "DeviceName": "/dev/sda1"
                        }
                    ],
                    "Tags": [
                        {
                            "Key": "Name",
                            "Value": "test"
                        }
                    ],
                    "SubnetId": "subnet-************",
                    "ProductCodes": [],
                    "Architecture": "x86_64",
                    "InstanceType": "t2.micro",
                    "Platform": "windows",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "InstanceId": "i-************",
                    "SourceDestCheck": true,
                    "ClientToken": "rduVq************",
                    "AmiLaunchIndex": 0,
                    "PublicDnsName": "",
                    "VirtualizationType": "hvm"
                }
            ]
        }
    ]
}

####インスタンスid取得

InstanceId
aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'
結果
"i-************"

####セキュリティグループ一覧取得

Groups
aws ec2 describe-instances |jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[]'
結果
{
  "GroupName": "RDP",
  "GroupId": "sg-984bb5fc"
}

GroupId取得

GroupId
aws ec2 describe-instances |jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[].GroupId'
結果
"sg-984bb5fc"

####""を剥く

InstanceId
aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'
結果
i-************
11
9
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
11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?