Authentication at the Edge with Lambda@Edge
One of the trickier parts of this site is protecting the photo gallery. The site is fully static — there’s no server to check credentials against. So the authentication has to happen at the CDN level.
The Setup
The solution uses three AWS services working together:
- Cognito — manages the user pool and handles Google OAuth
- CloudFront — serves the site and routes requests
- Lambda@Edge — runs on every request to protected paths and checks for a valid JWT
How It Works
When someone requests a gallery image, the request hits CloudFront first. Before CloudFront fetches the image from S3, it triggers a Lambda@Edge function on the viewer-request event: