CloudWatch is AWS’s monitoring service:
# View Lambda function logs
aws logs tail /aws/lambda/<FUNCTION_NAME> \
--region <REGION> \
--follow
# Or view specific logs
aws logs tail /aws/lambda/<FUNCTION_NAME> \
--region <REGION> \
--since 1h
# View API Gateway logs
aws logs tail /aws/apigateway/<API_NAME> \
--region <REGION> \
--follow
# List all log groups
aws logs describe-log-groups \
--region <REGION>
# Or filter by prefix
aws logs describe-log-groups \
--log-group-name-prefix /aws/lambda/lexi-be \
--region <REGION>
# View Lambda invocations
aws cloudwatch get-metric-statistics \
--namespace AWS/Lambda \
--metric-name Invocations \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# View Lambda errors
aws cloudwatch get-metric-statistics \
--namespace AWS/Lambda \
--metric-name Errors \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# View Lambda duration
aws cloudwatch get-metric-statistics \
--namespace AWS/Lambda \
--metric-name Duration \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Average,Maximum \
--region <REGION>
# View API calls
aws cloudwatch get-metric-statistics \
--namespace AWS/ApiGateway \
--metric-name Count \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# View API errors
aws cloudwatch get-metric-statistics \
--namespace AWS/ApiGateway \
--metric-name 4XXError \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# View API latency
aws cloudwatch get-metric-statistics \
--namespace AWS/ApiGateway \
--metric-name Latency \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Average,Maximum \
--region <REGION>
# View read capacity
aws cloudwatch get-metric-statistics \
--namespace AWS/DynamoDB \
--metric-name ConsumedReadCapacityUnits \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# View write capacity
aws cloudwatch get-metric-statistics \
--namespace AWS/DynamoDB \
--metric-name ConsumedWriteCapacityUnits \
--start-time 2026-05-01T00:00:00Z \
--end-time 2026-05-02T00:00:00Z \
--period 3600 \
--statistics Sum \
--region <REGION>
# Create alarm for Lambda errors
aws cloudwatch put-metric-alarm \
--alarm-name lexi-lambda-errors \
--alarm-description "Alert when Lambda errors exceed 5" \
--metric-name Errors \
--namespace AWS/Lambda \
--statistic Sum \
--period 300 \
--threshold 5 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--region <REGION>
# Create alarm for Lambda duration
aws cloudwatch put-metric-alarm \
--alarm-name lexi-lambda-duration \
--alarm-description "Alert when Lambda duration exceeds 5 seconds" \
--metric-name Duration \
--namespace AWS/Lambda \
--statistic Average \
--period 300 \
--threshold 5000 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--region <REGION>
# Create alarm for API errors
aws cloudwatch put-metric-alarm \
--alarm-name lexi-api-errors \
--alarm-description "Alert when API 4XX errors exceed 10" \
--metric-name 4XXError \
--namespace AWS/ApiGateway \
--statistic Sum \
--period 300 \
--threshold 10 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--region <REGION>
# List all alarms
aws cloudwatch describe-alarms \
--region <REGION>
# Or filter by alarm name
aws cloudwatch describe-alarms \
--alarm-name-prefix lexi \
--region <REGION>
# Delete alarm
aws cloudwatch delete-alarms \
--alarm-names <ALARM_NAME> \
--region <REGION>
# Create dashboard
cat > dashboard.json << EOF
{
"widgets": [
{
"type": "metric",
"properties": {
"metrics": [
["AWS/Lambda", "Invocations"],
[".", "Errors"],
[".", "Duration"]
],
"period": 300,
"stat": "Sum",
"region": "<REGION>",
"title": "Lambda Metrics"
}
}
]
}
EOF
aws cloudwatch put-dashboard \
--dashboard-name lexi-dashboard \
--dashboard-body file://dashboard.json \
--region <REGION>
Issue: Logs not appearing
# Check log group
aws logs describe-log-groups \
--log-group-name-prefix /aws/lambda/<FUNCTION_NAME> \
--region <REGION>
# Or check Lambda role
aws iam get-role-policy \
--role-name <LAMBDA_ROLE> \
--policy-name <POLICY_NAME>
Issue: Metrics not appearing
# Check metric namespace
aws cloudwatch list-metrics \
--namespace AWS/Lambda \
--region <REGION>
Continue to Cost Optimization to optimize costs.