LoginSignup
8
11

More than 5 years have passed since last update.

AWSの情報をCSV出力するワンライナー

Last updated at Posted at 2019-02-01

概要

EC2インスタンスの情報だとかRDSの情報だとかいろいろ一覧にして作業したいとき用のワンライナー
出力をスプレッドシートに貼り付けて,で列に分ければ表管理できるので便利

csv楽でいいんですよね、csv

前提

  • 各種資産を取得できるIAMの権限
  • ローカル端末はMacを使用
  • jqが必要

EC2

インスタンス名,インスタンスID,インスタンスタイプ,ステータス,作成日時
で出力し、クリップボードへ保存

Command
aws ec2 describe-instances \
    --query "Reservations[].Instances[].{InstanceId:InstanceId, InstanceType:InstanceType, State:State.Name, Name: Tags[?Key==\`Name\`].Value, LaunchTime:LaunchTime}" \
    --output json | 
    jq -r ".[] | [.Name[0], .InstanceId, .InstanceType, .State, .LaunchTime] | @csv" | pbcopy

RDS

DBインスタンス名,DBエンジン,インスタンスタイプ,DB名,ステータス,作成時間
で出力し、クリップボードへ保存

Command
aws rds describe-db-instances \
    --query "DBInstances[].{DBInstanceClass:DBInstanceClass, DBInstanceIdentifier:DBInstanceIdentifier, DBName:DBName, Engine:Engine, Status:DBInstanceStatus, InstanceCreateTime:InstanceCreateTime}" \
    --output json |
    jq -r ".[] | [.DBInstanceIdentifier, .Engine, .DBInstanceClass, .DBName, .Status , .InstanceCreateTime] | @csv" | pbcopy

ElasticBeanstalk

アプリケーション名,環境名,ステータス.作成日時,最終更新日時
で出力し、クリップボードへ保存

Command
aws elasticbeanstalk describe-environments \
    --query "Environments[].{ApplicationName:ApplicationName, EnvironmentName:EnvironmentName, Status:Status, DateUpdated:DateUpdated, DateCreated:DateCreated}" \
    --output json |
    jq -r ".[] | [.ApplicationName, .EnvironmentName, .Status, .DateCreated, .DateUpdated ] | @csv" | pbcopy

ElastiCache

ノード名,エンジン,ノードタイプ,ステータス,作成日時
で出力し、クリップボードへ保存

Command
aws elasticache describe-cache-clusters \
    --query "CacheClusters[].{Status:CacheClusterStatus, CacheNodeType:CacheNodeType, Engine:Engine, CacheClusterId:CacheClusterId, CacheClusterCreateTime:CacheClusterCreateTime}" \
    --output json |
    jq -r ".[] | [.CacheClusterId, .Engine, .CacheNodeType, .Status, .CacheClusterCreateTime] | @csv" | pbcopy

Cloudwatch logs

ロググループ毎の補完量と保管期間をログ量の降順で出力

Command
aws logs describe-log-groups \
    --query "logGroups[].{logGroupName:logGroupName, retentionInDays:retentionInDays, storedBytes:storedBytes}" \
    --output json |
    jq -r "sort_by(.storedBytes)| reverse | .[] | [.logGroupName, .storedBytes, .retentionInDays] | @csv" | pbcopy

VPC

Command
aws ec2 describe-vpcs \
    --query "Vpcs[].{VpcId:VpcId, Name:Tags[?Key==\`Name\`].Value, CidrBlock:CidrBlock, AccountId:OwnerId, IsDefaultVpc:IsDefault}" \
    --region ap-northeast-1 \
    --output json | jq -r ".[] | [.AccountId, .Name[0], .VpcId, .CidrBlock, .IsDefaultVpc] | @csv" | pbcopy

VPC peering

ステータスがactiveなもののみ表示

Command
aws ec2 describe-vpc-peering-connections \
    --query "VpcPeeringConnections[].{VpcPeeringConnectionId:VpcPeeringConnectionId, Name: Tags[?Key==\`Name\`].Value, AccepterAccountId:AccepterVpcInfo.OwnerId, AccepterVpcId:AccepterVpcInfo.VpcId, AccepterCidrBlock:AccepterVpcInfo.CidrBlock, RequesterAccountId:RequesterVpcInfo.OwnerId, RequesterVpcId:RequesterVpcInfo.VpcId, RequesterCidrBlock:RequesterVpcInfo.CidrBlock, Status:Status.Code}" \
    --region ap-northeast-1 \
    --output json |
    jq -r ".[] | select(.Status == \"active\") | [.VpcPeeringConnectionId, .Name[0], .AccepterAccountId, .AccepterVpcId, .AccepterCidrBlock, .RequesterAccountId, .RequesterVpcId, .RequesterCidrBlock] | @csv" | pbcopy
8
11
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
8
11