Boost Your Lambdas with Powertools
Boost Python Lambdas with AWS Lambda Powertools (Logger, Tracer, Metrics, & more). See how to deploy with Terraform!
1
2
3
4
5
6
7
8
from aws_lambda_powertools import Logger
logger = Logger()
def lambda_handler(event, context):
logger.info("Processing event")
return {"statusCode": 200}
1
2
3
4
5
6
7
8
from aws_lambda_powertools import Tracer
tracer = Tracer()
def handler(event, context):
# Your business logic here
pass
1
2
3
4
5
6
7
8
9
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit
metrics = Metrics()
def lambda_handler(event, context):
metrics.add_metric(name="SuccessfulBookings", unit=MetricUnit.Count, value=1)
return {"statusCode": 200}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
provider "aws" {
region = "us-east-1"
}
resource "aws_lambda_function" "my_lambda" {
function_name = "MyLambdaFunction"
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
role = aws_iam_role.lambda_exec_role.arn
filename = "path_to_your_lambda_deployment_package.zip"
layers = [
"arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPython:2"
]
}
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_execution_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Principal = {
Service = "lambda.amazonaws.com"
}
Effect = "Allow"
},
]
})
}
output "lambda_arn" {
value = aws_lambda_function.my_lambda.arn
}