Understanding the Lambda Logs API
Tags: aws, serverless, lambda, api
Published: 2021-07-21 15:01:16

---
title: Understanding the Lambda Logs API
published: true
description: Learn about what _AWS Lambda Logs API_ is and what is the use case for it.
tags: aws, serverless, lambda, api
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yufb3dn9j7r0pnj472kr.jpeg
---
In one of my previous blog posts, I've talked about the [_AWS Lambda_ Runtime API](https://dev.to/wojciechmatuszewski/understanding-the-aws-lambda-runtime-api-286n). There we've learned how to create our custom runtimes and interact with the _Runtime API_.

This time, I will focus on the _Lambda Logs API_. We will be looking at how the _Logs API_ fits in the grand scheme of various _AWS Lambda_ APIs and how to use it.

Let's dive in!

## What the Logs API is

In the context of _AWS Lambda_, the _Logs API_ is a publisher of logs produced by [various elements](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-logs-api.html#runtimes-logs-api-msg-function) of the _AWS Lambda execution environment_. The API will push desired logs to a given destination given a subscriber - either an HTTP endpoint or a TCP address.

The _AWS Lambda Logs API_ is usable only from within a registered _extension_. It does not interact with your code in any shape or form as a custom runtime would.

Here is an illustration depicting my mental model of how this API fits in the grand scheme of various _AWS Lambda_ APIs (some parts were omitted for brevity).

![Lambda Logs API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q9951zs66gccpeje59v.png)

## Publisher / subscriber

As I eluded earlier, the relation between your _Lambda extension_ code and the _Lambda Logs API_ is that of a _publisher_ and a _subscriber_.
It's your responsibility to spin up resources (most likely an HTTP server) that listen to incoming requests produced by the _Lambda Logs API_.

> For more information about the subscription options that are available to you, please refer to [the official AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-logs-api.html#runtimes-logs-api-subscribing).

![Logs API and Lambda extension communicating with each other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etwd04s338z9f0bx29gf.png)

Worth noting is the fact that **the _Lambda Logs API_ does not work locally. AFAIK no emulator exists that would allow you to test your implementation locally**. This fact was a bit surprising to me as I could not find such information within the documentation. The AWS-provided [Node.js](https://github.com/aws-samples/aws-lambda-extensions/blob/main/nodejs-example-logs-api-extension/nodejs-example-logs-api-extension/logs-api.js#L20) and [Go](https://github.com/aws-samples/aws-lambda-extensions/blob/main/go-example-logs-api-extension/logsapi/client.go#L140) examples seem to be hinting at this through log messages.

## The main use case

The primary use case that _Lambda Logs API_ enables is relatively hassle-free instrumentation of your lambda functions. Most third-party observability vendors now offer a special _Lambda extension_ that does just that.

Previous implementations included streaming _CloudWatch logs_ contents to the provider for parsing or sending the metrics directly after your code finished its execution. Both of the approaches would result in additional AWS charges not related to the observability offering of your choice - not ideal.

For even more inspiration related to the _Logs API_, checkout the AWS-provided samples. Some examples include [batching the logs and delivering them to S3](https://github.com/aws-samples/aws-lambda-extensions/blob/main/go-example-adaptive-batching-extension/README.md) or a [template to get you started](https://github.com/aws-samples/aws-lambda-extensions/blob/e764ef6efb/go-example-logs-api-extension/README.md).

## Summary

The _Lambda Logs API_ role can be greatly underestimated in the grand scheme of things. But, I find the functionality it provides is valuable for observability purposes, especially for third-party vendors.

I encourage you to give it a try!

You can find me on Twitter - [@wm_matuszewski](https://twitter.com/wm_matuszewski)

Thank you for your time.