A custom Lambda authorizer that validates JWT tokens issued by Auth0 for securing AWS API Gateway endpoints.
The terraform code that deploys this lambda code lives here: https://github.com/Faction-V/gofigure_terraform/tree/main/lambdas/auth0-authorizer
This Lambda function acts as a custom authorizer for AWS API Gateway, validating JWT tokens issued by Auth0. It verifies the token's signature, expiration, audience, and issuer claims before allowing access to protected API endpoints.
- JWT token validation using Auth0 JWKS (JSON Web Key Set)
- Token caching for improved performance
- Detailed error handling and logging
- Sentry integration for error tracking
- Containerized deployment using Docker
- Easy local development and testing
The authorizer follows this flow:
- API Gateway receives a request with an Authorization header
- The Lambda authorizer extracts the JWT token from the header
- It fetches the JWKS from Auth0 (with caching)
- It verifies the token signature, expiration, audience, and issuer
- If valid, it generates an IAM policy allowing access to the requested resource
- It passes user claims (scope, userId, email) to the API as context
- Python 3.12+
- Docker
- AWS CLI configured
- Auth0 account with an API configured
- Just command runner (optional, but recommended)
The following environment variables must be set:
Variable | Description |
---|---|
AUTH0_DOMAIN |
Your Auth0 domain (e.g., your-tenant.auth0.com ) |
AUTH0_AUDIENCE |
The API identifier in Auth0 |
AUTH0_JWKS_URI |
The JWKS URI (typically https://your-tenant.auth0.com/.well-known/jwks.json ) |
AWS_REGION |
AWS region for deployment (default: us-east-1 ) |
ENVIRONMENT |
Environment name for Sentry (default: dev ) |
IMAGE_TAG |
Docker image tag for deployment |
- Clone the repository
- Install dependencies:
poetry install
Build and run the Docker container:
just build-and-run
Or step by step:
just build
just run
Test the authorizer:
just ping
Access the container shell:
just ssh
Stop the container:
just stop
-
Create an ECR repository (if it doesn't exist):
just create-ecr-repository
-
Build the Docker image for ECR:
just build-for-ecr
-
Push the image to ECR:
just push-to-ecr
Or do both in one step:
just build-and-push
After pushing the image to ECR, you can create or update your Lambda function to use this image. This can be done through the AWS Console or using infrastructure as code tools like AWS CDK, CloudFormation, or Terraform.
Run just
to see all available commands. Here are some key commands:
just up
: Start all services with Docker Composejust down
: Stop and remove all containersjust rebuild
: Rebuild Docker imagesjust build
: Build the Docker imagejust run
: Run the Docker containerjust ping
: Test the Lambda functionjust build-and-push
: Build and push the Docker image to ECRjust list-ecr-images
: List images in the ECR repositoryjust open-ecr-repository
: Open the ECR repository in the browser
auth0-authorizer/
├── app.py # Main Lambda handler
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker configuration
├── justfile # Just commands
├── poetry.lock # Poetry lock file
├── pyproject.toml # Project dependencies
├── README.md # This file
└── utils/ # Utility functions
└── __init__.py # Sentry setup and utilities
aws-lambda-powertools
: Utilities for AWS Lambda functionspython-jose
: JWT token validationsentry-sdk
: Error tracking and monitoringboto3
: AWS SDK for Python
If you encounter issues with the authorizer:
- Check that all environment variables are correctly set
- Verify that your Auth0 API is properly configured
- Ensure the JWT token is correctly formatted and not expired
- Check Sentry for detailed error reports
- Review CloudWatch logs for the Lambda function