【AWS】aws(AWS CLI) & jq コマンドの便利な使い方解説【運用管理に役立つ】

公開日時:2019年05月12日 / 最終更新日時:2019年05月26日

最近は各企業でもオンプロミス環境(いわゆるデータセンターのラックに物理サーバーを設置して LAN ケーブルや L2 スイッチを接続して構築している環境)からクラウド環境への移行が進んでいます。

AWS を始めとするクラウド環境は、数分でサーバーを構築できる反面、気が付くと数百台・数千台クラスになることがあります。

そんな時に役立つ aws(AWS CLI) & jq コマンドの便利な使い方を解説します。

 

AWS CLI(コマンドラインインタフェース)& jq コマンドのインストール方法は以下の記事を参考にしてください。

 

AWS】「EC2 インスタンス」または「パソコン」に AWS CLI(aws コマンド)及び jq コマンドをインストールする手順【Windows&Linux】

 

 

【Windows】PowerShell(パワーシェル)やコマンドプロンプトで画面バッファーが足りなくてコマンド実行結果が途中で切れる場合の対処方法

 

 

 

aws --version コマンドで動作確認

aws コマンドをインストール後に動作に問題がないか確認します。

PS C:\> aws --version
aws-cli/1.16.101 Python/3.6.0 Windows/7 botocore/1.12.91
PS C:\>

 

 

 

aws configure コマンドで認証の設定をする

「aws configure」コマンドで認証の設定をします。

PS C:\> aws configure
AWS Access Key ID [**************]: AXXXXXXXXXXXXXXX ← アクセスキーを設定します。
AWS Secret Access Key [**************]: MXXXXXXXXXXXXXXXXXXXXXXXXXX ← シークレットアクセスキーを設定します。
Default region name [ap-northeast-1]: ← 東京リージョン(ap-northeast-1)で使用するため、Enter のみ押下します。
Default output format [json]: ← jq コマンドを利用するのでデフォルトの「JSON」を設定します。
PS C:\>

 

 

 

EC2 インスタンスの情報取得

aws ec2 describe-instances コマンドで EC2 インスタンスのすべての情報をしゅとくでき

 

PS C:\> aws ec2 describe-instances 

    "Reservations": [ 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    "AmiLaunchIndex": 0, 
 
 ~ 省略 ~

 

 

describe 系のコマンドを実行すると、コマンド実行結果が長くなることがあるため、以下の方法で画面バッファ―を拡張します。

プロパティ上では「9999」が限度ですが、以下の記事の方法では「32766」まで拡張できます。

 

【Windows】PowerShell(パワーシェル)やコマンドプロンプトで画面バッファーが足りなくてコマンド実行結果が途中で切れる場合の対処方法

 

jq コマンドと組み合わせてインスタンスIDのみ取得します。

PS C:\> aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'
"i-xxxxxxx"
"i-xxxxxxx"
"i-xxxxxxx"
"i-xxxxxxx"
"i-xxxxxxx"

 

jq コマンドとは

jq コマンドの公式サイトには以下の説明があります。

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

 

jq コマンドは JSON データにとって sed コマンドのようなものです。

sed、awk、grep、などテキストを操作するのと同じ簡単さで、jq コマンドを使って JSON データをスライス、フィルタリング、マッピング、変換することができます。

 

■jq コマンドマニュアル

公式マニュアルだがすべて英語

https://stedolan.github.io/jq/manual/

 

「jq  --help」コマンドでも多少解説が表示されます。

PS C:\> jq --help 
jq - commandline JSON processor [version 1.6] 
 
Usage:  C:\Program Files\Amazon\AWSCLI\bin\jq.exe [options] <jq filter> [file...] 
        C:\Program Files\Amazon\AWSCLI\bin\jq.exe [options] --args <jq filter> [strings...] 
        C:\Program Files\Amazon\AWSCLI\bin\jq.exe [options] --jsonargs <jq filter> [JSON_TEXTS...] 
 
jq is a tool for processing JSON inputs, applying the given filter to 
its JSON text inputs and producing the filter's results as JSON on 
standard output. 
 
The simplest filter is ., which copies jq's input to its output 
unmodified (except for formatting, but note that IEEE754 is used 
for number representation internally, with all that that implies). 
 
For more advanced filters see the jq(1) manpage ("man jq") 
and/or https://stedolan.github.io/jq 
 
Example: 
 
        $ echo '{"foo": 0}' | jq . 
        { 
                "foo": 0 
        } 
 
Some of the options include: 
  -c               compact instead of pretty-printed output; 
  -n               use `null` as the single input value; 
  -e               set the exit status code based on the output; 
  -s               read (slurp) all inputs into an array; apply filter to it; 
  -r               output raw strings, not JSON texts; 
  -R               read raw strings, not JSON texts; 
  -C               colorize JSON; 
  -M               monochrome (don't colorize JSON); 
  -S               sort keys of objects on output; 
  --tab            use tabs for indentation; 
  --arg a v        set variable $a to value 
  --argjson a v    set variable $a to JSON value 
  --slurpfile a f  set variable $a to an array of JSON texts read from 
  --rawfile a f    set variable $a to a string consisting of the contents of 
  --args           remaining arguments are string arguments, not files; 
  --jsonargs       remaining arguments are JSON arguments, not files; 
  --               terminates argument processing; 
 
Named arguments are also available as $ARGS.named[], while 
positional arguments are available as $ARGS.positional[]. 
 
See the manpage for more options. 
PS C:\> 

 

 

 

JSON とは

JSON に関してはこちらの記事でも解説しています。

 

【Ansible】【Python】Ansible で取得した JSON 形式のデータをパースして CSV、Excel 形式にコンバートするプログラム

 

 

 

aws ec2 describe-instances コマンド結果の分析

「aws ec2 describe-instances」コマンドの実行結果を見つつ構造を確認します。

以下は、テスト用に 3つのインスタンスが存在しているとします。

(本インスタンスはテスト用で確認が終わったら削除します)

PS C:\> aws ec2 describe-instances 

    "Reservations": [ 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    "AmiLaunchIndex": 0, 
                    "ImageId": "ami-0646204ed750711f5", 
                    "InstanceId": "i-07781d27bf720c5db", 
                    "InstanceType": "t2.micro", 
                    "KeyName": "basic-network", 
                    "LaunchTime": "2018-12-21T23:11:41.000Z", 
                    "Monitoring": { 
                        "State": "disabled" 
                    }, 
                    "Placement": { 
                        "AvailabilityZone": "ap-northeast-1a", 
                        "GroupName": "", 
                        "Tenancy": "default" 
                    }, 
                    "PrivateDnsName": "ip-172-xx-xx-94.ap-northeast-1.compute.internal", 
                    "PrivateIpAddress": "172.xx.xx.94", 
                    "ProductCodes": [], 
                    "PublicDnsName": "", 
                    "State": { 
                        "Code": 80, 
                        "Name": "stopped" 
                    }, 
                    "StateTransitionReason": "User initiated (2018-12-23 01:01:11 GMT)", 
                    "SubnetId": "subnet-653b2a2c", 
                    "VpcId": "vpc-dc2376bb", 
                    "Architecture": "x86_64", 
                    "BlockDeviceMappings": [ 
                        { 
                            "DeviceName": "/dev/sda1", 
                            "Ebs": { 
                                "AttachTime": "2018-12-21T23:11:41.000Z", 
                                "DeleteOnTermination": true, 
                                "Status": "attached", 
                                "VolumeId": "vol-094b5de281275233d" 
                            } 
                        } 
                    ], 
                    "ClientToken": "", 
                    "EbsOptimized": false, 
                    "Hypervisor": "xen", 
                    "NetworkInterfaces": [ 
                        { 
                            "Attachment": { 
                                "AttachTime": "2018-12-21T23:11:41.000Z", 
                                "AttachmentId": "eni-attach-0855afc43d3a73263", 
                                "DeleteOnTermination": true, 
                                "DeviceIndex": 0, 
                                "Status": "attached" 
                            }, 
                            "Description": "", 
                            "Groups": [ 
                                { 
                                    "GroupName": "launch-wizard-10", 
                                    "GroupId": "sg-0f854918497e22364" 
                                } 
                            ], 
                            "Ipv6Addresses": [], 
                            "MacAddress": "06:9e:b6:52:dd:fa", 
                            "NetworkInterfaceId": "eni-0f71ba7e901bfb995", 
                            "OwnerId": "295332586563", 
                            "PrivateDnsName": "ip-172-xx-xx-94.ap-northeast-1.compute.internal", 
                            "PrivateIpAddress": "172.xx.xx.94", 
                            "PrivateIpAddresses": [ 
                                { 
                                    "Primary": true, 
                                    "PrivateDnsName": "ip-172-xx-xx-94.ap-northeast-1.compute.internal", 
                                    "PrivateIpAddress": "172.xx.xx.94" 
                                } 
                            ], 
                            "SourceDestCheck": true, 
                            "Status": "in-use", 
                            "SubnetId": "subnet-653b2a2c", 
                            "VpcId": "vpc-dc2376bb" 
                        } 
                    ], 
                    "RootDeviceName": "/dev/sda1", 
                    "RootDeviceType": "ebs", 
                    "SecurityGroups": [ 
                        { 
                            "GroupName": "launch-wizard-10", 
                            "GroupId": "sg-0f854918497e22364" 
                        } 
                    ], 
                    "SourceDestCheck": true, 
                    "StateReason": { 
                        "Code": "Client.UserInitiatedShutdown", 
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown" 
                    }, 
                    "Tags": [ 
                        { 
                            "Key": "Name", 
                            "Value": "centos6" 
                        } 
                    ], 
                    "VirtualizationType": "hvm", 
                    "CpuOptions": { 
                        "CoreCount": 1, 
                        "ThreadsPerCore": 1 
                    }, 
                    "CapacityReservationSpecification": { 
                        "CapacityReservationPreference": "open" 
                    }, 
                    "HibernationOptions": { 
                        "Configured": false 
                    } 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-049eac652b765c205" 
        }, 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    "AmiLaunchIndex": 0, 
                    "ImageId": "ami-0358ac7a36387646f", 
                    "InstanceId": "i-025cb0be42e38aca3", 
                    "InstanceType": "t2.micro", 
                    "KeyName": "basic-network", 
                    "LaunchTime": "2019-02-11T01:43:41.000Z", 
                    "Monitoring": { 
                        "State": "disabled" 
                    }, 
                    "Placement": { 
                        "AvailabilityZone": "ap-northeast-1a", 
                        "GroupName": "", 
                        "Tenancy": "default" 
                    }, 
                    "Platform": "windows", 
                    "PrivateDnsName": "ip-172-xx-xx-160.ap-northeast-1.compute.internal", 
                    "PrivateIpAddress": "172.xx.xx.160", 
                    "ProductCodes": [], 
                    "PublicDnsName": "ec2-3-xx-xx-203.ap-northeast-1.compute.amazonaws.com", 
                    "PublicIpAddress": "3.xx.xx.203", 
                    "State": { 
                        "Code": 16, 
                        "Name": "running" 
                    }, 
                    "StateTransitionReason": "", 
                    "SubnetId": "subnet-653b2a2c", 
                    "VpcId": "vpc-dc2376bb", 
                    "Architecture": "x86_64", 
                    "BlockDeviceMappings": [ 
                        { 
                            "DeviceName": "/dev/sda1", 
                            "Ebs": { 
                                "AttachTime": "2019-02-11T01:43:42.000Z", 
                                "DeleteOnTermination": true, 
                                "Status": "attached", 
                                "VolumeId": "vol-04296c21cbf230817" 
                            } 
                        } 
                    ], 
                    "ClientToken": "", 
                    "EbsOptimized": false, 
                    "EnaSupport": true, 
                    "Hypervisor": "xen", 
                    "NetworkInterfaces": [ 
                        { 
                            "Association": { 
                                "IpOwnerId": "amazon", 
                                "PublicDnsName": "ec2-3-xx-xx-203.ap-northeast-1.compute.amazonaws.com", 
                                "PublicIp": "3.xx.xx.203" 
                            }, 
                            "Attachment": { 
                                "AttachTime": "2019-02-11T01:43:41.000Z", 
                                "AttachmentId": "eni-attach-0bdd37d37539c8170", 
                                "DeleteOnTermination": true, 
                                "DeviceIndex": 0, 
                                "Status": "attached" 
                            }, 
                            "Description": "", 
                            "Groups": [ 
                                { 
                                    "GroupName": "launch-wizard-11", 
                                    "GroupId": "sg-0b3f1667e793e05de" 
                                } 
                            ], 
                            "Ipv6Addresses": [], 
                            "MacAddress": "06:cd:10:ae:fa:6e", 
                            "NetworkInterfaceId": "eni-08f325e62d334988b", 
                            "OwnerId": "295332586563", 
                            "PrivateDnsName": "ip-172-xx-xx-160.ap-northeast-1.compute.internal", 
                            "PrivateIpAddress": "172.xx.xx.160", 
                            "PrivateIpAddresses": [ 
                                { 
                                    "Association": { 
                                        "IpOwnerId": "amazon", 
                                        "PublicDnsName": "ec2-3-xx-xx-203.ap-northeast-1.compute.amazonaws.com", 
                                        "PublicIp": "3.xx.xx.203" 
                                    }, 
                                    "Primary": true, 
                                    "PrivateDnsName": "ip-172-xx-xx-160.ap-northeast-1.compute.internal", 
                                    "PrivateIpAddress": "172.xx.xx.160" 
                                } 
                            ], 
                            "SourceDestCheck": true, 
                            "Status": "in-use", 
                            "SubnetId": "subnet-653b2a2c", 
                            "VpcId": "vpc-dc2376bb" 
                        } 
                    ], 
                    "RootDeviceName": "/dev/sda1", 
                    "RootDeviceType": "ebs", 
                    "SecurityGroups": [ 
                        { 
                            "GroupName": "launch-wizard-11", 
                            "GroupId": "sg-0b3f1667e793e05de" 
                        } 
                    ], 
                    "SourceDestCheck": true, 
                    "Tags": [ 
                        { 
                            "Key": "Name", 
                            "Value": "Windows" 
                        } 
                    ], 
                    "VirtualizationType": "hvm", 
                    "CpuOptions": { 
                        "CoreCount": 1, 
                        "ThreadsPerCore": 1 
                    }, 
                    "CapacityReservationSpecification": { 
                        "CapacityReservationPreference": "open" 
                    }, 
                    "HibernationOptions": { 
                        "Configured": false 
                    } 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-0cf9e498e624df7eb" 
        }, 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    "AmiLaunchIndex": 0, 
                    "ImageId": "ami-6b0d5f0d", 
                    "InstanceId": "i-0d994f3fcd9b15309", 
                    "InstanceType": "t2.micro", 
                    "KeyName": "basic-network", 
                    "LaunchTime": "2019-02-11T01:43:57.000Z", 
                    "Monitoring": { 
                        "State": "disabled" 
                    }, 
                    "Placement": { 
                        "AvailabilityZone": "ap-northeast-1a", 
                        "GroupName": "", 
                        "Tenancy": "default" 
                    }, 
                    "PrivateDnsName": "ip-172-31-xx-38.ap-northeast-1.compute.internal", 
                    "PrivateIpAddress": "172.31.xx.38", 
                    "ProductCodes": [], 
                    "PublicDnsName": "ec2-52-199-xx-25.ap-northeast-1.compute.amazonaws.com", 
                    "PublicIpAddress": "52.199.xx.25", 
                    "State": { 
                        "Code": 16, 
                        "Name": "running" 
                    }, 
                    "StateTransitionReason": "", 
                    "SubnetId": "subnet-653b2a2c", 
                    "VpcId": "vpc-dc2376bb", 
                    "Architecture": "x86_64", 
                    "BlockDeviceMappings": [ 
                        { 
                            "DeviceName": "/dev/sda1", 
                            "Ebs": { 
                                "AttachTime": "2018-10-23T12:30:00.000Z", 
                                "DeleteOnTermination": true, 
                                "Status": "attached", 
                                "VolumeId": "vol-0c670bcc4823fd0c8" 
                            } 
                        } 
                    ], 
                    "ClientToken": "", 
                    "EbsOptimized": false, 
                    "EnaSupport": true, 
                    "Hypervisor": "xen", 
                    "NetworkInterfaces": [ 
                        { 
                            "Association": { 
                                "IpOwnerId": "amazon", 
                                "PublicDnsName": "ec2-52-199-xx-25.ap-northeast-1.compute.amazonaws.com", 
                                "PublicIp": "52.199.xx.25" 
                            }, 
                            "Attachment": { 
                                "AttachTime": "2018-10-23T12:29:59.000Z", 
                                "AttachmentId": "eni-attach-0291f7882efee8486", 
                                "DeleteOnTermination": true, 
                                "DeviceIndex": 0, 
                                "Status": "attached" 
                            }, 
                            "Description": "", 
                            "Groups": [ 
                                { 
                                    "GroupName": "launch-wizard-9", 
                                    "GroupId": "sg-04384c2d5aa72eeb8" 
                                } 
                            ], 
                            "Ipv6Addresses": [], 
                            "MacAddress": "06:1d:6f:79:f9:c8", 
                            "NetworkInterfaceId": "eni-0b42b24b80c8cb879", 
                            "OwnerId": "295332586563", 
                            "PrivateDnsName": "ip-172-31-xx-38.ap-northeast-1.compute.internal", 
                            "PrivateIpAddress": "172.31.xx.38", 
                            "PrivateIpAddresses": [ 
                                { 
                                    "Association": { 
                                        "IpOwnerId": "amazon", 
                                        "PublicDnsName": "ec2-52-199-xx-25.ap-northeast-1.compute.amazonaws.com", 
                                        "PublicIp": "52.199.xx.25" 
                                    }, 
                                    "Primary": true, 
                                    "PrivateDnsName": "ip-172-31-xx-38.ap-northeast-1.compute.internal", 
                                    "PrivateIpAddress": "172.31.xx.38" 
                                } 
                            ], 
                            "SourceDestCheck": true, 
                            "Status": "in-use", 
                            "SubnetId": "subnet-653b2a2c", 
                            "VpcId": "vpc-dc2376bb" 
                        } 
                    ], 
                    "RootDeviceName": "/dev/sda1", 
                    "RootDeviceType": "ebs", 
                    "SecurityGroups": [ 
                        { 
                            "GroupName": "launch-wizard-9", 
                            "GroupId": "sg-04384c2d5aa72eeb8" 
                        } 
                    ], 
                    "SourceDestCheck": true, 
                    "Tags": [ 
                        { 
                            "Key": "Name", 
                            "Value": "redhat7" 
                        } 
                    ], 
                    "VirtualizationType": "hvm", 
                    "CpuOptions": { 
                        "CoreCount": 1, 
                        "ThreadsPerCore": 1 
                    }, 
                    "HibernationOptions": { 
                        "Configured": false 
                    } 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-09c07a3a22006e32a" 
        } 
    ] 

PS C:\> 

 

 

大枠の構造を確認するために中身を取り去りました。

PS C:\> aws ec2 describe-instances 

    "Reservations": [ 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    ~ 省略 ~ 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-049eac652b765c205" 
        }, 
        { 
            "Groups": [], 
            "Instances": [ 
                {

                    ~ 省略 ~ 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-0cf9e498e624df7eb" 
        }, 
        { 
            "Groups": [], 
            "Instances": [ 
                { 
                    ~ 省略 ~ 
                } 
            ], 
            "OwnerId": "295332586563", 
            "ReservationId": "r-09c07a3a22006e32a" 
        } 
    ] 

PS C:\>

 

 

AWS CLI より EC2 インスタンスの構築

AWS CLI より EC2 インスタンスの構築も可能です。

手順は最初に EC2 インスタンスの AMI イメージを作成します。

AMI イメージを作成する際は、EC2 インスタンスを停止しておくと確実にイメージを作成できます。

 

EC2 インスタンスを作成する際は --dry-run オプションを付ける

いきなり EC2 インスタンスを作成すると思わぬ動きをすることがあります。

そのため最初は「--dry-run」オプションを付けて本番実行前にリハーサルを行ないましょう。

 

タグもコマンドラインで付与する

AWS はタグもコマンドラインで付与することができます。

台数が増えてくると構成管理が必要になるのでタグを付けましょう。

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

Secured By miniOrange