個人的によく使うAWS CLIコマンド


概要

個人的によく使うAWS CLIのコマンド集.
yamlが好きなので json2yaml が頻出しますが,そこは従うなり無視するなりご自由に...

EC2インスタンス情報のサマリ

INSTANCE_ID=''
AWS_REGION='ap-northeast-1'

aws ec2 describe-instances \
    --filters Name=instance-id,Values=${INSTANCE_ID} \
    --region ${AWS_REGION} \
    --query 'Reservations[].Instances[].{InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress,PrivateDnsName:PrivateDnsName,PublicIpAddress:PublicIpAddress,PublicDnsName:PublicDnsName,Tags:Tags[]}[0]' \
    --output json \
    | json2yaml

Output

Tags:
- Value: ******
  Key: DeviceID
- Value: ******
  Key: Name
- Value: ******
  Key: PIC
- Value: ******
  Key: ExpireDate
- Value: ******
  Key: CreatedDate
InstanceId: ******
PublicDnsName: ******
PrivateDnsName: ******
PublicIpAddress: ******
PrivateIpAddress: ******

インターネットゲートウェイ情報

INTERNET_GATEWAY_IDS=''

aws ec2 describe-internet-gateways \
    --internet-gateway-ids ${INTERNET_GATEWAY_IDS} \
    | json2yaml

Output

InternetGateways:
- OwnerId: ******
  Tags:
  - Value: ******
    Key: Name
  - Value: ******
    Key: CreatedDate
  - Value: ******
    Key: ExpireDate
  Attachments:
  - State: available
    VpcId: ******
  InternetGatewayId: igw-******

ルートテーブル情報

ROUTE_TABLE_IDS='rtb-******'

aws ec2 describe-route-tables \
    --route-table-ids ${ROUTE_TABLE_IDS} \
    | json2yaml

Output

RouteTables:
- Associations:
  - AssociationState:
      State: associated
    RouteTableAssociationId: ******
    Main: true
    RouteTableId: rtb-******
  RouteTableId: rtb-******
  VpcId: vpc-******
  PropagatingVgws:
  - GatewayId: vgw-******
  Tags:
  - Value: ******
    Key: ExpireDate
  - Value: ******
    Key: Name
  - Value: ******
    Key: CreatedDate
  Routes:
  - GatewayId: local
    DestinationCidrBlock: ******/**
    State: active
    Origin: CreateRouteTable
  - GatewayId: vgw-******
    DestinationCidrBlock: ******/**
    State: active
    Origin: EnableVgwRoutePropagation
  OwnerId: ******

AMI情報

IMAGE_IDS=''
AWS_REGION='ap-northeast-1'

aws ec2 describe-images \
    --region ${AWS_REGION} \
    --image-ids ${IMAGE_IDS} \
    | json2yaml

Output

Images:
- ProductCodes:
  - ProductCodeId: ******
    ProductCodeType: ******
  Description: ******
  Tags:
  - Value: ******
    Key: Name
  VirtualizationType: hvm
  Hypervisor: xen
  EnaSupport: true
  SriovNetSupport: simple
  ImageId: ******
  State: available
  BlockDeviceMappings:
  - DeviceName: /dev/sda1
    Ebs:
      SnapshotId: ******
      DeleteOnTermination: false
      VolumeType: gp2
      VolumeSize: 8
      Encrypted: true
  Architecture: x86_64
  ImageLocation: ******/******
  RootDeviceType: ebs
  OwnerId: ******
  RootDeviceName: /dev/sda1
  CreationDate: '2020-01-11T00:00:00.000Z'
  Public: false
  ImageType: machine
  Name: ******

セキュリティグループ情報

GROUP_IDS='sg-****** sg-******' のように複数指定可能.

GROUP_IDS=''

aws ec2 describe-security-groups \
    --group-ids ${GROUP_IDS} \
    | json2yaml

Output

SecurityGroups:
- IpPermissionsEgress:
  - IpProtocol: '-1'
    PrefixListIds: []
    IpRanges:
    - CidrIp: 0.0.0.0/0
    UserIdGroupPairs: []
    Ipv6Ranges: []
  Description: test-sg-1
  IpPermissions:
  - PrefixListIds: []
    FromPort: 80
    IpRanges:
    - CidrIp: ***.***.***.***/32
    ToPort: 80
    IpProtocol: tcp
    UserIdGroupPairs: []
    Ipv6Ranges: []
  GroupName: test-sg-1
  VpcId: ******
  OwnerId: ******
  GroupId: ******
- IpPermissionsEgress:
  - IpProtocol: '-1'
    PrefixListIds: []
    IpRanges:
    - CidrIp: 0.0.0.0/0
    UserIdGroupPairs: []
    Ipv6Ranges: []
  Description: test-sg-2
  IpPermissions:
  - PrefixListIds: []
    FromPort: 80
    IpRanges:
    - CidrIp: ***.***.***.***/32
    ToPort: 80
    IpProtocol: tcp
    UserIdGroupPairs: []
    Ipv6Ranges: []
  GroupName: test-sg-2
  VpcId: ******
  OwnerId: ******
  GroupId: ******

S3バケット一覧

aws s3api list-buckets \
  --query "Buckets[].Name"

Output

[
    "test-bucket-1", 
    "test-bucket-2", 
    "test-bucket-3", 
    "test-bucket-4", 
    "test-bucket-5"
]

S3バケットのタグ情報

BUCKET_NAME=''

aws s3api get-bucket-tagging \
  --bucket ${BUCKET_NAME} \
  | json2yaml

Output

TagSet:
- Value: '20210101'
  Key: Expire
- Value: '20200101'
  Key: Created
- Value: Production
  Key: Environment
- Value: test-bucket-1
  Key: Name

S3 ls

便利(?)なオプション.結果についてはOutput参照

  • --human
    • ファイルサイズに単位を付けて表示
  • --sum
    • オブジェクト数,ファイルサイズ合計を表示
  • --recursive
    • 再帰的表示
aws s3 ls s3://test-bucket-1/ \
  --recursive \
  --human \
  --sum

Output

aws s3 ls s3://test-bucket-1/ \
  --recursive
2010-01-11 18:16:16         88 AWSLogs/AccessLog
2010-01-11 18:20:08        646 AWSLogs/elasticloadbalancing/.../test-web.log.gz
aws s3 ls s3://test-bucket-1/ \
  --recursive \
  --sum    
2010-01-11 18:16:16         88 AWSLogs/AccessLog
2010-01-11 18:20:08        646 AWSLogs/elasticloadbalancing/.../test-web.log.gz

Total Objects: 2
   Total Size: 734
aws s3 ls s3://test-bucket-1/ \
  --recursive \
  --human \
  --sum
2010-01-11 18:16:16   88 Bytes AWSLogs/AccessLog
2010-01-11 18:20:08  646 Bytes AWSLogs/elasticloadbalancing/.../test-web.log.gz

Total Objects: 2
   Total Size: 734 Bytes

備考

随時加筆予定...