A Terraform module to implement Microsoft Entra ID (Azure AD) authentication for CloudFront static websites using Lambda@Edge.
- User requests protected content from CloudFront
- Auth Lambda checks for valid session cookie
- If no valid session, user is redirected to Microsoft login
- User authenticates with Microsoft Entra ID
- Microsoft returns authorization code to callback URL
- Callback Lambda exchanges code for access and ID tokens
- Microsoft returns tokens to Callback Lambda
- Callback Lambda sets secure cookie and redirects to original URL
- Authenticated user receives protected content
- Create app registration in Microsoft Entra ID / Azure AD
- Set redirect URL to
https://your-domain.com/callback - Note the client ID, tenant ID, and create a client secret
Create a secret in AWS Secrets Manager:
{
"tenant": "your-tenant-id",
"client_id": "your-client-id",
"client_secret": "your-client-secret",
"redirect_uri": "https://your-domain.com/callback"
}module "sso_auth" {
source = "path/to/cloudfront-microsoft-sso"
name_prefix = "example"
app_code = "docs"
lambda_runtime = "nodejs18.x"
sso_config_arn = aws_secretsmanager_secret.sso_config.arn
}resource "aws_cloudfront_distribution" "distribution" {
# ... other configuration ...
default_cache_behavior {
# ... other settings ...
lambda_function_association {
event_type = "viewer-request"
lambda_arn = module.sso_auth.authenticator_lambda_arn
include_body = false
}
}
ordered_cache_behavior {
path_pattern = "/callback*"
# ... other settings ...
lambda_function_association {
event_type = "viewer-request"
lambda_arn = module.sso_auth.callback_lambda_arn
include_body = false
}
}
}| Name | Description | Type | Required |
|---|---|---|---|
| name_prefix | Prefix for resource names | string | Yes |
| app_code | Application identifier | string | Yes |
| lambda_runtime | Node.js runtime | string | Yes |
| sso_config_arn | Secret ARN | string | Yes |
| Name | Description |
|---|---|
| authenticator_lambda_arn | Authenticator Lambda ARN |
| callback_lambda_arn | Callback Lambda ARN |
- 403 Errors: Check IAM permissions and Lambda execution role
- Redirect Issues: Verify redirect URI matches exactly across all configurations
- Cookie Problems: Ensure CloudFront distribution and cookie domain match
- HTTP-only secure cookies
- Credentials stored in AWS Secrets Manager
- Edge authentication with Lambda@Edge
- No client-side credential exposure