Use EventBridge and Lambda to deliver CloudWatch custom metrics on a regular basis

TOC

Use EventBridge and Lambda to deliver CloudWatch custom metrics on a regular basis

There are two ways to push custom metrics to CloudWatch

you can push custom metrics to CloudWatch using the unified CloudWatch agent or the API.

How can I push custom metrics to CloudWatch?

This page uses the API from a Lambda function.
And we will show you how to use EventBridge to deliver CloudWatch custom metrics on a regular basis.

Environment

Diagram of using EventBridge and Lambda to deliver CloudWatch Custom Metrics on a regular basis.

Create a Lambda function.
The function’s action is to push a random number from 0 to 9 as a CloudWatch custom metric.

The EventBridge rule will execute this function periodically.
The execution interval is 1 minute.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation templates are placed at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/123

Explanation of key points of template files

Lambda Function

Resources:
  Function:
    Type: AWS::Lambda::Function
    Properties:
      Architectures:
        - !Ref Architecture
      Environment:
        Variables:
          DIMENSION_NAME: !Ref CloudWatchMetricDimensionName
          DIMENSION_VALUE: !Ref CloudWatchMetricDimensionValue
          METRIC_NAME: !Ref CloudWatchMetricName
          NAMESPACE: !Ref CloudWatchMetricNamespace
      Code:
        ZipFile: |
          import boto3
          import datetime
          import os
          import random

          dimension_name = os.environ['DIMENSION_NAME']
          dimension_value = os.environ['DIMENSION_VALUE']
          metric_name = os.environ['METRIC_NAME']
          namespace = os.environ['NAMESPACE']

          client = boto3.client('cloudwatch')

          def lambda_handler(event, context):
            response = client.put_metric_data(
              Namespace=namespace,
              MetricData=[
                {
                  'MetricName': metric_name,
                  'Dimensions': [
                    {
                      'Name': dimension_name,
                      'Value': dimension_value
                    }
                  ],
                  'Timestamp': datetime.datetime.now(),
                  'Value': random.randint(0, 9),
                  'Unit': 'Count',
                  'StorageResolution': 60
                }
              ]
            )
            print(response)
      FunctionName: !Sub "${Prefix}-function"
      Handler: !Ref Handler
      Runtime: !Ref Runtime
      Role: !GetAtt FunctionRole.Arn
Code language: YAML (yaml)

The code to be executed by the Lambda function in inline format.
For more information, please refer to the following page.

あわせて読みたい
3 parterns to create Lambda with CloudFormation (S3/Inline/Container) 【Creating Lambda with CloudFormation】 When creating a Lambda with CloudFormation, there are three main patterns as follows. Uploading the code to an S3 buc...

The code is to create a client object for CloudWatch in boto3 and then execute the put_metric_data method to deliver custom metrics.

The following is key information for distributing custom metrics.

  • Namespace: test
  • Metric name: randomnum
  • Dimension name: lambda
  • Dimension value: fa-123
  • Value to be delivered: random value between 0 and 9

The following is the IAM role for this function.

Resources:
  FunctionRole:
    Type: AWS::IAM::Role
    DeletionPolicy: Delete
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service:
                - lambda.amazonaws.com
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: CreateThumbnailPolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - cloudwatch:PutMetricData
                Resource: "*"
Code language: YAML (yaml)

Authorize CloudWatch to deliver custom metrics.

EventBridge Rule

Resources:
  Rule:
    Type: AWS::Events::Rule
    Properties:
      Name: !Sub "${Prefix}-EventsRule"
      ScheduleExpression: rate(1 minute)
      State: ENABLED
      Targets:
        - Arn: !Ref FunctionArn
          Id: !Ref Function
Code language: YAML (yaml)

The rule is to execute the aforementioned Lambda function every minute.

When using an EventBridge rule to periodically execute a Lambda function, the behavior is that the EventBridge invokes the Lambda function.
Therefore, it is necessary to authorize EventBridge to invoke the function.

Resources:
  EventsRulePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref Function
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt Rule.Arn
Code language: YAML (yaml)

The following official AWS page was used as a reference for setting up the system.

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-use-resource-based.html#eb-lambda-permissions

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create a CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following pages.

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After reviewing the resources in each stack, information on the main resources created in this case is as follows

  • Lambda Funcition: fa-123-function
  • EventBridge Rule: fa-123-EventRule

Check various resources from the AWS Management Console.

Check the Lambda function.

Detail of Lambda 1.

The function is successfully created.

Check the EventBridge rules.

Detail of EventBridge 1.
Detail of EventBridge 2.

This one is also created successfully.
The contents of the Lambda function is executed every minute.

Operation Check

We are ready.

First, check the execution log of the Lambda function.

Detail of Lambda 2.

The EventBridge rule has automatically executed the Lambda function.
You can see that it has already been executed three times.

Then check CloudWatch metrics.

Detail of CloudWatch 1.

Indeed, we can see that the numbers generated by the Lambda function are delivered as custom metrics.

Thus, using Lambda functions and EventBridge, CloudWatch custom metrics can be delivered on a regular basis.

Summary

We showed you how to use Lambda functions and EventBridge to deliver CloudWatch custom metrics on a regular basis.

TOC