Monitoring AWS Lambda Performance with CloudWatch
Introduction
CloudWatch provides metrics and logging for AWS Lambda to monitor performance, errors, and invocations in real time.
Prerequisites
- AWS CLI configured
- Lambda functions deployed
Step 1: Enable Enhanced Metrics
In Lambda console, enable Active Tracing with X-Ray or choose Enhanced Monitoring.
Step 2: Configure CloudWatch Logs
Ensure function has IAM role with logs:CreateLogGroup
and logs:CreateLogStream
permissions.
Step 3: Create CloudWatch Dashboard
aws cloudwatch put-dashboard \
--dashboard-name LambdaDashboard \
--dashboard-body file://dashboard.json
dashboard.json
example:
{
"widgets": [
{
"type": "metric",
"x": 0, "y": 0, "width": 24, "height": 6,
"properties": {
"metrics": [
["AWS/Lambda", "Invocations", "FunctionName", "myLambda"],
[".", "Errors", ".", "."]
],
"period": 60,
"stat": "Sum",
"view": "timeSeries",
"region": "us-east-1"
}
}
]
}
Step 4: Set Alarms
aws cloudwatch put-metric-alarm \
--alarm-name LambdaErrorsAlarm \
--metric-name Errors \
--namespace AWS/Lambda \
--statistic Sum \
--period 300 \
--threshold 1 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions Name=FunctionName,Value=myLambda \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:Alerts
Step 5: Analyze Logs
Use CloudWatch Logs Insights:
defaultView: SELECT @timestamp, @message
| filter @message LIKE "ERROR"
| sort @timestamp desc
| limit 20
Summary
CloudWatch metrics, dashboards, and alarms enable comprehensive monitoring of Lambda performance, ensuring reliability and quick issue detection.