AWS CLI (S3, EC2, Route 53)


After uploading a bunch of user content to your AWS S3 Bucket, what do you do if you want to download all that information to back it up?

Luckily AWS has a CLI that we can use.

MacOS X

Follow the steps below to install the CLI using curl. Amazon's steps are found here.

curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Configure CLI

Enter your AWS credentials using the following command.

aws configure

S3

Once configured, you can sync it with your local file system.

aws s3 sync s3://mybucket/dir /local/folder

Or the following below for the entire bucket.

aws s3 sync s3://mybucket .

Or you can use the “cp” command. It will need –recursive option to copy the contents of subdirectories.

aws s3 cp s3://myBucket/dir /local/folder --recursive

List all buckets.

aws s3 ls

List bucket's contents.

aws s3 ls s3://mybucket

Copy single object to S3.

aws s3 cp mypic.png s3://mybucket/

Get bucket size.

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

or a more human readable way.

aws s3 ls --summarize --human-readable --recursive s3://bucket-name/

Copy local folder recursively to S3.

aws s3 cp myfolder s3://mybucket/myfolder --recursive

Remove empty bucket.

aws s3 rb s3://bucket-name

Remove entire bucket with all the content.

aws s3 rb s3://bucket-name --force

To create a bucket.

aws s3 mb s3://bucket-name 

Route 53

To save your Route 53 hosted zone.

aws route53 list-resource-record-sets --hosted-zone-id hosted-zone-id > path-to-output-file

To migrate a saved hosted zone to new account.

aws route53 change-resource-record-sets --hosted-zone-id id-of-new-hosted-zone --change-batch file://path-to-file-that-contains-records

To compare new hosted zone to saved zone to verify all details imported correctly.

aws route53 list-resource-record-sets --hosted-zone-id hosted-zone-id > path-to-output-file

When migrating, make sure you add a Comment and a Changes array at the very top. Add an Action and ResourceRecordSet key to each of the records you need created (AWS guide here).

{
    "Comment": "string",
    "Changes": [
        {
            "Action": "CREATE",
            "ResourceRecordSet":{
                "ResourceRecords": [
                    {
                        "Value": "192.0.2.4"
                    }, 
                    {
                        "Value": "192.0.2.5"
                    }, 
                    {
                        "Value": "192.0.2.6"
                    }
                ], 
                "Type": "A", 
                "Name": "route53documentation.com.", 
                "TTL": 300
            }
        },
        {
            "Action": "CREATE",
            "ResourceRecordSet":{
                "AliasTarget": {
                    "HostedZoneId": "Z3BJ6K6RIION7M",
                    "EvaluateTargetHealth": false,
                    "DNSName": "s3-website-us-west-2.amazonaws.com."
            },
                "Type": "A",
                "Name": "www.route53documentation.com."
            }
        }
    ]
}
  • Photo credit PcMag.com