CloudFormation script to set up static site hosting on AWS with S3 and CloudFront
This is a CloudFormation script that helps you host a static website on S3. The root file is set to the index.html on that bucket, so your page will need to have index.html root file or you can update the scripts yourself.
The only thing you need to do is sync your static page directory in the S3 bucket with index.html as the root.
- The only manual step required is to create a hosted zone for your domain on Route53 if you aren't using Route53 for your site already. This is required to host a static page on AWS (Important: Remember to transfer your existing DNS records to Route53)
- Modify files in config as needed. You need to edit
config/config.yml
and change the configuration values as needed. Configuration options are very self-explanatory - Install packages to run the script using pip install
- boto - and configure boto with your AWS keys Boto config
- troposphere - CloudFormation templates are written using troposphere package
- Finally, run the script
python cfn.py --create
-c
or--create
is a flag to note that it's to create the stack and if you need to update it later due to some changes just leave that flag out
- Optional: Manually change the static site configuration on S3 by following these steps
That's it, you are now all set up to host a static site on AWS S3 and CloudFront. The only thing left to do is to transfer your files to the bucket.
This option allows you to choose between having the root domain redirect to a www subdomain or vice versa. For example, if www_to_root is set to "True," requests to www.example.com will be redirected to example.com and if its set to "False" requests to example.com will be redirected to www.example.com.
Depending on the option selected, CloudFront will use different S3 buckets to serve the files. If set to "True" and the main domain is the root domain, it will use that S3 bucket (example.com
bucket). If set to "False" and the www subdomain is the main one, it will use the www.example.com
bucket to serve the files.
Depending on how the www_to_root config optionis set up, files will need to be uploaded either to the example.com
or www.example.com
S3 bucket. You can upload files manually from the AWS console or through a script.