Deployment Patterns
Lambda Function
A standalone Lambda function that handles authorization requests directly via API Gateway. Use cases:- Centralized authorization service
- Multi-application policy enforcement
- Microservices architecture with shared authorization
Lambda Extension
Cerbos runs as a Lambda Extension alongside your application code, providing local policy evaluation. Use cases:- Low-latency authorization requirements
- Sidecar pattern for existing Lambda functions
- Isolated policy evaluation per function
Lambda Function Deployment
Prerequisites
- AWS CLI configured
- AWS SAM CLI installed
- Policies prepared in a directory structure
- ARM64 Lambda runtime support
Architecture
The Lambda function deployment uses:- Runtime:
provided.al2(Amazon Linux 2) - Architecture: ARM64
- Memory: 1024 MB (recommended)
- Timeout: 5 seconds
- Handler:
bootstrap
---
auxData:
jwt:
disableVerification: true # Enable if not using signed JWTs
storage:
driver: "disk"
disk:
directory: /opt/policies
Disabling JWT verification is not recommended for production. This makes the system insecure by forcing Cerbos to evaluate policies using potentially tampered data.
# Download Cerbos binary for Lambda
wget https://github.com/cerbos/cerbos/releases/latest/download/cerbos_Linux_arm64.tar.gz
mkdir -p dist
tar -xzf cerbos_Linux_arm64.tar.gz -C dist/
mv dist/cerbos dist/bootstrap
chmod +x dist/bootstrap
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Cerbos Lambda Function
Globals:
Function:
Timeout: 5
Resources:
CerbosConfigLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: cerbos-config
Description: Cerbos config
ContentUri: config-layer/
CompatibleRuntimes:
- provided.al2
- provided.al2023
CompatibleArchitectures:
- arm64
CerbosFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: provided.al2
CodeUri: dist/
Handler: bootstrap
Architectures:
- arm64
MemorySize: 1024
Layers:
- !Ref CerbosConfigLayer
Events:
CheckResources:
Type: HttpApi
Properties:
Path: /api/check/resources
Method: POST
PlanResources:
Type: HttpApi
Properties:
Path: /api/plan/resources
Method: POST
HealthCheck:
Type: HttpApi
Properties:
Path: /
Method: GET
Environment:
Variables:
CERBOS_LOG_LEVEL: info
XDG_CACHE_HOME: /tmp
CERBOS_CONFIG: /opt/.cerbos.yaml
Outputs:
CerbosFunctionAPI:
Description: "API Gateway endpoint URL for Cerbos Function"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
CerbosFunction:
Description: "Cerbos Lambda Function ARN"
Value: !GetAtt CerbosFunction.Arn
CerbosFunctionIamRole:
Description: "IAM Role created for the Cerbos Lambda function"
Value: !GetAtt CerbosFunctionRole.Arn
# Get the API endpoint from SAM output
API_URL=$(sam list stack-outputs --stack-name cerbos-lambda \
--output json | jq -r '.[] | select(.OutputKey=="CerbosFunctionAPI") | .OutputValue')
# Test health endpoint
curl $API_URL/
# Test authorization
curl -X POST "$API_URL/api/check/resources" \
-H "Content-Type: application/json" \
-d '{
"requestId": "test",
"principal": {
"id": "user1",
"roles": ["user"]
},
"resource": {
"kind": "document",
"id": "doc1"
},
"actions": ["view", "edit"]
}'
Lambda Extension Deployment
The Lambda Extension pattern deploys Cerbos as a Lambda Layer that runs alongside your application.Architecture
The extension:- Runs as a separate process in the Lambda execution environment
- Communicates via Unix domain sockets
- Starts before your function handler
- Socket path:
unix:/tmp/cerbos.http.sock(HTTP) andunix:/tmp/cerbos.grpc.sock(gRPC)
cd cerbos/deploy/awslambda/extension
# Create extension directory structure
mkdir -p layer/extensions
# Download Cerbos binary
wget https://github.com/cerbos/cerbos/releases/latest/download/cerbos_Linux_arm64.tar.gz
tar -xzf cerbos_Linux_arm64.tar.gz -C layer/extensions/
chmod +x layer/extensions/cerbos
---
auxData:
jwt:
disableVerification: true
server:
httpListenAddr: "unix:/tmp/cerbos.http.sock"
grpcListenAddr: "unix:/tmp/cerbos.grpc.sock"
storage:
driver: "disk"
disk:
directory: /var/task/policies
# Build the gateway
GOOS=linux GOARCH=arm64 go build -o dist/bootstrap main.go
chmod +x dist/bootstrap
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Cerbos Lambda Extension
Globals:
Function:
Timeout: 5
Resources:
CerbosExtensionLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: cerbos-extension
Description: Cerbos extension layer
ContentUri: ./layer
CompatibleRuntimes:
- provided.al2
- provided.al2023
CompatibleArchitectures:
- arm64
CerbosServerFunctionWithExt:
Type: AWS::Serverless::Function
Properties:
Runtime: provided.al2
CodeUri: dist/
Handler: bootstrap
Architectures:
- arm64
Layers:
- !Ref CerbosExtensionLayer
MemorySize: 1024
Events:
CatchAll:
Type: HttpApi
Properties:
Path: /{proxy+}
Method: ANY
Environment:
Variables:
CERBOS_LOG_LEVEL: info
XDG_CACHE_HOME: /tmp
CERBOS_CONFIG: /var/task/.cerbos.yaml
Outputs:
CerbosServerFunctionAPI:
Description: "API Gateway endpoint URL"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
CerbosServerFunctionWithExt:
Description: "Cerbos Server Function ARN"
Value: !GetAtt CerbosServerFunctionWithExt.Arn
CerbosExtensionLayerArn:
Description: "Cerbos Extension Layer ARN"
Value: !Ref CerbosExtensionLayer
MyAppFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.11
Handler: app.lambda_handler
Layers:
- !Ref CerbosExtensionLayer
Environment:
Variables:
CERBOS_ADDRESS: "unix:/tmp/cerbos.http.sock"
import http.client
import socket
import json
class UnixHTTPConnection(http.client.HTTPConnection):
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect("/tmp/cerbos.http.sock")
def check_authorization(principal, resource, action):
conn = UnixHTTPConnection("localhost")
payload = {
"requestId": "req-123",
"principal": principal,
"resource": resource,
"actions": [action]
}
conn.request("POST", "/api/check/resources",
json.dumps(payload),
{"Content-Type": "application/json"})
response = conn.getresponse()
return json.loads(response.read())
Configuration
Environment Variables
Memory and Timeout
Recommended settings:Policy Storage Locations
IAM Permissions
For S3 policy storage, add IAM permissions:Monitoring and Logging
CloudWatch Logs
Logs are automatically sent to CloudWatch Logs:CloudWatch Metrics
Key Lambda metrics to monitor:- Invocations: Number of authorization requests
- Duration: Response time
- Errors: Failed invocations
- Throttles: Rate limiting events
- ConcurrentExecutions: Concurrent function instances
Custom Metrics
Publish custom metrics for authorization decisions:X-Ray Tracing
Enable X-Ray for distributed tracing:Cost Optimization
Tips for Reducing Costs
- Right-size memory: Start with 1024 MB and adjust based on metrics
- Use ARM64: 20% cost savings compared to x86_64
- Reserved concurrency: Limit concurrent executions to control costs
- Cold start optimization: Keep policies small and use Lambda layers
- Request batching: Batch multiple authorization checks in one request