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

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

## EC2

### List All Instances

```bash
aws ec2 describe-instances
```

#### As A Table

```bash
aws ec2 describe-instances --output table
```

### Stop/Starting Instances

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

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

### Resizing Instances

After stopping the target instance

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

### Describe The Instance

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

### Describe The Instance State

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

### Add A Public IP Address

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

### List AMIs

```bash
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

```bash
aws ec2 deregister-image --image-id AMI_ID
```

### Create An Instance From Image

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

### List Instances

```bash
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
```bash
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

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

### Display Certain Fields

```bash
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

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

### Setting an Inbound Rule on the Security Group

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

## S3

### Show Bucket Size

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

## ElastiCache

### List ElastiCache Subnet

```bash
aws elasticache describe-cache-subnet-groups
```

### Delete ElastiCache Subnet

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

## Auto Scaling Groups

### List Auto Scaling Group Names and ARNs

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

### Delete an Auto Scaling Group

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

## Elastic Container Registry (ECR)

### List Repositories

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

### List Images

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

#### As TSV

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