Cloudfront Static Site
S3 is a really basic service that allows us to store files in the cloud. Cloudfront is a more advanced service that allows us to take those files stored in an S3 bucket and distribute them globally.
If our static files exist in a bunch of locations around the world, this will significantly reduce latency when people request our files. On top of that we get a lot more power over cloudfront than we do s3 and we can add custom domain names and ssl certificates. We can also run lightweight serverless functions in cloudfront, but that's a topic for another day.
We are going to host a static website using S3 and Cloudfront.
Here's what we'll do:
- Create another S3 Bucket to store the files
- Create a Cloudfront Distribution to serve the files from the bucket
- Enable website hosting in cloudfront
- Add a dns record to point to the cloudfront distribution
- Setup a certificate to use https
Static Site Requirements
You can use any static site you want for this, it just has to be valid HTML and optionally css, javascript, images, and other assets.
If you wan't, you can use this qr code generator as your static site. Just download the dist.zip folder and unzip it. It contains the index.html
file and other assets. Feel free to clone the react code and play around with the app if you like.
This is different than the steps we went through to host a static website in an S3 bucket. It's just a simple S3 bucket with the files stored it it.
- It doesn't matter what you name the bucket, as long as it's unique
- You don't need to configure any special permissions on the bucket
- You don't need to enable web hosting on the bucket
You can do this through the AWS console, or you can use the AWS CLI. If you run the following script, make sure you change the four variables at the top.