AWS CLIとjqを使った簡易管理ツール

AWSの新しいユニファイドCLIは最新バージョンである0.5.1でかなりサポートされたサービスが増えています。RDS, ELB, EMRなどの主力サービスはだいたいサポートされてきています。ところがどっこい、unified cliは基本的に返すレスポンスが基本JSONで返ってきます。なかなか人間様には見にくいので少々こねくりまわす必要があります。そこでJSONを簡単にパースしてくれるjqコマンドでうまいこと見たいものだけをとるのが望ましいです。


というわけで温泉ハッカソンで簡単なコンソール表示をEC2, ELB, RDSでやってみました。

EC2インスタンスのタイプ、状態、URLを見る

初級編。

aws ec2 describe-instances|
    jq '.reservationSet.instancesSet|
        .instanceType + " / " + .instanceState.name, .dnsName, 
    "---------------------------------------------------------"'


結果はこんな感じ。

"m2.xlarge / stopped"
""
"---------------------------------------------------------"
"m1.medium / stopped"
""
"---------------------------------------------------------"
"m1.medium / stopped"
""
"---------------------------------------------------------"
"t1.micro / running"
"ec2-54-249-133-52.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"m1.medium / running"
"ec2-54-228-151-12.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"m1.medium / running"
"ec2-54-228-155-116.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"t1.micro / running"
"ec2-54-228-38-217.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"t1.micro / running"
"ec2-54-229-153-38.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"t1.micro / running"
"ec2-173-34-19-211.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"t1.micro / running"
"ec2-46-54-220-111.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"t1.micro / running"
"ec2-54-223-50-57.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"
"m1.small / running"
"ec2-54-223-11-57.ap-northeast-1.compute.amazonaws.com"
"---------------------------------------------------------"

ELBのざっくりとした概要を知る

少し慣れてきたのでELBではもうちょい細かく知りたい感じだったので、ぶら下がってるインスタンスと、ポートのフォワードとヘルスチェックも。
もうちょいやるなら、インスタンスの詳細も欲しいな。

aws elb describe-load-balancers|
    jq '.LoadBalancerDescriptions|
        .DNSName, 
        "Instance", 
            "    " + .Instances.InstanceId, 
         "AZ", 
            "    " + .AvailabilityZones, 
         "Listen port config", 
            "    " + .ListenerDescriptions.Listener.Protocol + ":" + (.ListenerDescriptions.Listener.LoadBalancerPort|tostring) + "->" + .ListenerDescriptions.Listener.InstanceProtocol + ":" + (.ListenerDescriptions[].Listener.InstancePort|tostring),
         "Healthcheck",
            "    " + .HealthCheck.Target, 
"====================================================================="'

さっくり力技。

"myfrontend-1709110016.ap-northeast-1.elb.amazonaws.com"
"Instance"
"    i-0948680a"
"    i-0d48680e"
"    i-0f48680c"
"    i-6d4a6a6e"
"    i-c14868c2"
"    i-c74868c4"
"AZ"
"    ap-northeast-1a"
"    ap-northeast-1b"
"    ap-northeast-1c"
"Listen port config"
"    HTTP:80->HTTP:80"
"Healthcheck"
"    HTTP:80/index.html"
"====================================================================="
"yourapp442.ap-northeast-1.elb.amazonaws.com"
"Instance"
"AZ"
"    ap-northeast-1a"
"Listen port config"
"    HTTP:80->HTTP:80"
"Healthcheck"
"    HTTP:80/index.html"
"====================================================================="

RDSでもざっくり概況を知る

RDSでは、エンジンやバージョン、ストレージだけでなく、マルチAZかどうか、リードレプリカなども。

aws rds describe-db-instances|
    jq '.DBInstances[]| 
        .DBInstanceIdentifier + "(" + .Engine + " " + .EngineVersion + ", " + (.AllocatedStorage|tostring) + "GB)" + "[" + .DBInstanceStatus + "]",
        .AvailabilityZone, 
        .Endpoint.Address + ":" + (.Endpoint.Port|tostring),
        if .MultiAZ == true then "MAZ" else "Single" end,
        if .ReadReplicaSourceDBInstanceIdentifier then "RR : " + .ReadReplicaSourceDBInstanceIdentifier  else "" end,
"======================================================================" '

結果はこんなん。

"hoge(mysql 5.5.8, 5GB)[available]"
"ap-northeast-1c"
"hoge.cb1tkvzxk6ev.ap-northeast-1.rds.amazonaws.com:3306"
"MAZ"
""
"======================================================================"
"mydb(mysql 5.5.27, 5GB)[available]"
"ap-northeast-1b"
"mydb.cb1tkvzxk6ev.ap-northeast-1.rds.amazonaws.com:3306"
"Single"
""
"======================================================================"
"mydb-rr(mysql 5.5.27, 5GB)[available]"
"ap-northeast-1b"
"mydb-rr.cb1tkvzxk6ev.ap-northeast-1.rds.amazonaws.com:3306"
"Single"
"RR : mydb"
"======================================================================"


まあほとんどjqの練習になってしまいましたが、まあいいでしょう。