AWS Compute Blog
Using AWS Lambda extensions to send logs to custom destinations
You can now send logs from AWS Lambda functions directly to a destination of your choice using AWS Lambda Extensions. Lambda Extensions are a new way for monitoring, observability, security, and governance tools to easily integrate with AWS Lambda. For more information, see “Introducing AWS Lambda Extensions”.
To help you troubleshoot failures in Lambda functions, AWS Lambda automatically captures and streams logs to Amazon CloudWatch Logs. This stream contains the logs that your function code and extensions generate, in addition to logs the Lambda service generates as part of the function invocation.
Previously, to send logs to a custom destination, you typically configure and operate a CloudWatch Log Group subscription. A different Lambda function forwards logs to the destination of your choice.
Logging tools, running as Lambda extensions, can now receive log streams directly from within the Lambda execution environment, and send them to any destination. This makes it even easier for you to use your preferred extensions for diagnostics.
Today, you can use extensions to send logs to Coralogix, Datadog, Honeycomb, Lumigo, New Relic, and Sumo Logic.
Overview
To receive logs, extensions subscribe using the new Lambda Logs API.
The Lambda service then streams the logs directly to the extension. The extension can then process, filter, and route them to any preferred destination. Lambda still sends the logs to CloudWatch Logs.
You deploy extensions, including ones that use the Logs API, as Lambda layers, with the AWS Management Console and AWS Command Line Interface (AWS CLI). You can also use infrastructure as code tools such as AWS CloudFormation, the AWS Serverless Application Model (AWS SAM), Serverless Framework, and Terraform.
Logging extensions from AWS Lambda Ready Partners and AWS Partners available at launch
Today, you can use logging extensions with the following tools:
- The Datadog extension now makes it easier than ever to collect your serverless application logs for visualization, analysis, and archival. Paired with Datadog’s AWS integration, end-to-end distributed tracing, and real-time enhanced AWS Lambda metrics, you can proactively detect and resolve serverless issues at any scale.
- Lumigo provides monitoring and debugging for modern cloud applications. With the open source extension from Lumigo, you can send Lambda function logs directly to an S3 bucket, unlocking new post processing use cases.
- New Relic enables you to efficiently monitor, troubleshoot, and optimize your Lambda functions. New Relic’s extension allows you send your Lambda service platform logs directly to New Relic’s unified observability platform, allowing you to quickly visualize data with minimal latency and cost.
- Coralogix is a log analytics and cloud security platform that empowers thousands of companies to improve security and accelerate software delivery, allowing you to get deep insights without paying for the noise. Coralogix can now read Lambda function logs and metrics directly, without using Cloudwatch or S3, reducing the latency, and cost of observability.
- Honeycomb is a powerful observability tool that helps you debug your entire production app stack. Honeycomb’s extension decreases the overhead, latency, and cost of sending events to the Honeycomb service, while increasing reliability.
- The Sumo Logic extension enables you to get instant visibility into the health and performance of your mission-critical applications using AWS Lambda. With this extension and Sumo Logic’s continuous intelligence platform, you can now ensure that all your Lambda functions are running as expected, by analyzing function, platform, and extension logs to quickly identify and remediate errors and exceptions.
You can also build and use your own logging extensions to integrate your organization’s tooling.
Showing a logging extension to send logs directly to S3
This demo shows an example of using a simple logging extension to send logs to Amazon Simple Storage Service (S3).
To set up the example, visit the GitHub repo and follow the instructions in the README.md file.
The example extension runs a local HTTP endpoint listening for HTTP POST events. Lambda delivers log batches to this endpoint. The example creates an S3 bucket to store the logs. A Lambda function is configured with an environment variable to specify the S3 bucket name. Lambda streams the logs to the extension. The extension copies the logs to the S3 bucket.
The extension uses the Extensions API to register for INVOKE
and SHUTDOWN
events. The extension, using the Logs API, then subscribes to receive platform
and function
logs, but not extension
logs.
As the example is an asynchronous system, logs for one invoke may be processed during the next invocation. Logs for the last invoke may be processed during the SHUTDOWN
event.
Testing the function from the Lambda console, Lambda sends logs to CloudWatch Logs. The logs stream shows logs from the platform, function, and extension.
The logging extension also receives the log stream directly from Lambda, and copies the logs to S3.
Browsing to the S3 bucket, the log files are available.
Downloading the file shows the log lines. The log contains the same platform and function logs, but not the extension logs, as specified during the subscription.
Lambda has sent specific logs directly to the subscribed extension. The extension has then copied them directly to S3.
For more example log extensions, see the Github repository.
How do extensions receive logs?
Extensions start a local listener endpoint to receive the logs using one of the following protocols:
- TCP – Logs are delivered to a TCP port in Newline delimited JSON format (NDJSON).
- HTTP – Logs are delivered to a local HTTP endpoint through
PUT
orPOST
, as an array of records in JSON format.http://sandbox:${PORT}/${PATH}
. The $PATH parameter is optional.
AWS recommends using an HTTP endpoint over TCP because HTTP tracks successful delivery of the log messages to the local endpoint that the extension sets up.
Once the endpoint is running, extensions use the Logs API to subscribe to any of three different logs streams:
- Function logs that are generated by the Lambda function.
- Lambda service platform logs (such as the START, END, and REPORT logs in CloudWatch Logs).
- Extension logs that are generated by extension code.
The Lambda service then sends logs to endpoint subscribers inside of the execution environment only.
Even if an extension subscribes to one or more log streams, Lambda continues to send all logs to CloudWatch.
Performance considerations
Extensions share resources with the function, such as CPU, memory, disk storage, and environment variables. They also share permissions, using the same AWS Identity and Access Management (IAM) role as the function.
Log subscriptions consume memory resources as each subscription opens a new memory buffer to store the logs. This memory usage counts towards memory consumed within the Lambda execution environment.
For more information on resources, security and performance with extensions, see “Introducing AWS Lambda Extensions – In preview”.
What happens if Lambda cannot deliver logs to an extension?
The Lambda service stores logs before sending to CloudWatch Logs and any subscribed extensions. If Lambda cannot deliver logs to the extension, it automatically retries with backoff. If the log subscriber crashes, Lambda restarts the execution environment. The logs extension re-subscribes, and continues to receive logs.
When using an HTTP endpoint, Lambda continues to deliver logs from the last acknowledged delivery. With TCP, the extension may lose logs if an extension or the execution environment fails.
The Lambda service buffers logs in memory before delivery. The buffer size is proportional to the buffering configuration used in the subscription request. If an extension cannot process the incoming logs quickly enough, the buffer fills up. To reduce the likelihood of an out of memory event due to a slow extension, the Lambda service drops records and adds a platform.logsDropped
log record to the affected extension to indicate the number of dropped records.
Pricing
Logging extensions, like other extensions, share the same billing model as Lambda functions. When using Lambda functions with extensions, you pay for requests served and the combined compute time used to run your code and all extensions, in 1 ms increments. To learn more about the billing for extensions, visit the Lambda FAQs page.
Conclusion
Lambda extensions enable you to extend the Lambda service to more easily integrate with your favorite tools for monitoring, observability, security, and governance.
Extensions can now subscribe to receive log streams directly from the Lambda service, in addition to CloudWatch Logs. Today, you can install a number of available logging extensions from AWS Lambda Ready Partners and AWS Partners. Extensions make it easier to use your existing tools with your serverless applications.
To try the S3 demo logging extension, follow the instructions in the README.md file in the GitHub repository.
Extensions are now available in preview in all commercial regions other than the China regions.
For more serverless learning resources, visit https://serverlessland.com.