This repo accompanies the blog post.
In that blog post a solution is explained, that puts Cognito authentication in front of (S3) downloads from CloudFront, using Lambda@Edge. JWT's are transferred using cookies to make authorization transparent to clients.
The sources in this repo implement that solution.
The purpose of this sample code is to demonstrate how Lambda@Edge can be used to implement authorization, with Cognito as identity provider (IDP). Please treat the code as an illustration––thoroughly review it and adapt it to your needs, if you want to use it for serious things.
This repo contains (a.o.) the following files and directories:
Lambda@Edge functions in src/lambda-edge:
- check-auth: Lambda@Edge function that checks each incoming request for valid JWT's in the request cookies
- parse-auth: Lambda@Edge function that handles the redirect from the Cognito hosted UI, after the user signed in
- refresh-auth: Lambda@Edge function that handles JWT refresh requests
- sign-out: Lambda@Edge function that handles sign-out
- http-headers: Lambda@Edge function that sets HTTP security headers (as good practice)
CloudFormation custom resources in src/cfn-custom-resources:
- react-app: A sample React app that is protected by the solution. It uses AWS Amplify Framework to read the JWT's from cookies. The directory also contains a Lambda function that implements a CloudFormation custom resource to build the React app and upload it to S3
- user-pool-client: Lambda function that implements a CloudFormation custom resource to update the User Pool client with OAuth config
- user-pool-domain: Lambda function that implements a CloudFormation custom resource to update the User Pool with a domain for the hosted UI
- lambda-code-update: Lambda function that implements a CloudFormation custom resource to inject configuration into the lambda@Edge functions and publish versions
- shared: Utility functions used by several Lambda@Edge functions
Other files and directories:
- ./example-serverless-app-reuse: Contains an example SAM template that shows how to reuse this application from the Serverless Application Repository in your own SAM templates.
- ./template.yaml: The SAM template that comprises the solution
- ./webpack.config.js: Webpack config for the Lambda@Edge functions and for the React-app custom resource
- ./tsconfig.json: TypeScript configuration for this project
The solution can be deployed with a few clicks through the Serverless Application Repository.
- Download and install Node.js
- Download and install AWS SAM CLI
- Of course you need an AWS account and necessary permissions to create resources in it. Make sure your AWS credentials can be found during deployment, e.g. by making your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY available as environment variables.
- You need an existing S3 bucket to use for the SAM deployment. Create an empty bucket.
NOTE: Deploy this application to region us-east-1. This is because Lambda@Edge must be deployed to us-east-1 as it is a global configuration.
- Clone this repo
git clone https://github.com/aws-samples/cloudfront-authorization-at-edge
- Install dependencies:
npm install
- TypeScript compile and run Webpack:
npm run build
- Run SAM build. Use a container to support binaries:
sam build --use-container
- Run SAM package:
sam package --output-template-file packaged.yaml --s3-bucket <Your SAM bucket> --region us-east-1
- Run SAM deploy:
sam deploy --template-file packaged.yaml --stack-name <Your Stack Name> --capabilities CAPABILITY_IAM --parameter-overrides EmailAddress=<your email> --region us-east-1
Providing an email address (as above in step 6) is optional. If you provide it, a user will be created in the Cognito User Pool that you can sign-in with.
See ./example-serverless-app-reuse
Deploy the solution while setting parameter CreateCloudFrontDistribution
to false
. This way, only the Lambda@Edge functions will de deployed in your account, including a User Pool and Client. Then you can wire those Lambda@Edge functions up into your own CloudFront distribution.
The CloudFormation Stack's Outputs contain the Lambda Version ARNs that you can refer to in your CloudFront distribution.
When following this route, also provide parameter AlternateDomainNames
upon deploying, so the correct redirect URL's can be configured for you in the Cognito User Pool Client.
If you want to contribute, please read CONTRIBUTING, and note the hints below.
The sources that are not webpacked but rather run through sam build
should have their dependencies listed in their own package.json files––to make sam build
work properly.
For the sources that are webpacked this doesn't matter.
This sample code is made available under a modified MIT license. See the LICENSE file.