Posted at

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

AWS CLIを使うと1つ1つのサブコマンドも引数名も長いし、出力も情報量が多いので、よく使うものを jq と組み合わせたりしたものをまとめました。


EC2


List All Instances

aws ec2 describe-instances


As A Table

aws ec2 describe-instances --output table


Stop/Starting Instances

aws ec2 stop-instances --instance-ids INSTANCE_ID_1 INSTANCE_ID_2 ...

aws ec2 start-instances --instance-ids INSTANCE_ID_1 INSTANCE_ID_2 ...


Resizing Instances

After stopping the target instance

aws ec2 modify-instance-attribute --instance-id INSTANCE_ID --instance-type INSTANCE_TYPE


Describe The Instance

aws ec2 describe-instances --instance-ids INSTANCE_ID_1 INSTANCE_ID_2 ...


Describe The Instance State

aws ec2 describe-instance-status --instances-id INSTANCE_ID


Add A Public IP Address

aws ec2 associate-address --instance-id INSTANCE_ID --public-ip PUBLIC_IP_ADDRESS


List AMIs

aws ec2 describe-images --filters "Name=is-public,Values=false" | jq -c '.Images | sort_by(.CreationDate) | .[] | { id: .ImageId, name: .Name, created_at: .CreationDate }'


Deregister AMI

aws ec2 deregister-image --image-id AMI_ID


Create An Instance From Image

aws ec2 create-image --instance-id INSTANCE_ID --name NAME


List Instances

aws ec2 describe-instances | jq -c '.Reservations[] | .Instances | sort_by(.LaunchTime) | .[] | { instanceId: .InstanceId, state: .State.Name, name: (if(.Tags | length) > 0 then (.Tags[] | select(.Key=="Name") | .Value) else "" end), launchTime: .LaunchTime, publicIp: .PublicIpAddress }'


Filter By Name

aws ec2 describe-instances | jq -c '.Reservations[] | .Instances | sort_by(.LaunchTime) | .[] | { instanceId: .InstanceId, state: .State.Name, name: (if(.Tags | length) > 0 then (.Tags[] | select(.Key=="Name") | .Value) else "" end), launchTime: .LaunchTime, publicIp: .PublicIpAddress } | select(.name | contains("SOME_STRING"))'


Start A Instance From An AMI

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups MySecurityGroup


Display Certain Fields

aws ec2 describe-instances | jq '.Reservations[].Instances[] | {ID: .InstanceId, IP: .PublicIpAddress, PrivateIP: .PrivateIpAddress, Type: .InstanceType, Name: .Tags[].Value, State: .State.Name}'


Security Group


Describe a Security Group

aws ec2 describe-security-groups --group-ids SECURITY_GROUP_ID


Setting an Inbound Rule on the Security Group

aws ec2 authorize-security-group-ingress --group-id SECURITY_GROUP_ID --protocol PROTOCOL --port PORT_NUMBER --cidr IP_RANGE


S3


Show Bucket Size

aws s3api list-objects --bucket BUCKET_NAME --output json --query "sum(Contents[].Size)"


ElastiCache


List ElastiCache Subnet

aws elasticache describe-cache-subnet-groups


Delete ElastiCache Subnet

aws elasticache delete-cache-subnet-group --cache-subnet-group-name SUBNET_NAME


Auto Scaling Groups


List Auto Scaling Group Names and ARNs

aws autoscaling describe-auto-scaling-groups | jq '.AutoScalingGroups | map({ name: .AutoScalingGroupName, arn: .AutoScalingGroupARN, status: .Status })'


Delete an Auto Scaling Group

aws autoscaling delete-auto-scaling-group --auto-scaling-group-name 'YOUR_ASG_NAME'


Elastic Container Registry (ECR)


List Repositories

aws ecr describe-repositories | jq '.repositories[] | { arn: .repositoryArn, uri: .repositoryUri, name: .repositoryName }'


List Images

aws ecr list-images --repository-name YOUR_REPOSITORY_NAME | jq '.imageIds[] | { digest: .imageDigest, tag: .imageTag }'


As TSV

aws ecr list-images --repository-name YOUR_REPOSITORY_NAME | jq -r '.imageIds[] | "\(.imageTag)\t\(.imageDigest)"'