Using Amazon CloudWatch alarms to monitor AWS Lambda
Tags: aws, awscommunity, monitoring, cloud
Published: 2022-02-18 09:24:33

You want to receive a notification when your [AWS Lambda](https://aws.amazon.com/de/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](https://aws.amazon.com/de/cloudformation/) template at the end of the post.

## Let's start

Besides your Lambda function, you need two other AWS services: [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) und [Amazon SNS](https://aws.amazon.com/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.

```yaml
Type: AWS::SNS::Topic
Properties:
  Subscription:
    - Endpoint: "add valid email address"
      Protocol: email
```

📖 [Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html)

### 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.

```yaml
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](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html)

## 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.

```yaml
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!