Skip to content

Latest commit

 

History

History
115 lines (94 loc) · 5.32 KB

AWS-CLI-Cheatsheet.md

File metadata and controls

115 lines (94 loc) · 5.32 KB

AWS CLI Cheatsheet

Listed below are example CLI commands you can use to "copy, paste, configure, then commit the command". The fussy options are listed as a starting template for you to quickly configure Amazon Web Services without using the web-based Management Console.

Requirements

Index of CLI commands in this guide


Create a new public bucket

1. Set public permissions, set region

  • You can't use --region by itself for regions not in us-east-1; you also need the LocationConstraint option.
  • Replace example.com below with the name of your S3 bucket.
# For us-east-1
aws s3api create-bucket --bucket example.com --acl public-read --region us-east-1

# For regions not in us-east-1
aws s3api create-bucket --bucket example.com --acl public-read --region us-west-1 --create-bucket-configuration LocationConstraint=us-west-1

2. Enable Static Website Hosting, set defaults for the Index and Error objects

  • If necessary, customize the target files for index-document and error-document
  • Replace the example.com bucket name
aws s3 website s3://example.com --index-document index.html --error-document index.html

3. Get public-read policy and save it to home folder

  • Downloading of the .json template only needs to be done once
  • The master .json file will be will be saved to: ~/aws-policies/s3/
curl -o ~/aws-policies/s3-bucket-public-read.json --create-dirs https://raw.githubusercontent.com/spiritphyz/aws-policies/master/s3/s3-bucket-public-read.json

4. Customize the policy, change bucket name to 'example.com'

  • We will duplicate the original master policy as a new file named s3.json
  • In the sed command below, customize the example.com bucket name:
sed 's/YOURBUCKETNAME/example.com/g' ~/aws-policies/s3-bucket-public-read.json > s3.json

5. Add bucket policy to S3

  • We will use the customized s3.json in Step 4 for the bucket permission rules
  • Replace the example.com bucket name:
aws s3api put-bucket-policy --bucket example.com --policy file://s3.json

Sync to S3

  • Replace example.com below with the name of your S3 bucket
  • aws sync shares the same options as the Unix rsync tool
# Sync contents of directory "public" to destination bucket "example.com"
aws s3 sync --delete --exclude '*.DS_Store' public/ s3://example.com

# Sync to S3 using alternate profile.
# See the guide "Setting-Up-IAM-Identities-for-CLI.md" for setup.
aws s3 --profile qateam sync --delete public/ s3://example.com

Create a CloudFront invalidation

  • Replace the distribution ID below
# Typing command directly into terminal:
aws cloudfront create-invalidation --distribution-id D0DISTRBID000D --paths /\*

# As part of a JSON file (like package.json), you need to escape the backslash character:
"scripts": { "invalidate:cf": "aws cloudfront create-invalidation --distribution-id D0DISTRBID000D --paths /\\*" }

Delete a bucket

  • All objects in the bucket must be deleted before the bucket can be deleted
  • Replace example.com below with the name of your bucket
  • The region parameter is optional
# First, remove all bucket objects
aws s3 rm s3://example.com --recursive

# Then remove the bucket
aws s3api delete-bucket --bucket example.com --region us-west-1

Resources

S3 documentation

CloudFront documentation