A lightweight, production-ready Lambda@Edge function for securing CloudFront with Okta authentication. Optimized for Node.js 22 runtime with ESBuild bundling.
src/
└─ index.js <-- your Lambda@Edge code (optimized)
Minimal dependencies:
# Run this
npm init -y
- josewebtoken → JWT verification
- cookie → Fetch cookie
Install dependencies:
npm install cookie jsonwebtoken
#For production-index.js
npm install cookie jsonwebtoken jwk-to-pem
zip -r okta-auth.zip index.js node_modules ;
Upload okta-auth.zip to AWS Lambda.
- Runtime: Node.js 22.x
- Handler:
index.handler
Attach the Lambda@Edge function to CloudFront events:
- Origin Request → checks authentication before reaching origin
- Viewer Request → faster redirect but slightly more limited
Required:
JWT_SECRETOKTA_CLIENT_IDOKTA_CLIENT_SECRETOKTA_DOMAINOKTA_TIMEOUT_MSAUTH_COOKIE_NAMEAUTH_COOKIE_TTL_SEC
node server.js
# Notes : generate self signed certificate or make changes in index.js for redirect url from https ---> http- Single minified file <1 MB
- Fast execution at CloudFront edge
- Production-ready with secure cookies and JWT validation
- JWKS caching and state validation for security
- Easy to maintain, extend, and debug
- Write your Lambda@Edge logic in
src/index.js - Zip the output →
okta-auth.zip - Upload to AWS Lambda and attach to CloudFront
Created and maintained by: Ravi Kumar Kushwaha