| theme | background | class | highlighter | lineNumbers | info | drawings | transition | css | ||
|---|---|---|---|---|---|---|---|---|---|---|
seriph |
text-center |
shiki |
false |
## Best Practices for Using Python to Power Serverless Applications
### PyCon 2023 Presentation Materials
|
|
slide-left |
unocss |
PyCon 2023
Press Space for next page
::right::
Dan Furman
Distinguished Engineer
Card Tech, Data
::default::
Brian McNamara
Distinguished Engineer
Retail Bank, Serverless
- Simple is better than complex
- Fast development cycle
- Fantastic scaling
- Lower cost
- API provider
- Streaming data processor
- Machine Learning Inference
- Code is ephemeral
- Code is isolated
- Code is self-contained
- Code scales without intervention
- How do you observe your application (e.g., logs, metrics, performance)?
- How do you test, build, and deploy your application?
- How do you architect your application?
- Should you use Lambda for your application?
- Rules of observability change
- Logs, metrics, and traces all must persist outside of the function
transition: slide-up layout: image-right image: https://source.unsplash.com/collection/94734566/1920x1080
- Structure your logs - JSON is nice
- Set a retention period on your logs
- Log what you need but allow for debugging if necessary
- Emit custom metrics asynchronously using Embedded Metrics Format (EMF)
- Understand the different types of Lambda metrics and the statistics that should be used with each
- Metric math functions are available to you
- Don't just look at the average - look at the edges (p90, p99)
...just use AWS Lambda Powertools
Sample of using AWS Lambda Powertools package for logging1 and metrics collection2
from aws_lambda_powertools import Logger, Metrics
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
metrics = Metrics()
@logger.inject_lambda_context
@metrics.log_metrics
def handler(event: dict, context: LambdaContext) -> str:
logger.info("Collecting payment")
logger.info({"operation": "charge", "charge_id": event["charge_id"]})
metrics.add_metric(name="SuccessfulCharge", unit=MetricUnit.Count, value=event["charge_amount"])
return {
"operation": "charge",
"charge_id": event["charge_id"],
"charge_amount": event["charge_amount"]
}- Use Powertools to seamlessly
- Annotate your functions
- Instrument your packages
- Inject keys for your telemetry
from uuid import uuid4
from aws_lambda_powertools import Tracer
# Automatically instrument calls to external dependencies
tracer = Tracer(patch_modules=["requests"])
@tracer.capture_method
def generate_uuid4() -> str:
# You can apply this at any level to understand what is needed
return str(uuid4())
@tracer.capture_method
def post_payment(user_id: str) -> dict:
tracer.put_annotation(key="user_id", value=user_id)
# Process the payment hereThese examples are nice; but we're at PyCon! Let's do some real code.
Join us at github.com/mcnamarabrian/pycon203


