AWS CLI cheatsheet

# AWS CLI cheatsheet

## configure

### Create profile

```console
% aws configure --profile admin
AWS Access Key ID [None]: xxxxxxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]:

% cat ~/.aws/credentials
[admin]
aws_access_key_id = xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

% cat ~/.aws/config
[profile admin]
region = ap-northeast-1
```

## ec2

### Get description of instances

```console
% aws ec2 describe-instances
```

Get the instance IDs:

```console
% aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'
i-xxxxxxxxxxxxxxxxx
i-yyyyyyyyyyyyyyyyy
```

Get public ip address:

```console
% aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxxxx | jq -r '.Reservations[].Instances[].PublicIpAddress'
192.0.2.1
```

### Start instances

```console
% aws ec2 start-instances --instance-ids INSTANCE_ID [INSTANCE_ID...]
```

### Stop instances

```console
% aws ec2 stop-instances --instance-ids INSTANCE_ID [INSTANCE_ID...]
```

### Check instance status

* InstanceState: reports the instance state such as `running` (16).
* InstanceStatus: reports malfunctions caused by internal issues of the instance.
* SystemStatus: reports malfunctions caused by issues of the system that supports the instance, such as hardware or network problems.

```console
% aws ec2 describe-instance-status --instance-ids i-xxxxxxxxxxxxxxxx
{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "ap-northeast-1a",
            "InstanceId": "i-xxxxxxxxxxxxxxxx",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },
            "InstanceStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "passed"
                    }
                ],
                "Status": "ok"
            },
            "SystemStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "passed"
                    }
                ],
                "Status": "ok"
            }
        }
    ]
}
```

### Change delete-on-termination attribute of EBS

To disable delete-on-termination:

```console
% cat > delete-on-termination.json
[
  {
    "DeviceName": "/dev/xvda",
    "Ebs": {
      "DeleteOnTermination": false
    }
  }
]
% aws ec2 modify-instance-attribute --instance-id i-xxxxxxxxxxxxxxxxx --block-device-mappings file://delete-on-termination.json
```

## s3

### Create bucket

"mb" probably means "make bucket".

```console
% aws s3 mb s3://bucket_name
make_bucket: bucket_name
```

### Upload file

Simple copy:

```console
% aws cp local_path s3://bucket_name
```

Copy multiple files (e.g. copy *.txt, *.jpg):

```console
% aws cp /path/to/source/dir s3://bucket_name --exclude "*" --include "*.txt" --include "*.jpg"
```

Synchronize local dir to s3:
`sync` is different from `cp`. `sync` does not copy already existing and updated files on the destination. So, `sync` is more cost-efficient thatn `cp`. In addition, `sync` can delete files from the destination with `--delete` option, if they are not present on the source.

```console
% aws s3 sync local_path s3://bucket_name [options...]
```

### Download file

Just swap the source path and destination path of the upload command.
Like:

```console
% aws cp s3://bucket_name local_path
```

### List objects

```console
% aws s3 ls s://bucket_name [--recursive]
```

### Generate a pre-signed URL for an object

```console
% aws s3 presign s3://awsexamplebucket/test2.txt [--expires-in <seconds> (default:3600)]
https://awsexamplebucket.s3.amazonaws.com/test2.txt?AWSAccessKeyId=AKIAEXAMPLEACCESSKEY&Signature=EXHCcBe%EXAMPLEKnz3r8O0AgEXAMPLE&Expires=1555531131
```