Use this solution to create a secure static website for your registered domain name. With this solution, your website:
For more information about each of these components, see the Solution details section on this page.
The following diagram shows an overview of how the solution works:
This solution creates an S3 bucket that hosts your static website’s assets. The website is only accessible via CloudFront, not directly from S3.
This solution creates a CloudFront distribution to serve your website to viewers. The distribution is configured with a CloudFront origin access identity to make sure that the website is only accessible via CloudFront, not directly from S3. The distribution is also configured with a CloudFront Response Header Policy that adds security headers to every response.
This solution creates an SSL/TLS certificate in ACM, and attaches it to the CloudFront distribution. This enables the distribution to serve your domain’s website using HTTPS.
The CloudFront Response Header Policy adds security headers to every response served by CloudFront.
The security headers can help mitigate some attacks, as explained in the Amazon CloudFront - Understanding response header policies documentation. Security headers are a group of headers in the web server response that tell web browsers to take extra security precautions. This solution adds the following headers to each response:
For more information, see Mozilla’s web security guidelines.
You must have a registered domain name, such as example.com, and point it to a Route 53 hosted zone in the same AWS account in which you deploy this solution. For more information, see Configuring Amazon Route 53 as your DNS service.
:⚠️ This template can only be deployed in the
us-east-1
region
To deploy the solution, you use AWS CloudFormation. You can use the CloudFormation console, or download the CloudFormation template to deploy it on your own.
Note: You must have IAM permissions to launch CloudFormation templates that create IAM roles, and to create all the AWS resources in the solution. Also, you are responsible for the cost of the AWS services used while running this solution. For more information about costs, see the pricing pages for each AWS service.
To deploy the solution using the CloudFormation console
Click the Launch on AWS button to open the solution in the CloudFormation console.
If necessary, sign in with your AWS account credentials.
You should see a Create stack page, with pre-populated fields that specify the CloudFormation template. Choose the Next button at the bottom of the page.
On the Specify stack details page, enter values for the following fields:
After entering values, choose the Next button.
On the Configure stack options page, you can optionally add tags and other stack options. When finished, choose the Next button.
On the Review page, you must scroll down and check the two boxes in the Capabilities section:
These capabilities allow CloudFormation to create an IAM role that allows access to the stack’s resources, and to name the resources dynamically.
Choose the Create stack button.
Wait for the CloudFormation stack to launch. The stack launches some nested stacks, and can take several minutes to finish. When it’s launched, the Status changes to CREATE_COMPLETE.
After the stack is launched, go to www.example.com to view your website (replace example.com with your domain name). You should see the website’s default content:
To replace the website’s default content with your own
Note: Make sure to choose the bucket with s3bucketroot in its name, not s3bucketlogs. The bucket with s3bucketroot in its name contains the content. The one with s3bucketlogs contains only log files.
To download the CloudFormation template to deploy on your own, for example by using the AWS CLI, go to:
To customize the website with your own content before deploying the solution
Install npm. For more information, go to https://www.npmjs.com/get-npm.
Clone or download this project from https://github.com/awslabs/aws-cloudformation-templates.
Run the following command to package a build artifact.
make package-static
Copy your website content into the www folder.
If you don’t have one already, create an S3 bucket to store the CloudFormation artifacts. To create one, use the following AWS CLI command:
aws s3 mb s3://<S3 bucket name>
Run the following AWS CLI command to package the CloudFormation template. The template uses the AWS Serverless Application Model, so it must be transformed before you can deploy it.
aws --region us-east-1 cloudformation package \
--template-file templates/main.yaml \
--s3-bucket <your S3 bucket name> \
--output-template-file packaged.template
Run the following command to deploy the packaged CloudFormation template to a CloudFormation stack. To optionally deploy the stack with a domain apex skip this section and proceed to [Step 8] below.
aws --region us-east-1 cloudformation deploy \
--stack-name <your CloudFormation stack name> \
--template-file packaged.template \
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides DomainName=<your domain name> SubDomain=<your website subdomain> HostedZoneId=<hosted zone id>
[Optional] Run the following command to deploy the packaged CloudFormation template to a CloudFormation stack with a domain apex.
aws --region us-east-1 cloudformation deploy \
--stack-name <your CloudFormation stack name> \
--template-file packaged.template \
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides DomainName=<your domain name> SubDomain=<your website subdomain> HostedZoneId=<hosted zone id> CreateApex=yes
To change the Response Header Policy of the site:
templates/cloudfront-site.yaml
. Here you can modify any of the headers for Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, X-XSS-Protection, and Referrer-Policy. Contributions are welcome. Please read the code of conduct and the contributing guidelines.
This project is licensed under the Apache-2.0 License.