Building a Secure Static Website with S3, CloudFront, and Route 53
Overview
Hosting a static website on AWS provides several benefits including high availability, scalability, and security. This guide walks through the complete process of setting up a static website with a custom domain and HTTPS support.
Step 1: Create an S3 Bucket for Your Website
First, create an S3 bucket with the same name as your domain:
- Sign in to the AWS Management Console and open the S3 console
- Click Create bucket
- Enter your domain name as the bucket name (e.g.,
demo.example.com) - Select the appropriate region
- Configure other settings as needed
- Click Create bucket
Step 2: Create an IAM User for Content Management
Create a dedicated IAM user with limited permissions for uploading and managing website content:
- Go to the IAM console
- Click Users > Add users
- Enter a name (e.g.,
user-demo.example.com) - Select Access key - Programmatic access
- Create a policy with appropriate S3 permissions or use an existing one
- Complete the user creation process
Step 3: Configure S3 Bucket Policy
Update the S3 bucket policy to allow public read access and specific permissions for your IAM user. Replace <aws-account> with your AWS account ID:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::demo.example.com/*"
},
{
"Sid": "Allow-deployment-To-Bucket",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<aws-account>:user/user-demo.example.com"
},
"Action": [
"s3:ListBucket",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::demo.example.com",
"arn:aws:s3:::demo.example.com/*"
]
}
]
}
Step 4: Enable Static Website Hosting
Configure your S3 bucket to serve as a static website:
- Select your bucket and go to the Properties tab
- Scroll down to Static website hosting and click Edit
- Select Enable
- Enter
index.htmlfor the Index document - Optionally enter
error.htmlfor the Error document - Click Save changes
Record the website endpoint URL that appears, which will look like:
http://demo.example.com.s3-website-{region}.amazonaws.com
Step 5: Upload Website Content
Upload your website files to the S3 bucket:
- Select your bucket and go to the Objects tab
- Click Upload
- Add your website files (HTML, CSS, JavaScript, images, etc.)
- Click Upload
After uploading, test your website by visiting the S3 website endpoint URL from Step 4.
Step 6: Request an SSL Certificate
Use AWS Certificate Manager (ACM) to create a free SSL certificate for your domain:
- Open the AWS Certificate Manager console
- Click Request a certificate
- Select Request a public certificate
- Enter your domain name (e.g.,
demo.example.com) - Select DNS validation as the validation method
- Click Request
- Follow the validation process to verify domain ownership
Step 7: Create a CloudFront Distribution
Set up a CloudFront distribution to serve your website with HTTPS:
- Open the CloudFront console
- Click Create Distribution
- Under Origin Domain, enter your S3 website endpoint
- Configure the following settings:
| Setting | Value |
|---|---|
| Origin Domain Name | demo.example.com.s3-website-{region}.amazonaws.com |
| Viewer Protocol Policy | Redirect HTTP to HTTPS |
| Alternate Domain Names | demo.example.com |
| SSL Certificate | Custom SSL Certificate (select the certificate created in Step 6) |
| Default Root Object | index.html |
- Configure other settings as needed
- Click Create Distribution
Note: It may take 15-30 minutes for the CloudFront distribution to deploy. You can check the status in the CloudFront console.
Content Updates: When you update your website content in S3, you may need to invalidate the CloudFront cache to see the changes immediately. To do this, create an invalidation with the path /*.
Step 8: Configure DNS with Route 53
Connect your domain to the CloudFront distribution:
- Open the Route 53 console
- Select your hosted zone
- Click Create record
- Leave the subdomain field empty (or enter a subdomain if needed)
- Select A - Routes traffic to an IPv4 address and some AWS resources
- Enable Alias
- In the Route traffic to dropdown, select Alias to CloudFront distribution
- Select your CloudFront distribution
- Click Create records
After DNS propagation (which can take up to 48 hours, but often completes within an hour), your website will be accessible via HTTPS at your custom domain (e.g., https://demo.example.com).
Conclusion
You now have a secure static website hosted on AWS with the following benefits:
- High Availability: S3 provides 99.99% availability
- Global Content Delivery: CloudFront serves your content from edge locations worldwide
- HTTPS Security: All traffic is encrypted with your SSL certificate
- Cost-Effective: Pay only for what you use with no upfront costs
- Scalable: Handles any amount of traffic without configuration changes