Restric Access to Cloudfront Distribution using Lambda@Edge
Tags: aws, serverless, security
Published: 2022-03-26 06:36:51

Hi folks!
Recently I came across a usecase where I had to restrict access to a website in the UAT environment, so that the iterations and changes to the UAT env are not available for public view. The website was served with the CloudFront CDN, so one of the ways we could restrict access was enforcing authentication on the Cloudfront Distribution using Lambda@Edge.

<h3>How did I achive this?</h3>
Create a Lambda Function
- Go to us-east-1 region as that is the only region we can deploy Lambda@Edge functions.
- Go to the AWS Console and choose Lambda from services.
- Create function and author from scratch.
- Provide function name, and select Node JS 14.x as runtime.
- In Change Default Execution Role, create a new role with basic Lambda permissions.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8flwn1xw7oufgkux5el.png)
 
Boom! Your function is created.
- Use the below code and replace the user and password with your required username and password.
[code](https://gist.githubusercontent.com/njofce/3382b0fe51c59ae9038046cd5087e42a/raw/1b6a1b7c477df4b49a2c2a633265d84b4a6ac846/basic-auth.js)

Source: https://gist.github.com/njofce/3382b0fe51c59ae9038046cd5087e42a

- Deploy the code.
- Go to general configuration in Configuration and change the function timeout to 5sec, that's the max allowed timeout for CDN triggered Lambda Function.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avnmq8n5icoc47784vrp.png) 
- Go to Permissions, and open the IAM role, and update the Trust Relationships with the below json snippet, which allows lambda and lambda@edge to assume the role.

`{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "lambda.amazonaws.com",
                    "edgelambda.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a4fu1uizkquz97wsyh8.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ol8xtgj40xb5quwoa91i.png)
 
- Go to Actions, and under capabilities, select Deploy to Lambda@Edge.
- Configure a new Cloudfront Trigger, select your distribution under Cloudfront event and select a Viewer request, then check include body, and confirm deploy to Lambda@Edge.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/deeo9q5hmiqw9kvdp1nk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56ids55084iewktigdxm.png)
  
It shall take few minutes to enforce the authentication, once done we can see a sign in option as we try to access our UAT env website, provide the username and password you used in the code and access your website. (to not disclose our client, I have used a dummy CloudFront URL with my content)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3dskdvbb44seqms9zwc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o2v2yyzpp8e9i4khf7i.png)  

If you face any challenges please connect and discuss with me on [Linkedin](https://www.linkedin.com/in/rajitpaul/)

Also I'm currently part of the Save Soil Movement initiated by the Isha Foundation, I know you are aware of the conditions of the soil and as a generation we have to turn this around, Become an [Earth Buddy](https://consciousplanet.org/action-now?utm_campaign=cp_vol_mob&utm_medium=social&utm_source=vanity)
Stay Joyful! :)