Publishing Custom Metrics from Lambda

Length: 00:10:50

Lesson Summary:

CloudWatch custom metrics are coded into your applications and can be used to report custom business and operational data. In this lesson, we will learn how to publish custom CloudWatch metric data from a Lambda function, reporting several key performance indicators (KPIs). These data can help us monitor progress toward sales, marketing, and customer service goals.

Install the demo app using the AWS Serverless Application Model. You can find the instructions for installing the AWS SAM CLI here.

Create the Lambda Deployment Package

  1. Run the following command:

    cd shopping-cart-app
  2. Create an S3 bucket in the location where you want to save the packaged code. (If you want to use an existing S3 bucket, skip this step.)

     aws s3 mb s3://123456789012-shopping-cart-app
  3. Create the deployment artifacts with dependencies.

     sam build
  4. Create the Lambda function deployment package by running the following package AWS SAM CLI command at the command prompt:

     sam package \
         --output-template-file packaged.yaml \
         --s3-bucket 123456789012-shopping-cart-app
  5. In the AWS SAM CLI, use the deploy command to deploy all of the resources that you defined in the template.

     sam deploy \
         --template-file packaged.yaml \
         --stack-name shopping-cart-app \
         --capabilities CAPABILITY_IAM

Locate the API Gateway Endpoint URLs

  1. Open the AWS CloudFormation console at
  2. Choose the AWS CloudFormation stack that you created in the preceding step from the list.
  3. Under Outputs, note the API Gateway endpoint URLs.
  4. Browse each one and observe the JSON responses.

Generate Traffic

Using the API Gateway endpoint URLs from the previous step, generate traffic against each of these endpoints.

  1. Run an HTTP testing tool like vegeta to generate traffic to your API gateway endpoints.

  2. Modify URLs.txt to use the endpoint URLs in your account.

  3. Run a test for 60 minutes.

     cat URLS.txt | vegeta attack -duration=60m | tee results.bin | vegeta report

View Custom Metrics

You may view custom metric data while a load test is in progress.

  1. Open the CloudWatch Console at
  2. Navigate to Metrics.
  3. Under All metrics, select ShoppingCartApp.
  4. Select Metrics with no dimensions.
  5. Select ItemsAddedToCart, OrderTotal, and ViewProduct.

This lesson is only available to Linux Academy members.

Sign Up To View This Lesson

Or Log In

Looking For Team Training?

Learn More