- Login to AWS account
- Go to https://s3.console.aws.amazon.com/
- Click on "Create bucket" button
- Enter a unique bucket name
- Click on "Create bucket" button
- A private S3 bucket should be created successfully.
- Create "index.html" file
<html>My index file</html>
- Go to https://s3.console.aws.amazon.com/
- Select the newly created S3 bucket
- Click on "Upload" button
- Click on "Add files" button
- Select your "index.html" file
- Click on "Upload" >> "Close"
- Go to https://console.aws.amazon.com/cloudfront
- Click on "Create distribution" button
- Under "Origin domain", select your newly created private S3 bucket
- Under "S3 bucket access", select
Yes use OAI (bucket can restrict access to only CloudFront)
- Click on "Create new OAI"
- Accept default entry, or, enter a new name
- Click on "Create" button
- Under "Bucket policy", select
Yes, update the bucket policy
- Under "Viewer protocol policy", select
HTTPS only
- Under "Allowed HTTP methods", select
GET, HEAD
- Under "Settings" > "Price class", select a suitable option
- Under "Settings" > "Default root object", enter
index.html
- Click on "Create distribution" button
- Note down the CloudFront distribution ID
Lambda@Edge functions must be created in us-east-1
region.
-
Go to https://console.aws.amazon.com/lambda/home?region=us-east-1
-
Click on "Create function" button
-
Select
Author from scratch
option -
Enter a function name of your choice
-
Accept the default ruuntime, i.e.
Node.js 14.x
-
Click on "Create function" button
-
Scroll down to "Code source" section
-
Replace the contents of "index.js" file with
exports.handler = (event, context, callback) => { // Get the request and its headers const request = event.Records[0].cf.request; const headers = request.headers; // Specify the username and password to be used const user = 'mirage'; const pw = 'secqation'; // Build a Basic Authentication string const authString = 'Basic ' + new Buffer(user + ':' + pw).toString('base64'); // Challenge for auth if auth credentials are absent or incorrect if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) { const response = { status: '401', statusDescription: 'Unauthorized', body: 'Unauthorized', headers: { 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}] }, }; callback(null, response); } // User has authenticated callback(null, request); };
-
Click on "Deploy" button
-
Click on "Configuration" tab >> "Permissions"
-
Under "Execution role", click on the role name
-
On the IAM role page, click on "Trust relationships" >> "Edit trust relationship"
-
Replace the line
"Service": "lambda.amazonaws.com"
with"Service": ["lambda.amazonaws.com","edgelambda.amazonaws.com"]
-
Click on "Update Trust Policy" button
-
Return to the Lambda@Edge function page
-
Click on "Actions" button
-
Click on
Publish new version
>> "Publish" -
Click on "Add trigger"
-
Under "Trigger configuration", select
CloudFront
-
Under "Configure CloudFront trigger" >> "Distribution", select the correct CloudFront distribution ID
-
Under "CloudFront event", select
Viewer request
-
Select the
Confirm deploy to Lambda@Edge
checkbox -
Click on "Add" button
-
Navigate to https://console.aws.amazon.com/cloudfront
-
Wait until the CloudFront distribution status changes from
Deploying...
toEnabled
-
This process might take a few minutes to complete
-
Once enabled, copy the distribution domain name and paste it in a browser window, e.g. https://d7jlhjzn5jgsm.cloudfront.net
-
You woud be prompted to enter a valid username and password
-
Only after correct credentials are provided, user would be allowed to access the
index.html
page
When not needed anymore, make sure the S3, CloudFront and Lambda@Edge AWS resources are deleted. This will prvent incurring unnecessary costs.
CloudFront
- Select your CloudFront distribution
- Click on "Disable" button
- Once disabled completely, click on "Delete" button
- Click "Delete" in the confirmation box
S3 Bucket
- Select your S3 bucket
- Click on "Empty" button
- Enter
permanently delete
- Click on "Empty" >> "Exit"
- Select your emptied S3 bucket
- Click on "Delete" button
- Enter name of your S3 bucket in the confirmation box
- Click on "Delete bucket" button
Lambda@Edge
- Wait for a few hours
- Select your lambda@edge function
- Click on "Actions" >> "Delete"
- Refer this link
Note: Replicated functions cannot be deleted instantly