Set up alerting on a service
This guide walks through adding alerting to a service using the centralised alert topics — the current, go-forward approach. It uses services/schedule as a worked example.
The shape is always the same:
- Every Lambda gets a log group, declared alongside it.
- (Log-based alerts only) a metric filter turns a log pattern in that log group into a CloudWatch metric.
- A CloudWatch alarm watches that metric (or a built-in AWS metric).
- The alarm publishes directly to one or more of the centralised SNS topics —
${Environment}-critical-alerts,${Environment}-error-alerts,${Environment}-warning-alerts— which already exist in every environment and are already wired to the matching Slack channel via AWS Chatbot.
Your service never creates its own alert topic and never touches infrastructure/alerts/. For guidance on which level (critical/error/warning) to pick, and on naming conventions, see the Monitoring and Alerting standard.
1. Log group
Each Lambda needs a AWS::Logs::LogGroup resource in application.yml — both so a metric filter (step 2) has something to attach to, and so the log group is retained under DeletionPolicy if the stack is ever deleted:
# services/schedule/application.yml
ScheduleQuerierFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
DependsOn: ScheduleQuerierFunction
Properties:
LogGroupName: !Sub /aws/lambda/${ScheduleQuerierFunction}
RetentionInDays: 30
Tags:
- Key: "Project"
Value: "luna"
- Key: "Environment"
Value: !Ref Environment
- Key: "Service"
Value: "schedule"For a new service or a new Lambda, this is all you need — CloudFormation creates the log group as part of the same deploy as the function.
Retrofitting alerting onto an already-deployed function
If the function has been running in production without this LogGroup resource, its log group already exists outside CloudFormation — declaring the resource as above would try to create a group that's already there and fail. In that case, import the existing log group into the stack first via a CloudFormation create-change-set --change-set-type IMPORT (see the CloudFormation import docs), then add the metric filter and alarm as normal. This is the exception, not the default path — most services adding alerting today are adding a LogGroup for the first time alongside the function.
2. Metric filter (log-based alerts only)
Skip this step if you're alarming on a built-in AWS metric (Lambda duration, memory, etc.) — go straight to Alarms.
For alerts driven by something appearing in the logs, add a AWS::Logs::MetricFilter per condition you want to detect, attached to the log group from step 1. schedule defines two per Lambda — one for errors, one for timeouts:
# services/schedule/application.yml
ScheduleQuerierErrorLogCountMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
FilterName: !Sub ${ScheduleQuerierFunction}-errorlogcount-metricfilter
FilterPattern: ERROR
LogGroupName: !Ref ScheduleQuerierFunctionLogGroup
MetricTransformations:
- MetricName: !Sub ${ScheduleQuerierFunction}-errorlogcount
MetricNamespace: lambda/logs/errors
MetricValue: '1'
DefaultValue: 0
ScheduleQuerierTimeoutLogCountMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
FilterName: !Sub ${ScheduleQuerierFunction}-timeoutlogcount-metricfilter
FilterPattern: "timed out"
LogGroupName: !Ref ScheduleQuerierFunctionLogGroup
MetricTransformations:
- MetricName: !Sub ${ScheduleQuerierFunction}-timeoutlogcount
MetricNamespace: lambda/logs/errors
MetricValue: '1'
DefaultValue: 0MetricNamespace is conventionally lambda/logs/errors for log-derived metrics — it must match the Namespace you use on the alarm in step 3.
Before deploying, verify the FilterPattern actually matches by running it against CloudWatch Logs Insights on the function's log group.
3. Alarms
Alarms publish straight to the centralised topics — no Fn::ImportValue, no parameter, no per-service topic. Choose the topic(s) based on the alert level (see the standard's alert levels).
Log-based alarm, built on the metric filter above — the schedule querier's error count alarm publishes to both error-alerts and critical-alerts, since repeated errors on this function are treated as business-critical:
# services/schedule/application.yml
ScheduleQuerierErrorLogCountAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub ${ScheduleQuerierFunction}-errorlogcount-alarm
AlarmDescription: 'Alarm on the schedule querier function error log count'
MetricName: !Sub ${ScheduleQuerierFunction}-errorlogcount
Namespace: lambda/logs/errors
ComparisonOperator: GreaterThanOrEqualToThreshold
Statistic: Sum
Threshold: 1
EvaluationPeriods: 1
Period: 60
TreatMissingData: notBreaching
AlarmActions:
- !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${Environment}-error-alerts'
- !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${Environment}-critical-alerts'The timeout count alarm on the same function is judged less severe, so it only publishes to error-alerts:
ScheduleQuerierTimeoutLogCountAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub ${ScheduleQuerierFunction}-timeoutlogcount-alarm
AlarmDescription: 'Alarm on the schedule querier function timeout log count'
MetricName: !Sub ${ScheduleQuerierFunction}-timeoutlogcount
Namespace: lambda/logs/errors
ComparisonOperator: GreaterThanOrEqualToThreshold
Statistic: Sum
Threshold: 1
EvaluationPeriods: 1
Period: 60
TreatMissingData: notBreaching
AlarmActions:
- !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${Environment}-error-alerts'Namespace must match the MetricNamespace on the metric filter, and MetricName must match the filter's MetricName.
Metric-based alarm, no filter needed — the schedule querier's memory alarm watches the Lambda Insights memory_utilization metric directly and publishes to warning-alerts, since high memory alone doesn't mean the function is failing:
ScheduleQuerierMemoryAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub ${ScheduleQuerierFunction}-memory-alarm
AlarmDescription: 'Alarm on the schedule querier function memory utilisation'
MetricName: memory_utilization
Namespace: LambdaInsights
Dimensions:
- Name: function_name
Value: !Ref ScheduleQuerierFunction
ComparisonOperator: GreaterThanThreshold
Statistic: Average
Threshold: 95
EvaluationPeriods: 1
Period: 900
TreatMissingData: ignore
AlarmActions:
- !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${Environment}-warning-alerts'schedule follows the same pattern for a duration alarm (AWS/Lambda / Duration, also to warning-alerts) on each of its three Lambdas (querier, commands, subscriber) — see services/schedule/application.yml for the full set.
Picking how many topics an alarm publishes to, and which ones, is a business-criticality judgement call — see the standard's alert levels for the critical/error/warning definitions and SLAs.
You can use the CloudWatch console to visualise a metric's recent history before settling on a threshold. For the full set of alarm properties available, see the CloudFormation docs.
What you don't need to do
- Create a per-service or per-alarm SNS topic — the centralised topics already exist in every environment.
- Touch
infrastructure/alerts/application.yml— the topics and their Chatbot → Slack wiring are already provisioned there. - Update any Slack configuration — the centralised topics are already subscribed to the correct channel per environment and alert level.
Deployment
Metric filters, log groups, and alarms all live in your service's own application.yml, so they deploy in the same MR and the same sam deploy as the rest of your service — no separate infrastructure change is required.
