A Developer's Guide to AWS PowerTools for Python Lambda Functions
INTRO:?
Powertools for AWS Lambda (Python) is a developer toolkit to implement Serverless best practices and increase developer velocity. AWS Powertools is a suite of utilities for AWS Lambda functions to simplify operational best practices, such as structured logging, distributed tracing, and metrics collection. It helps developers achieve best practices and reduce boilerplate code when building serverless applications on AWS. It currently supports AWS Lambda functions written in Python, with support for runtime versions 3.6 and newer.?
KEY FEATURES:?
ARCHITECTURE:?
We will be using the above architecture to understand all the features of AWS Powertools. Here our goal is to send some data from the client to the S3 bucket without losing any data. We will be using the below AWS resources:?
HOW IT WORKS:?
Logger:?
2. Initialize the “Logger” with various parameters?
3. Parameters are to set the values/flags as TRUE or FALSE.??
4. Now after setting all the logs parameter, we can use @logger.inject_lambda_context? to get all the logs from the Lambda_Handler.?
5. There is also a way to handle the exceptions in Logger module. It gives us the two new parameters in the JSON log as “exception” & “exception_name”.?
6. After setting all the parameters and exceptions, we can check the logs from cloud watch?
7. Below you will find the Lambda Function Code.
?
8. The final output of the logger is given below.?
9. The default values for the above parameters are?
?Tracer:?
2. Go into “Configuration” tab and select “monitoring and operation tool” and ENABLE “X-Ray Active tracing”?
?
3. Select layers and configure the default AWSLambda_Powertool_pythonV2 as mentioned below.
?
4. Then start with the code section below.?
领英推荐
USE-CASE:?
2. Import all the libraries required. Set the X-ray Active Trace as “ENABLE”. Add a layer “AWSLambdaPowertoolsPythonV2” into both the lambdas.?
3. Let's come to the first lambda function.??
4. This will send the data to SQS. We can set the trigger for 2nd lambda through this SQS. Like whenever some data comes to this SQS, it will trigger the 2nd lambda .?
5. Now coming to the second lambda.?
6. Set the new log_group that you created in “CloudWatch log group” so that both the lambdas will send their logs to the same log_group.?
7. Run the first lambda and you can go to the cloudwatch logs in the monitoring section. You can even check the X-ray tracing in the monitoring section.?
8. In the cloudwatch logs, you will find both the logs contain a key named “correlation_id” which we can use to check the connectivity between two lambdas.?
9. After all this you can go to the given S3 bucket and you will see the (.json) file.?
REFERENCES:?