| Method | Use Case | Token Type | Caching |
|---|---|---|---|
| IAM Authorizer | AWS services, internal APIs | SigV4 | N/A |
| Lambda Authorizer | Custom logic, third-party tokens | Custom/Bearer | ✅ (TTL) |
| Cognito Authorizer | User authentication | JWT | ✅ |
Client → Sign request with SigV4 → API Gateway → Verify IAM permissions
Client → API Gateway → Lambda Authorizer → Generate IAM Policy → Allow/Deny
def handler(event, context):
token = event['authorizationToken'] # Bearer token
# Validate token with third-party IdP
if is_valid(token):
return generate_policy('user', 'Allow', event['methodArn'])
return generate_policy('user', 'Deny', event['methodArn'])
def generate_policy(principal_id, effect, resource):
return {
'principalId': principal_id,
'policyDocument': {
'Version': '2012-10-17',
'Statement': [{
'Action': 'execute-api:Invoke',
'Effect': effect,
'Resource': resource
}]
},
'context': {
'userId': '123',
'role': 'admin'
}
}
$context.authorizer.keyClient → Get JWT from Cognito → API Gateway → Cognito Authorizer → Validate JWT
$context.authorizer.claimsExam Tip: IAM = SigV4, service-to-service. Lambda Authorizer = custom/third-party tokens. Cognito Authorizer = simplest cho Cognito users. Lambda Authorizer có caching → giảm latency.