
Deploy a Serverless Static Website with Amazon S3, CloudFront & Lambda@Edge.
Build a globally distributed, HTTPS-enabled static website using AWS serverless services while learning how Lambda@Edge extends CloudFront with edge computing.
Introduction
Modern websites don't always require virtual machines or web servers. For websites that primarily serve static content, AWS provides a fully managed, serverless architecture that is highly available, globally distributed, and cost-effective.
In this project, I built a serverless static website using Amazon S3, Amazon CloudFront, and Lambda@Edge. The goal was to understand how these services work together to deliver content securely with low latency while also exploring how code can be executed at AWS edge locations.
By the end of this project, I had:
- Hosted a static website using Amazon S3
- Distributed content globally using CloudFront
- Enabled HTTPS without managing SSL certificates
- Implemented automatic HTTP to HTTPS redirection
- Added a custom HTTP response header using Lambda@Edge
- Learned the deployment lifecycle and cleanup process for Lambda@Edge
Architecture
User
│
HTTPS
│
▼
Amazon CloudFront
│
Lambda@Edge (Viewer Response)
│
▼
Amazon S3 Bucket
index.html / error.html
AWS Services Used
- Amazon S3
- Amazon CloudFront
- AWS Lambda@Edge
- AWS IAM
Why S3?
Amazon S3 provides an inexpensive and highly durable way to host static websites.
For this project, I:
- Created a dedicated S3 bucket
- Uploaded the website files
- Enabled Static Website Hosting
- Configured:
- index.html
- error.html
- Applied a bucket policy to allow public read access
The website was immediately accessible through the S3 website endpoint.
Why CloudFront?
Although S3 can host static websites, CloudFront significantly improves the user experience.
CloudFront provides:
- Global edge caching
- HTTPS support
- Reduced latency
- Automatic HTTP → HTTPS redirection
- Improved scalability
I configured CloudFront with:
- Amazon S3 as the origin
- Redirect HTTP to HTTPS
- GET and HEAD methods
- AWS managed CachingOptimized cache policy
Once deployed, users accessed the website through the CloudFront domain instead of the S3 website endpoint.
Understanding Lambda@Edge
The most interesting part of this project was learning Lambda@Edge.
Unlike standard AWS Lambda, Lambda@Edge executes code at CloudFront edge locations before requests or responses reach users.
For this project, I associated the function with the Viewer Response event.
The function simply added a custom HTTP response header:
export const handler = async (event) => {
const response = event.Records[0].cf.response;
response.headers['x-custom-header'] = [
{
key: 'X-Custom-Header',
value: 'MyServerlessSite'
}
];
return response;
};
Although the functionality is simple, it demonstrates that custom logic is executed at the CloudFront edge before the response reaches the browser.
Verifying Lambda@Edge
Instead of relying only on deployment status, I verified the function by inspecting the HTTP response in Chrome Developer Tools.
The response included:
X-Custom-Header: MyServerlessSite
This confirmed that:
- CloudFront invoked the Lambda function
- The function executed successfully
- The HTTP response was modified before reaching the client
What I Learned
This project helped me better understand several AWS concepts that are often used together in production environments.
Amazon S3
- Static website hosting
- Bucket policies
- Public access configuration
- Website endpoints
Amazon CloudFront
- CDN fundamentals
- Cache behavior
- HTTPS implementation
- Origin configuration
- Cache policies
Lambda@Edge
- Edge computing
- Viewer Response events
- Lambda versioning
- Function associations
- Global deployment model
IAM
- Execution roles
- Trust relationships
- Lambda permissions
Challenges I Faced
One of the biggest learning moments came during the Lambda@Edge deployment.
CloudFront requires Lambda@Edge functions to:
- Be created in us-east-1 (N. Virginia)
- Use a published version
- Have an IAM execution role trusted by both:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
I also discovered that deleting Lambda@Edge functions is different from standard Lambda functions because CloudFront first removes replicated copies from edge locations before allowing deletion.
Understanding these deployment and cleanup requirements gave me a much better appreciation of how globally distributed AWS services operate.
Cost Optimization
This project was intentionally designed to keep AWS costs minimal.
To reduce cost, I:
- Used Amazon S3 instead of EC2
- Selected the CloudFront North America and Europe price class
- Used the default CloudFront SSL certificate
- Avoided Route 53 and AWS WAF
- Deleted all resources immediately after testing
The overall cost remained negligible while still demonstrating multiple AWS services working together.
Key Takeaways
This project demonstrated how several AWS services combine to build a scalable serverless web application.
More importantly, it showed that edge computing isn't limited to large enterprise applications. Even a simple static website can benefit from CloudFront and Lambda@Edge to improve security, performance, and flexibility.
While my Lambda function only added a custom HTTP header, the same approach can be extended to implement:
- Security headers
- URL rewrites
- Authentication
- Geo-based redirects
- A/B testing
- Personalized responses
GitHub Repository
GitHub Repository: https://github.com/swaralichine/aws-projects/tree/main/02-serverless-and-modern-web/01-serverless-static-website-lambda-edge
Final Thoughts
Building this project strengthened my understanding of how AWS serverless services work together beyond basic static website hosting. It also introduced me to edge computing concepts that are increasingly important for modern cloud architectures.
If you're learning AWS, I highly recommend building this project yourself. It combines storage, CDN, IAM, HTTPS, and edge computing into a single hands-on exercise while remaining inexpensive and relatively quick to complete.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article