PCG logo
Article

Lambda Powertools: an important criteria when choosing your Lambda language

If you work with AWS LambdasExternal Link regularly, then you should already be using Lambda PowertoolsExternal Link. This library has a really great set of features and utilities, maintained by AWS, and specifically designed and language-optimized for AWS Lambda. This set includes simple features that improve basic Lambda capabilities, such as structured logging and improved tracing, but also more elaborate features normally requiring a lot of boilerplate (i.e., Idempotency - but more on this later).

Due to their popularity, the Lambda Powertools are often mentioned in Lambda related articles, but one feature can quickly become frustrating. Some of the best, most often mentioned features, such as feature flags management only seem to exist in one or two of the Lambda languages (and not always in a production ready state). Comparing the Lambda Powertools documentation for PythonExternal Link, JavaExternal Link, TypescriptExternal Link, and .NetExternal Link, quickly reveals that this is not a rare occurrence, and that the number of features available is extremely dependent on which language you work with.

The goal of this article is to show you how much the features available in the Powertools differ per language, and why it might be judicious to take it into account when deciding what language you will use to develop your Lambdas.

What’s so great about the Powertools?

The Lambda Powertools are designed to make the developer’s life easier by providing:

  • less boilerplate for both simple and more complex features (such as logging)
  • opinionated code optimized for Lambda
  • an easier adoption of AWS best practicesExternal Link
  • a softer learning curve for newcomers to Lambda and Serverless in general
  • for most languages, a set of Lambda layers maintained by AWS

They support a limited number of languages, which currently are PythonExternal Link (2019), JavaExternal Link (2020), TypescriptExternal Link (mid-2020), and .NetExternal Link (2023). The team also plans to add support at some point next year for Go, but there are no official plans for Ruby yet.

To demonstrate what the Powertools can do, let’s have a look at one of the most popular features they offer: the structured logging featureExternal Link.

Structured logging

The standard logs for a Lambda are usually not very detailed, not easy to read or to parse, and lack contextual information.

image-92fecc541a44

By simply adding a couple Powertools decorators in your code, you can make Lambda logs considerably more useful. The below example shows what the content of each log would look like if you configured the Powertools to add almost every detail available, including the original payload.

javascript
Code Copied!copy-button
{
    "cold_start": true,
    "function_arn": "arn:aws:lambda:eu-central-1:11111111111:function:PowertoolsExampleStack-S3LambdaPoweredUp6F7EA-BC4pVVUUc5Dd",
    "function_memory_size": 128,
    "function_name": "PowertoolsExampleStack-S3LambdaPoweredUp6F7EA-BC4pVVUUc5Dd",
    "function_request_id": "68e8db49-65f7-430d-9514-dd2af37ce5d3",
    "level": "INFO",
    "message": "Lambda invocation event",
    "service": "s3Uploader",
    "timestamp": "2023-07-25T15:44:34.777Z",
    "xray_trace_id": "1-64bfede2-43e5e22d5f1b1530bea5ef7",
    "event": {
        "resource": "/upload-powered-up",
        "isBase64Encoded": false,
        [...] // Full API Gateway event details
    }
}

In this log, some fields can be especially useful:

  • cold_start: indicates if this function was subject to a cold start
  • function_request_id can be used to figure out if this execution is a retry of a previously processed payload
  • xray_trace_id: can be used to trace a particular request just by searching the logs and without paying for X-Ray
  • service: this helps identify Lambdas composing a particular Service entity

To configure this, very little change to your code is required. It basically comes down to adding a few decoratorsExternal Link.

This is just an example of how powerful Lambda Powertools can be with very little effort invested. But that’s all I will show here, since the topic of this article is not to give a tutorial on how to use the Powertools. If you want to see more on how it can be configured in a full CDK project, including an integration of the Powertools’ Idempotency featureExternal Link, have a look at my GitHub repositoryExternal Link.

Most common features

Now that you have an idea of the sort of functionality we can achieve with the Lambda Powertools, let’s have a look at some of the most common features that most languages support.

Logging (linkExternal Link)

As demonstrated earlier, this feature provides structured JSON logging with a lot of additional contextual data, including the original event payload, if requested.

Tracing (linkExternal Link)

This feature is a thin wrapper for the AWS X-Ray serviceExternal Link, which provides tracing capabilities. It generates traces in X-Rays with additional metadata and time datapoints to help debugging, such as the cold start, exception messages, and response bodies. It also provides helpers to add your own tracing points and will be disabled automatically if not running inside the Lambda environment (great for local development).

Custom Metrics (linkExternal Link)

This feature uses the CloudWatch Embedded Metric FormatExternal Link to allow asynchronous recording of custom metrics in CloudWatch. The metrics are recorded in the logs, then extracted by CloudWatch as metrics at a much lower cost than creating custom metrics via CloudWatch APIs.

Parameters (linkExternal Link)

Using this feature, you can automatically retrieve parameters stored in the Parameter StoreExternal Link, Secrets ManagerExternal Link, AppConfigExternal Link, or in DynamoDBExternal Link. The parameters will be cached for a configurable amount of time and passed to your handler at runtime. You can even use your own custom parameter store if desired.

Idempotency (linkExternal Link)

Event sources rarely provide exactly-once delivery. This is why most event handlers need to be idempotent, which means they have to make sure that processing the same event multiple times will have the same result, no matter how many times it happens. This can be achieved easily with this feature, which relies on DynamoDB for the storage part of the mechanism.

Feature flags (linkExternal Link)

This utility can be used to configure your code to enable or disable features, either statically (e.g., a feature is not yet ready for production) or dynamically (e.g., per user). Each flag value can be retrieved either from AppConfigExternal Link or from your own custom configurations store.

Streaming (linkExternal Link)

Lambdas usually have a very limited amount of memory, which can be an issue if you need to process very large S3 objects. You can either increase this value (up to 10Gb) and deal with the associated cost, or you can use the Powertools’ streaming utility to process this enormous file a few Mb at a time.

Batch failure processing (linkExternal Link)

When using SQS, Kinesis or DynamoDB streams as an event source with batch processing, if you fail to process one of the events, you have the option to tell those services which event has been successfully handledExternal Link, and which was not. This process can be extremely simplified by using the Batch Processing feature from the Powertools library.

Validation (linkExternal Link)

This utility provides JSON Schema validation for events and responses, including JMESPath support to unwrap events before validation.

Large SQS messages handling (linkExternal Link)

When using SQS, the size of the messages is limited to 256kb. If you need to send a larger event, one of the solutions is to use the AWS SDK to upload the large payload to S3, then to send the reference to this file to SQS. You will then need to download the payload from S3 in the event handler before using it. The Powertools’ utility can be used to automatically retrieve the content of that payload from S3 automatically during the event processing.

Not all languages are equal

All those features are great, but unfortunately, not all features are available for all languages. And even when they are present, they are not always implemented with the same level of support and readiness (prod ready, preview, beta…). This also applies to the features that I previously called “most common.” The table below highlights this disparity in language support.

image-dcca5534693d

It should be pretty obvious that Python and Java are the languages with the most production ready features. This makes sense since they were the first languages added to the Lambda Powertools. You might have also noticed the last row where I mention that Python has a lot more features, and by that I meant that the Python Lambda PowertoolsExternal Link contain at least 7 more unique features than any other language.

This inequality is probably due to the very high difference of activity level between each language repository, which often happens with open source and community supported projects. If you look at the number of PRs, and at the commit dates and frequency on each library’s repository on GitHub, it is clear that Python is by far the most lively repository.

image-e10d2e74ec50

Selecting the language to use for your Lambda usually comes down to the following questions:

  • what language is my team most comfortable with?
  • what language offers me the features/libraries that are most adapted to my use case?
  • how fast does the Lambda startup/execution need to be?
  • are cold starts a concern?

Those are the usual questions, but considering how useful and time saving some of the features of the Lambda Powertools are, it can be a good idea to also include it into the decision process.

Imagine yourself in the situation where:

  • you are building a very simple application processing SQS events referring to large objects stored in S3
  • each large object should be downloaded from S3 and split into parts, sent to EventBridge as an event
  • the logic is simple, but you must make sure that events are not processed twice
  • you want to stream the S3 object content instead of downloading it all in one go (to avoid using a lot of memory)
  • your team is very skilled at Typescript, but has also some experience in Python
image-bc1031d21cc0

In this situation it might make sense to choose Python, despite your team having less experience with it. The code will be be relatively simple anyway, but would be even simpler and shorter if you use the idempotency and S3 streaming features from the Python Powertools library.

Conclusion

Lambda Powertools are a very powerful set of utilities which most Lambdas should be using. But you should never assume that a feature is present in a specific language just because you heard about it, or that this feature is production ready. Furthermore, considering the value of this toolset and depending on your particular use case, it might be worth considering the Powertools available features per language when choosing a programming language for your Lambda. You might save yourself a lot of pain and time by switching to a language you are less familiar with, but for which the capabilities that you need are already implemented, tested and optimized.

Keep in mind though that, just like any external dependency, adding the Lambda Powertools to your project will add some constraints on your project. And it might incite you to use a lot of fancy features you might not really need, and that might impact the overall performances of your application. Furthermore, those features are implemented in an opinionated fashion, which might not fit with your way of working, and offer a more or less limited number of configuration options, depending on the feature and language.

Note that since the AWS Lambda Powertools team recently became an official AWS TeamExternal Link, it should provide more official support and help to fix this features disparity in the medium to long term.

I hope this advice will help you in your future projects.

Links


Continue Reading

News
PCG Showcases Cutting-Edge AI Solutions at FAIEMA 2024

PCG presented AI innovations at FAIEMA 2024, featuring document retrieval and road monitoring solutions using AWS Cloud. Speakers included Thanasis Politis and Vasko Donev, along with industry experts.

Learn more
Article
AWS Lambda: Avoid these common pitfalls

It's a great offering to get results quickly, but like any good tool, it needs to be used correctly.

Learn more
Article
Google Cloud report uncovers: GenAI as a driver of growth and success

The study ‘The ROI of Generative AI’ by Google Cloud delivers impressive figures. Find out how organisations around the world benefit from GenAI.

Learn more
Case Study
Sports
How TVB Stuttgart organizes its home games with Asana

With the work management tool, the German handball league benefits from efficient collaboration and increases employee satisfaction.

Learn more
See all

Let's work together

United Kingdom
Arrow Down