Boost your lambda serverless power using Powertools
Tags: aws, awslambda, lambda, serverless
Published: 2022-10-09 15:09:30

There are many positive aspects to serverless technology, but how can you truly comprehend what is happening with your AWS Lambda functions in terms of performance, cold start effects, error handling, and tracing?

For the rescue- The AWS Lambda Powertools libraries built to compliant with the Serverless Lens-Well Architected Framework. The [Python version](https://awslabs.github.io/aws-lambda-powertools-python/latest/) of AWS Lambda Powertools was the first to emerge and rapidly achieved broad adoption. Currently AWS Powertools is available for [Java](https://awslabs.github.io/aws-lambda-powertools-java/) and [Typescript-recently launched](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/).

In this post, I'll talk about different patterns available in AWS Powertools to construct metrics, tracing, and standard logging.

**Why do we need AWS Powertools**
The Serverless Lens in AWS Well Architected Tool, [specifically the Operational Excellence (OPS) Pillar] covers the critical topics of observability, troubleshooting, logging & monitoring.
![waf-serverless-lens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h55mt2m7lauqqx7dek2.PNG)
The OPS pillar mainly talks about three components:

- Structured logging

- Metrics and alarms

- Distributed tracing

Instead of investing time to search solution over internet/forums to satisfy best practices regarding afore mentioned components, AWS Lambda Powertools provide an efficient, easy, optimise, readymade recipe to directly adopt into your lambda functions. In this blogpost, I'll show Python based code snippets to show the real power of Powertool library.
AWS Lambda Powertools make this more easily, since we just need to add an library and use a few decorators to provide a good level of observability into our lambdas.

**How to enable/use powertools library**
Add the following layer on Lambda function:
`arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:37`
![lambda-layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvd7pb1rn3qciczvy1nb.PNG)

Alternatively, use the PyPi approach- `pip install aws-lambda-powertools`


**Logging**
Lets first understand the different type of structure logging first:
![logging](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74pb5juvmey3wm0daqkd.PNG)

Normally, we use default logging in the lambda function as follows:
![normal-logging](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a51snbsiarsb0i1zos19.png)

Powertools offer a simple, standardized log formmater, that can be enriched with important informations, simply using decorators:
![powertools-logging](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujuk3qa3ciaefa2mtjfo.png)

Now, we can see a detailed structured logs by just adding 2 lines of code!


**Metrics**
The requirement is to have a metric to count how many Carts have been checked out. All it takes to log metrics is the log_metrics decorator.

![powertool-metric1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtw9d6fqxdnv3yfjrftf.PNG)
![powertool-metric2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gps4ftvt8ju6h3io2ib2.PNG)

With a few lines, we can get one of the most important metrics when working with lambda functions: the cold start metric. The cold start happens every time that your container isn't waked up, and it can became a big problem to your architecture.

**Tracing**
Similar to non-serverless applications, anomalies can occur at larger scale in distributed systems. Due to
the nature of serverless architectures, it’s fundamental to have distributed tracing.
![tracing-sol-arch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqouyz50smj02w2661la.PNG)

Active tracing with AWS X-Ray should be enabled to provide distributed tracing capabilities as well as to
enable visual service maps for faster troubleshooting. X-Ray helps you identify performance degradation
and quickly understand anomalies. 
![enable-tracing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja2055q9nwea9mb0pguv.PNG)

Powertools offer multiple decorators to work with tracing. Please remember, in order to have it operational, we need to have the previous step done (previous article in this tutorial), which means proper setup in the SAM template:

Tracing needs to be enabled for Lambda function: `Tracing: Active`

And variable `POWERTOOLS_SERVICE_NAME` set, like in our example: `POWERTOOLS_SERVICE_NAME: shopping-cart`

```
@tracer.capture_lambda_handler
def lambda_handler(event, context):
    path_params = event["pathParameters"]
    game_id = path_params.get("product_id")

    logger.info("Retrieving product_id%s", product_id)
```
Using @tracer.capture_lambda_handler, we can automatically capture the metadata information about our handler, and see this information in Xray service map.
```
@tracer.capture_method
def get_product(product_id, dynamodb=None):
    table_name = os.getenv("TABLE_NAME")

    if not dynamodb:
        logger.info("Initializing DDB Table %s", table_name)
        dynamodb = boto3.resource('dynamodb')

    table = dynamodb.Table(table_name)
```

![metircs-lpt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2lvxgwwjjnbtcqeq5k0.PNG)

**Logger Tracer Metrics All in one**
Use all three- Logger, Tracer and Metrics component in all of your lambda functions:
![tracing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk5zsnfws2bpxau4chem.PNG)

**Conclusion**
Avoiding the circumstance where each service, or even each Lambda function, takes a slightly different approach to achieving the objectives of a well-architected architecture is the problem in applying best practises for observability. Sure, it's simple to create your own lightweight libraries for monitoring, tracing, and logging, but as your application develops, you'll need more functionality and bug fixes. What was once lightweight might rapidly turn into an unwanted maintenance burden for teams. To assist teams in writing less boilerplate code to fulfil these non-functional needs, Lambda Powertools were created. These concepts formed the foundation of the AWS Lambda Powertools packages, which concentrate on the Well-Architected Framework Serverless Lens.