In this guide, we will show you how to protect your static website, hosted on AWS S3, with Basic Authentication
Introduction
This article assumes that you already have:
- AWS S3 Bucket setup
- AWS Cloudfront Distribution
- Linked a Domain to Cloudfront
Create a Lambda Edge Function
Open AWS Console
Navigate to Lambda
Make sure you are in the N. Virginia region
Click “Create function”
A new screen will appear, named “Create function”:
Give the function a name, for this article, “lambda-basic-auth”
Select NodeJS Runtime
Click “Create”
You should now see the newly created function:
In the code section, you should see the following javascript code:
1 | exports.handler = async (event) => { |
In Trust relationship, click “Edit trust policy”, and replace with the following:
1 | { |
- Click deploy, and then Actions - Deploy new Version
- Go to the AWS Cloudfront Console
- Select the Distribution you want to edit.
- Select “Behaviors”
- Scroll down to the section “Function associations”
- Next to the “Viewer request” label, select Lambda@Edge, and fill in the ARN from the section above.
- Wait for the distribution to deploy