We’re excited to announce Epsagon’s new AWS Lambda Extension, furthering the value delivered to teams running modern applications in AWS serverless environments. Lambda Extensions are a new method, released by AWS, that help users to integrate monitoring, observability, security, and governance tools with their Lambda functions.

Extensions are companion processes that expand Lambda functions. They run within the Lambda execution environment, alongside your Lambda function code, enabling extensions to run before/during/after your Lambda function is invoked.

What can you do with AWS Lambda Extensions?

According to AWS:

“Use extensions to integrate your Lambda functions with your preferred monitoring, observability, security, and governance tools”

A more concrete example is how reporting can be run on every function invocation or every function shutdown. You can create an agent that will run on the extension (a separate process), just like in a regular container. You can also communicate with this agent and do some CPU processing in parallel to Lambda execution.

For example, if you send an HTTP request to a remote service in each Lambda invocation, Lambda Extensions enable you to send this request to a local agent that runs inside the extension. Because it’s the same execution environment, it will be faster than sending it to a remote service. Moreover, we can send this request in a batch to this service, so we’ll save a lot of requests to a remote service that would have cost us more money.

Epsagon’s AWS Lambda Extension

At Epsagon, we help you gain visibility into your Lambda functions with distributed tracing, using our lightweight SDK. We’ve developed an extension that runs in parallel to your Lambda function and listens to invocation events. The traces from the Lambda function are being sent to the extension, instead of being sent to a remote server and blocking the client handler. Our extension stores the traces and sends them in parallel to Lambda function executions.

Epsagon AWS Lambda Extensions

Figure 1: Epsagon’s AWS Lambda Extension Solution Diagram

With this, Epsagon enables you to reduce the overhead of distributed tracing and improve performance while still gaining full visibility into your serverless application.

When do AWS Lambda Extensions run?

Before answering this question, let’s take a look at a diagram that shows the lifecycle of an AWS Lambda execution environment:

AWS Lambda execution lifecycle - before lambda extensions

Figure 2: AWS Lambda execution lifecycle – before lambda extensions

As we can see Lambda execution environment has 3 phases:

  • INIT: In this phase, the Lambda service creates a container, which downloads all the dependencies, initializes the runtime, and runs the code outside of the Lambda’s handler. The INIT phase happens before the first Lambda invocation (you may know it as a cold start).
  • INVOKE: In this phase, the Lambda service executes the Lambda function handler. After the handler finishes the execution, the Lambda service freezes the environment (this why you don’t pay extra, but rather per use) and waits some time until another invocation.
  • SHUTDOWN: In this phase of the lifecycle, the Lambda service does not receive any new invokes. It terminates and cleans the runtime, the container, and the whole environment.

The Lifecycle of an AWS Lambda Execution Environment

Figure 3: AWS Lambda execution lifecycle - after lambda extension

Figure 3: AWS Lambda execution lifecycle – after lambda extension

As we can see in the diagram above, the Lambda execution environment has made two changes:

  • INIT: The init phase has been separated into three sub-phases: Extension, Runtime, and Function INIT. The Lambda service creates an ordering where extensions can perform setup tasks before the function’s code is invoked.
  • SHUTDOWN: In this phase of the lifecycle, the Lambda service will send an event of “shutdown” to each extension and let it stop/gently clean itself if needed.

How to Communicate with an Extension

The Lambda service communicates with the function’s runtime using the Runtime API, then communicates with extensions using the Lambda Runtime Extensions API (see explanation below).

AWS Lambda Extensions API

The Lambda Extensions API is a pretty simple HTTP API that consists of four methods:

  • Register:
    • During Extension INIT (See above diagram), every extension should send an HTTP request to register endpoint with the full name of the extension file and, optionally, a list of events (INVOKE, SHUTDOWN) that you want to register to.
  • Next:
    • The extension should send the request to the next endpoint to receive information about the next event (InvokeEvent or ShutdownEvent). The response should be a JSON-based object that contains event data.
  • Init error:
    • The extension uses the init error endpoint to report the Lambda service about initialization error. It should be called when the registration request has failed.
  • Exit error:
    • The extension uses an exit error endpoint to report to the Lambda service about unexpected failures. It should be called when the extension has received an unexpected exception and the request has failed.

Next Steps

About Epsagon

Epsagon enables teams to instantly simplify, visualize, and understand what’s happening within complex microservice architectures. With our comprehensive lightweight auto-instrumentation, users are able to eliminate the gaps in data and manual work associated with other APM solutions, providing significant reductions in issue detection, troubleshooting, and resolution times. Epsagon aggregates, unifies, analyzes, and correlates data from all the third party tools you love, delivering a single pane of glass for understanding containers, Kubernetes, serverless, and more. Engineers now know when something is wrong and can immediately trace issues to root cause before they affect production. Try free for 14 days!

Read More About AWS Lambda Here:

How to Effectively Monitor AWS Lambda

How to Make AWS Lambda Faster: Memory Performance

Troubleshooting AWS Lambda 101

What AWS Lambda Performance Stats Reveal