Skip to content
This repository was archived by the owner on May 12, 2023. It is now read-only.

Latest commit

 

History

History
374 lines (275 loc) · 8.47 KB

File metadata and controls

374 lines (275 loc) · 8.47 KB
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
persist
true
slide-left
unocss

Best Practices for Using Python to Power Serverless Applications

Capital One

PyCon 2023

Press Space for next page

transition: fade-out layout: two-cols

::right::

Dan Furman

Dan's Profile Picture

Distinguished Engineer

Card Tech, Data

::default::

Brian McNamara

Brian's Profile Picture

Distinguished Engineer

Retail Bank, Serverless


transition: fade-out

Why Python for Serverless?

  • Simple is better than complex
  • Fast development cycle
  • Fantastic scaling
  • Lower cost

transition: slide-down

Kinds of Serverless Applications

  • API provider
  • Streaming data processor
  • Machine Learning Inference

transition: slide-down

API Application


transition: slide-down

Streaming Data Application


transition: slide-down

ML Inference Application


The Serverless Mindset

- Code is ephemeral
- Code is isolated
- Code is self-contained
- Code scales without intervention

Key Questions

- 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?

transition: slide-up

How do you know what your Lambda application is doing?

  • 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

Observability Pro Tips

- 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

transition: slide-up

Logging

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"]
    }
<style> .footnotes-sep { @apply mt-20 opacity-10; } .footnotes { @apply text-sm opacity-75; } .footnote-backref { display: none; } </style>

Tracing Pro Tips

  • Use Powertools to seamlessly
    • Annotate your functions
    • Instrument your packages
    • Inject keys for your telemetry

Trace your code

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 here

Live Code

These examples are nice; but we're at PyCon! Let's do some real code.

Join us at github.com/mcnamarabrian/pycon203


Thank you for joining us!

Footnotes

  1. Logging

  2. Metrics