This repo is used to learn how to deploy a static page to AWS.
Our site will be hosted on S3 and served via CloudFront CDN. You will need external access to your AWS resources so
it's best to setup a new IAM user with AmazonS3FullAccess
and CloudFrontFullAccess
policy only.
- Go to S3 in your AWS account.
- Create a new bucket on S3
- By default your bucket is private and no one can access it so we have to configure it to make it publicly available:
- Open the properties tab, click on Static Website Hosting and check Enable Website Hosting. Also fill in the path to your index and error page (you can do this later after setting up you repo).
- You also have to give
read
access to public otherwise no one could access the bucket although static hosting is enabled. To set the access click on Permissions and Edit bucket policy and fill in the following policy but replaceYOUR_BUCKET_NAME
with - you wouldn't have guessed it - your bucket name
{
"Version": "2012-10-17",
"Id": "Policy1478014846282",
"Statement": [
{
"Sid": "Stmt1478014844834",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
}
]
}
If you put CloudFront in front of S3 you can save a lot of costs and increase your sites response time.
Take in mind that all changes you do on CloudFront take some minutes till they get applied.
- Go to CloudFront in your AWS account
- Create a new
Web
distribution - Configure your distribution:
- Set your bucket as
Origin domain name
. This will link CloudFront and S3 - Set the
index.html
file asDefault Root Object
. This will allow the user to access your domain without any path and still get the index file. - For cheaper pricing only use US, Canada and Europe as
Price Class
- Click on
Create Distribution
to create your CloudFront CDN
- Set your bucket as
- You are now on your distributions dashboard. Select your new distribution
- Go to
Error Pages
to set up some error pages for specific HTTP status codes- Click
Create Custom Error Response
then select404
asHTTP Error Code
and clickYes
onCustomize Error Response
. Set/error.html
asResponse Page Path and
404as
HTTP Response Code`
- Click
- Go to
- In you distributions dashboard you can also see your distributions ID. You need this ID in your travis-ci environment variables.
Optional you can go back to your S3 bucket and disable Static Website Hosting
because CloudFront will do this work.
You should also restrict direct access to your S3 bucket because users should only access your bucket through
CloudFront to prevent high costs. Detailed info can be found in the aws docs
We will setup our repo in a way that all files which get hosted are placed in the www folder. For the sake of
simplicity we place a static index.html
, error.html
and sample.html
file in this folder.
2 files are special: The index and error page. The index page will be send when the user uses your bucket or CloudFront URL and doesn't provide any path or file. The error page will be send when a 4XX error code occurs. Both pages need to be set up on your bucket properties.
To give your user better feedback you should setup extra error pages for specific error codes.
Also place a .travis.yml
file for travis.ci. We will fill in this file in the next paragraph.
Explanation of whats happening:
install
: The install block will download theaws-cli
and add it to thePATH
variablescript
: In the script block, we will use theaws-cli
to first sync all files with S3. The sync command has also the--delete
tag to first delete all files on S3 and then upload the new ones. Next we will enable the CloudFront feature and then invalidate all files.
The script
block can also be used to build your project if you have a javascript app or something else.
Add the following environment variables on travis.ci or as encrypted values:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
CLOUDFRONT_DISTRIBUTION_ID
- Blog article on how to setup S3 on AWS: https://renzo.lucioni.xyz/s3-deployment-with-travis/
- AWS Docs - Restrict access to S3 Content: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
- StackOverflow - Restrict access to S3: http://stackoverflow.com/questions/22668121/creating-an-s3-bucket-policy-that-allows-access-to-cloudfront-but-restricts-acce