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.
- You can't use
--region
by itself for regions not inus-east-1
; you also need theLocationConstraint
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
- If necessary, customize the target files for
index-document
anderror-document
- Replace the
example.com
bucket name
aws s3 website s3://example.com --index-document index.html --error-document index.html
- 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
- We will duplicate the original master policy as a new file named
s3.json
- In the
sed
command below, customize theexample.com
bucket name:
sed 's/YOURBUCKETNAME/example.com/g' ~/aws-policies/s3-bucket-public-read.json > s3.json
- 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
- Replace
example.com
below with the name of your S3 bucket aws sync
shares the same options as the Unixrsync
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
- 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 /\\*" }
- 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
S3 documentation
- https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html
- https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html
- https://docs.aws.amazon.com/AmazonS3/latest/dev/delete-or-empty-bucket.html#empty-bucket-awscli
- https://docs.aws.amazon.com/cli/latest/reference/s3/website.html
- https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-policy.html
- https://stackoverflow.com/questions/39466716/how-do-i-get-the-aws-s3-website-endpoint-url-through-the-api
CloudFront documentation