Centralised Logging for serverless applications (Part1)

With the appeal and popularity of serverless applications come unique challenges. One of such challenges is logging, specifically the need for a robust centralised logging for serverless workloads.

Lambda Logging

Lambda is a fantastic service which provides a great platform for application development. It natively integrates with a range of AWS services from a functional perspective such as event source mapping with Kinesis and event based triggers for S3,DynamoDB and CloudWatch Events, as well as also providing operational and monitoring integration with CloudTrail, CloudWatch and X-Ray services.

One the key areas every Lambda developer would be familiar with is how the service handles logging, specifically using CloudWatch logs in which it streams application logs to, directly from the Lambda runtime.

Note: The examples covered in this blog post are written in Python, however the same methodology can be applied to any other runtime supported by Lambda.

Accessing CloudWatch logs for lambda is trivial through the console and with a bit of help can also be streamed in a tail -f fashion using API.

However, once an application reaches certain size and maturity, we need to start considering how multiple components of a serverless application will handle logging, aggregation and visualisation. This is where, we need to start looking at other AWS services such as Kinesis Streams with the support of LogDestination and SubscriptionFilters for addressing these requirements.

Aggregating Logs to a Kinesis Stream

Kinesis Stream provides a sequenced stream of data that retains records for up to 14 days. Kinesis Streams can be read multiple times by multiple consuming applications and acts as a great medium for real-time visualisaiton.

To enable Kinesis as a destination for logs from multiple Lambda functions we need to:

  1. Create a Kinesis Stream to be used as a Log destination
  2. Create an IAM role allowing CloudWatch Logs service
  3. Create a CloudWatch Logs Destination which targets the stream
  4. Assign a CloudWatch Logs Subscription Filter to each CloudWatch Logs Group assign to a Lambda function

ServerlessLogging

As all of the required resources are now supported natively in CloudFormation. I have provided an example implementation for a centralised log destination that you can deploy into your own accounts here

In the next article we will examine, implementation of a log processing function which will read the Kinesis Stream and forward logs to third party log aggregation services.

This blog is maintained by ylit