Powertools for AWS Lambda provides application utilities for structured logging, tracing, metrics, and configuration access. This example focuses on the Python library; language implementations do not necessarily expose identical APIs.
A pinned local exercise
Use Python 3.12 in an isolated environment:
python3.12 -m venv .venv
.venv/bin/pip install aws-lambda-powertools==3.3.0 aws-xray-sdk==2.14.0 boto3==1.35.76
Save the following as orders.py. It has no payment side effect and does not call AWS during a local invocation with tracing disabled.
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.metrics import MetricUnit
logger = Logger(service="orders")
metrics = Metrics(namespace="BlogExample", service="orders")
tracer = Tracer(service="orders")
@logger.inject_lambda_context(log_event=False)
@tracer.capture_lambda_handler(capture_response=False)
@metrics.log_metrics
def lambda_handler(event, context):
order_id = event.get("order_id")
if not isinstance(order_id, str) or not order_id:
return {"statusCode": 400, "body": "order_id is required"}
logger.info("Validated order request")
metrics.add_metric(name="ValidatedOrders", unit=MetricUnit.Count, value=1)
return {"statusCode": 200, "body": "Order request validated"}
The tracer decorator instruments the handler; log_metrics flushes collected metrics when the invocation exits. add_metric alone does not publish a complete invocation’s metrics. Tracer and metrics references explain their respective APIs.
For a local smoke check, save this as check_orders.py beside it:
import os
from types import SimpleNamespace
os.environ["POWERTOOLS_TRACE_DISABLED"] = "true"
from orders import lambda_handler
context = SimpleNamespace(function_name="orders-local", memory_limit_in_mb=128,
invoked_function_arn="local", aws_request_id="example")
assert lambda_handler({"order_id": "example-1"}, context)["statusCode"] == 200
assert lambda_handler({}, context)["statusCode"] == 400
Run .venv/bin/python check_orders.py. Expect JSON logs, a metric document for the valid request, and no assertion failure. A local check does not prove CloudWatch ingestion or X-Ray delivery.
Conceptual illustration; labels describe responsibilities, not a deployment specification.
Feature flags use a store
A dictionary is not a substitute for the library’s store interface. In an AWS deployment, configure an AppConfig application, environment, and configuration profile, then pass an AppConfigStore to FeatureFlags and call evaluate(name=..., default=False). Keep identifiers in deployment configuration and use a least-privilege execution role. Feature flags reference.
For a local feature-flag exercise with the same pinned library, implement the store contract explicitly:
from aws_lambda_powertools.utilities.feature_flags import FeatureFlags, StoreProvider
class LocalFlagStore(StoreProvider):
@property
def get_raw_configuration(self):
return {"new_feature": {"default": True}}
def get_configuration(self):
return self.get_raw_configuration
flags = FeatureFlags(store=LocalFlagStore())
assert flags.evaluate(name="new_feature", default=False) is True
assert flags.evaluate(name="missing", default=False) is False
This validates local evaluation behavior. It does not test an AppConfig deployment or its refresh policy.
Configuration is an input, not a response
Fetch a protected parameter for the operation that needs it; do not return its value to the caller or attach it to logs/traces. In Lambda, enable the relevant tracing and logging setup and grant access only to the parameters or AppConfig resources actually used. Use bounded cache lifetimes when configuration changes need to become visible.
After the exercise, remove its local environment if no longer needed. If deploying it, remove the example function and any monitoring/configuration resources created specifically for it when finished.