Reputation: 1
How can I log Lambda authorizer requests to a separate CloudWatch log group based on the API Gateway stage? My function is acting as a Lambda authorizer for the API
i tried this
def get_stage_log_group(stage_name):
log_group_name = "/aws/lambda/terraform_apigw_LambdaAuthorizer"
log_stream_name = f"{log_group_name}/{stage_name}"
return logging.getLogger(log_stream_name)
# Set up logging
# This will be updated within lambda_handler based on the stage
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
stage = event.get("requestContext", {}).get("stage", "unknown")
# Create a dynamic logger per stage
global logger
logger = get_stage_log_group(stage)
logger.info(f"Using stage-specific log group for stage: {stage}")
Upvotes: 0
Views: 13
Reputation: 743
AWS Lambda automatically sends all logs from a function execution to a single log group (/aws/lambda/<function_name>), and you can't change that log group or create separate log groups or streams per API Gateway stage dynamically from within the function.
However you can add stage information to the same log group and create a filter if required in CloudWatch
# Example log with stage-specific information
logger.info(f"Processing request for stage: {stage}", extra=extra)
Upvotes: 0