Serverless Python in 2024
Episode Deep Dive
Guest and Background
Tony Sherman
Tony began his career in the cable industry, doing everything from door-to-door installations to outside-plant work. After realizing the limited growth opportunities in that field, he took an introduction to programming class in Python and became captivated by software development. Now, Tony works as a seasoned Python developer focusing on serverless programming—especially with AWS Lambda—in domains such as IoT and school bus safety applications.
1. What Is Serverless?
- Definition: “Serverless” does not mean “no servers,” but rather that you, as the developer, do not manage the underlying server infrastructure. Instead, you focus on writing functions or code that runs in a fully managed environment.
- AWS Lambda as the Flagship Example: AWS Lambda is one of the earliest and most popular serverless function services. Other notable providers include Google Cloud Functions, Azure Functions, Vercel Functions, DigitalOcean Functions, and Cloudflare Workers.
2. Why Choose Serverless?
- Spiky or Irregular Traffic: For workloads like school bus IoT, usage is high only at certain times of day (morning and afternoon). Serverless scales automatically for these “bursty” or unpredictable workloads, saving cost and ops overhead.
- Reduced DevOps: You no longer need to provision, patch, and secure entire virtual machines or Kubernetes clusters. The cloud provider takes care of scaling and server maintenance.
- Cost Model: AWS Lambda, for example, often has a free tier of 1 million monthly requests and then charges per millisecond of usage. It can be cost-effective if you do not have heavy, constant workloads.
3. AWS Lambda Considerations
Packaging and Deployment
- Size Constraints: Zipped packages have size limits (around 50 MB compressed, 250 MB uncompressed). Container-based Lambdas can go bigger (up to 10 GB), though potentially with some cold-start penalty.
- Optimizing with AWS Lambda Power Tuner:
- AWS Lambda Power Tuning helps find the ideal CPU/memory setting for cost or performance. It tries multiple memory allocations, times them, and graphs the tradeoffs.
- Dependency Management with Pants:
- Pants Build can automatically bundle only the necessary Python code and libraries for each function based on imports, preventing bloated packages and helping reduce cold-start times.
Cold Starts and Performance
- Cold Starts: The first request to a new or scaled-out Lambda instance takes longer because the environment must load libraries and establish connections.
- Warm Lambdas: Once “warmed,” additional requests on the same Lambda run faster. Anything defined outside the handler (e.g., database connections) may be reused across invocations.
Databases
- DynamoDB: A serverless NoSQL database that scales well for spiky, high-volume usage. No connection pooling needed.
- RDS (Relational Databases): To mitigate issues with large numbers of connections, consider RDS Proxy for connection pooling when Lambdas interact with MySQL/PostgreSQL in AWS.
4. Observability: Logging & Tracing
AWS Lambda Powertools
- AWS Lambda Powertools for Python is a toolkit that codifies best practices for logging, tracing, and structured events on AWS Lambda.
- Offers a logging utility that outputs structured JSON for easier querying in services like CloudWatch or external log aggregators (Datadog, Splunk, etc.).
- Facilitates integrating with
Pydantic
for input validation and event parsing.
Tracing Tools
- AWS X-Ray can trace Lambda performance, including cold starts and external calls.
- DataDog APM automatically detects SQL queries and other actions, showing you breakdowns of execution time.
- Tracing is especially useful in microservices architectures where many services are called in one request.
5. Testing Serverless Code
- Local Testing: Basic unit tests can be run locally with standard Python tooling (e.g.,
pytest
). However, simulating the entire AWS environment (Lambda runtime + AWS services) can be cumbersome. - Integration Testing: Often involves deploying to a real or QA/staging environment in the cloud to verify interactions with queues (SQS), databases, or other services. Some teams spin up separate, temporary environments (e.g., ephemeral environments per pull request) to run more robust tests.
Relevant Tools and Links
AWS Lambda
https://aws.amazon.com/lambda/AWS Lambda Power Tuning
https://github.com/alexcasalboni/aws-lambda-power-tuningAWS Lambda Powertools for Python
https://awslabs.github.io/aws-lambda-powertools-python/Datadog (APM / Observability)
https://www.datadoghq.com/Pants Build
https://www.pantsbuild.org/RDS Proxy
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html
Overall Takeaway
Serverless computing in Python continues to mature, with AWS Lambda among the most widely adopted options. Tools like AWS Lambda Powertools simplify structured logging, tracing, and data validation, while build systems such as Pants help minimize package size and manage dependencies more intelligently. Organizations that deal with spiky traffic, or prefer a managed environment over self-hosting servers, stand to benefit from a serverless approach. By pairing thorough unit tests with limited integration or QA deployments, teams can streamline development while achieving high reliability and performance.
Links from the show
Tony Sherman: linkedin.com
PyCon serverless talk: youtube.com
AWS re:Invent talk: youtube.com
Powertools for AWS Lambda: docs.powertools.aws.dev
Pantsbuild: The ergonomic build system: pantsbuild.org
aws-lambda-power-tuning: github.com
import-profiler: github.com
AWS Fargate: aws.amazon.com
Run functions on demand. Scale automatically.: digitalocean.com
Vercel: vercel.com
Deft: deft.com
37 Signals We stand to save $7m over five years from our cloud exit: world.hey.com
The Global Content Delivery Platform That Truly Hops: bunny.net
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy