AWS CLI: Some Useful Commands

Delete an S3 bucket and all its contents with just one command

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

Recursively copy a directory and its subfolders from your PC to Amazon S3

#aws s3 cp MyFolder s3://bucket-name — recursive [–region us-west-2

3. Display subsets of all available ec2 images

#aws ec2 describe-images | grep ubuntu

Warning: this may take a few minutes.

4. List users in a different format

#aws iam list-users –output table

5.  List the sizes of an S3 bucket and its contents

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

6. Move S3 bucket to a different location

aws s3 sync s3://oldbucket s3://newbucket –source-region us-west-1 –region us-west-2

7. List users by ARN

“jq” is like sed for JSON data – you can use it to slice, filter, map, and transform structured data with the same ease that sedawkgrep and friends let you play with non-JSON text.

Armed with that knowledge, we can now nicely list all our users, but only show their ARNs.

aws iam list-users –output json | jq -r .Users[].Arn

Note: jq, might not be installed on your system by default. On Debian-based systems (including Ubuntu), use sudo apt-get install jq


8. List all of your  instances that are currently stopped and the reason for the stop

aws ec2 describe-instances –filters Name=instance-state-name,Values=stopped –region eu-west-1 –output json | jq -r .Reservations[].Instances[].StateReason.Message