aws snippets

How to use the AWS Command Line Interface

Tagged cloudfront, aws, s3  Languages bash

How to use the AWS Command Line Interface Documentation.

AWS CLI doesn't support Cloudfront at the moment, see list of supported services for details.

Install the Amazon AWS command line client

$ pip install awscli
# or
$ sudo easy_install awscli

Now you need to configure the client:

$ aws configure

How to create an S3 bucket

aws s3 mb s3://xxx

How to list S3 buckets

$ aws s3 ls

How to configure a website

$ aws s3 website help

How to configure the index and error documents of a website

aws s3api put-bucket-website --bucket xxx.com --website-configuration '{
    "IndexDocument": {
        "Suffix": "index.html"
    },
    "ErrorDocument": {
        "Key": "404/index.html"
    }
}'

How to set the website policy

aws s3api put-bucket-policy --bucket www.xxx.com --policy '{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AddPerm",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::www.xxx.com/*"
    }
  ]
}'

How to get the website configuration

$ aws s3api get-bucket-website --bucket x.com

How to configure redirection for your website

This creates a redirection from x.com to www.x.com:

aws s3api put-bucket-website --bucket x.com --website-configuration '{
    "RedirectAllRequestsTo": {
        "HostName": "www.x.com"
    }
}'

How to set the error document

$ aws s3 website s3://xxx --index-document index.html --error-document 404/index.html

How to upload a website to S3

$ aws s3 sync ~/xxx s3://xxx --delete --cache-control \"max-age=3600\" --acl public-read --exclude *.txt

Note that the command will: * delete files from S3 that don't exist locally. * set max-age header to 3600 * allow anyone to access the files * exclude *.txt files

How to Create and Configure a Static Website Hosted on Amazon S3

Tagged static, amazon, website, aws, s3  Languages bash

This will create two S3 buckets and configure them for hosting a static website:

aws s3 mb s3://xxx.com
aws s3 mb s3://www.xxx.com

aws s3api put-bucket-website --bucket www.xxx.com --website-configuration '{
    "RedirectAllRequestsTo": {
        "HostName": "xxx.com"
    }
}'

aws s3api put-bucket-website --bucket xxx.com --website-configuration '{
    "IndexDocument": {
        "Suffix": "index.html"
    },
    "ErrorDocument": {
        "Key": "404/index.html"
    }
}'

aws s3api get-bucket-website --bucket www.xxx.com
aws s3api get-bucket-website --bucket xxx.com

Next, allow anyone to access the files:

aws s3api put-bucket-policy --bucket www.xxx.com --policy '{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AddPerm",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::www.xxx.com/*"
    }
  ]
}'

Next, upload your website to S3:

aws s3 sync build s3://www.xxx.com --acl public-read --delete

In the above example all files are located in a folder named build.

Done. Your website can be found at http://.s3.amazonaws.com/index.html.

Note: * if you're serious about hosting your website, use AWS Cloudfront or Cloudflare + S3 * the "www" URL redirects to the "non-www" URL