You want to receive a notification when your AWS Lambda function does not perform as expected?
In this blog post, I'll show you how to get a notification when your Lambda function hasn't been invoked at least once within a day. You can find the complete AWS CloudFormation template at the end of the post.
Let's start
Besides your Lambda function, you need two other AWS services: Amazon CloudWatch und Amazon SNS.
SNS
Amazon Simple Notification Service is a managed service that provides message delivery from publishers to subscribers.
Since we want an email notification, we set the Protocol
property to email
and put the recipient email address in the Endpoint
property.
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: "add valid email address"
Protocol: email
π Documentation
CloudWatch
Amazon CloudWatch is a monitoring and observability service. You can also add alarms to CloudWatch - let's look at it in detail:
With the AlarmActions
property you create the connection to the previously created SNS topic. The Namespace
property of the metric associated with the alarm must be set to AWS/Lambda
and the Dimensions
to the Lambda function. Other properties allow you to specify when the alarm should be triggered.
In this example, the alarm is triggered if the Lambda function has not been invoked once within one day.
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Invocation alarm for my AWS Lambda
AlarmActions:
- !Ref InvocationAlarmSNSTopic
Namespace: AWS/Lambda
MetricName: Invocations
Dimensions:
- Name: FunctionName
Value: !Ref LambdaFunction
Statistic: Sum
ComparisonOperator: LessThanThreshold
Threshold: 1
EvaluationPeriods: 1
Period: 86400
TreatMissingData: breaching
π Documentation
CloudFormation template
Here you can find the complete AWS CloudFormation template. Update the CloudWatch Dimensions
and the SNS Subscription
- and you're ready to go.
AWSTemplateFormatVersion: '2010-09-09'
Resources:
InvocationAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Invocation alarm for my AWS Lambda
AlarmActions:
- !Ref InvocationAlarmSNSTopic
Namespace: AWS/Lambda
MetricName: Invocations
Dimensions:
- Name: FunctionName
Value: !Ref LambdaFunction
Statistic: Sum
ComparisonOperator: LessThanThreshold
Threshold: 1
EvaluationPeriods: 1
Period: 86400
TreatMissingData: breaching
InvocationAlarmSNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: "add valid email address"
Protocol: email
If you have any kind of feedback, suggestions or ideas - feel free to comment this post!
Top comments (0)