Introduction
Hosting a static website on Amazon S3 with CloudFront ensures fast, scalable, and cost-effective web delivery. AWS S3 provides secure storage, while CloudFront enhances performance with global content distribution. This guide walks through the step-by-step process to host a static website on AWS S3 and CloudFront.
Step 1: Create an S3 Bucket
- Sign in to the AWS Management Console.
- Navigate to S3 and click Create Bucket.
- Choose a unique bucket name and select the preferred AWS region.
- Uncheck “Block all public access” and acknowledge the warning to allow public access.
- Click Create Bucket.
Step 2: Upload Website Files
- Open the newly created S3 bucket.
- Click Upload and add the website’s HTML, CSS, JavaScript, and other static assets.
- Set public read permissions under Permissions.
- Click Upload.
Step 3: Enable Static Website Hosting
- Inside the S3 bucket, go to the Properties tab.
- Scroll down to Static Website Hosting and click Edit.
- Select Enable and specify the index document (e.g., index.html).
- Click Save Changes.
Step 4: Configure Bucket Policy for Public Access
- Go to the Permissions tab and click Bucket Policy.
- Add the following policy to grant public read access:
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Sid”: “PublicReadGetObject”,
“Effect”: “Allow”,
“Principal”: “*”,
“Action”: “s3:GetObject”,
“Resource”: “arn:aws:s3:::your-bucket-name/*”
}
]
}
- Replace your-bucket-name with the actual S3 bucket name.
- Click Save Changes.
Step 5: Set Up CloudFront for Global Content Delivery
- Navigate to AWS CloudFront and click Create Distribution.
- Under Origin Domain Name, select the previously created S3 bucket.
- Set Origin Access Control (OAC) to restrict S3 access to CloudFront only.
- Enable Compress Objects Automatically for performance optimization.
- Under Default Root Object, enter index.html.
- Click Create Distribution.
Step 6: Update DNS Settings for a Custom Domain (Optional)
- If using a custom domain, configure Amazon Route 53 or another DNS provider.
- Create an A record (Alias) pointing to the CloudFront distribution.
- Verify the domain is correctly mapped to CloudFront.
Step 7: Enable SSL/TLS for Secure HTTPS
- In AWS Certificate Manager (ACM), request a public certificate for the custom domain.
- Validate the domain via email or DNS.
- Associate the certificate with the CloudFront distribution.
- Enable HTTPS redirect in CloudFront settings.
Conclusion
Hosting a static website using AWS S3 and CloudFront ensures high availability, fast load times, and cost efficiency. With global caching, improved security, and seamless scalability, AWS provides an ideal hosting solution for static websites.