Skip to content

Different ways to meter a lambda function

Notifications You must be signed in to change notification settings

amberflo/lambda-metering-examples

Repository files navigation

Lambda Metering Examples

This project demonstrates how to meter a lambda function.

Amberflo provides different methods to ingest meter records:

In the context of a running service, in our case an AWS Lambda function, this can be done:

  • during the lambda execution itself, or
  • by subscribing to the lambda logs, or
  • (TODO) from a lambda extension.

In this project, you can see how to implement each of these methods.

Overview

This project uses AWS SAM to create an API that will be served by a lambda function. When this function runs, it causes a meter record to be ingested.

Each ingestion method is triggered by a different call to the API. For instance, POST $API_URL/direct-s3 will cause the ingestion to happen immediately via your designated S3 bucket.

Ingestion Methods

In the direct ingestion methods, the ingestion happens in your main lambda code.

All these methods support the same payload format.

These methods are easier to setup because they require no additional infrastructure, but they will make an HTTP request to the underlying service, so you'll need to handle errors. They'll also increase the running time of your lambda and may require installing additional dependencies. For these reasons, an indirect method is preferable.

In the indirect ingestion methods, your meter records are logged by the main lambda, and another lambda processes the log records, extracts the meter records and ingests them.

This facilitates error handling (you can always retry processing the logs) and keeps your lambda code simple. Although the infrastructure cost is higher (the log subscription and consumer), it does not grow with each new lambda you need to meter but remains constant.

Currently, a CloudWatch log group can have at most 2 subscription filters, so having Kinesis intermediate the ingestion may be useful if you also need the logs for other purposes (i.e. Kinesis allows fanning out).

To read more about the CloudWatch integration, see this doc.

How to Use

Requisites

To run this example you'll need:

Deploying & Testing

Fill in the lambda environment variables in the SAM template file and run the deploy script.

When the deployment is complete, find the API stage URL in the API Gateway console and set it as the api_url in the test script.

Now you can run the test script to cause the lambda to execute and meter events ingested.

In the Amberflo UI, you should now be able to see the usage.

About

Different ways to meter a lambda function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •