Skip to main content
XenonStack

DevOps

XenonStack Blog Serverless Microservices in Python on AWS Lambda

| Published | Updated

Key takeaway

  • Serverless microservices in Python run as event-driven functions. Learn packaging, deployment, cold starts and limits, and how AI agents change the design.

A serverless microservice in Python is a small Python function, packaged with its dependencies, that a managed platform runs in response to an event, so the team writes application logic without managing servers, operating systems or virtual machines. The servers still exist, but provisioning and managing the infrastructure belongs to the service provider.

This guide is for programmers, DevOps engineers and architects who want a clear path from a Python service to a function. It covers how monolith, microservice and function styles differ, how to package and test a Python function, the limits to plan for, and how AI agents change the design. For the Java version, read our guide to serverless microservices in Java.

What is a serverless microservice in Python?

The AWS Lambda documentation describes Lambda as "a serverless compute service" where you "run code without provisioning or managing servers". You write a handler, connect it to a trigger, and the platform runs it. Our guide to serverless computing covers the wider model, and function as a service explains the function layer. Your company still owns software design: serverless removes machine work, not engineering work.

How do monolith, microservice and function styles differ?

Serverless changes how applications are built. Traditionally, business rules, boundary conditions and complex integrations are wired into one application, which slows delivery and hard-codes a fixed set of requirements. Smaller deployable units let an application evolve and be deployed without disturbing the rest of the system. See our overview of microservices architecture.

Two-column diagram. Left, before: monolith, scale by copying, hard-wired rules, manage servers. Right, serverless Python: event trigger, zip package, thin handler, platform runs it.
StyleHow it is builtHow it scales
MonolithAll functionality in a single processReplicate the whole process on more servers
MicroserviceEach capability is a separate serviceDistribute services across servers and replicate as needed
Function (FaaS)Services broken into functions triggered by eventsThe platform runs functions as events arrive

How do you package and deploy a Python function?

The Python on Lambda page lists the supported runtimes with their deprecation dates, so choose a supported version first. A handler has the signature lambda_handler(event, context), and the code runs in an environment that includes the AWS SDK for Python (Boto3), with credentials from an IAM role that you manage. The page also lists three deployment options: .zip file archives, layers, and container images.

Our worked example is an employee lookup that reads from a MongoDB database. The steps below follow the .zip route.

  1. Prepare the data store: install MongoDB, create an EmployeeDB database and an Employee collection, and insert a few demo records.
  2. Create a project directory and save your .py source files at its root, with a folder such as app for the code.
  3. List extra libraries in requirements.txt and install them into the directory so they ship with the code.
  4. Read the connection string from an environment variable instead of a config.py file inside the package, then create the client outside the handler.
  5. Zip the directory contents, upload the archive, set the handler name and choose a trigger.
  6. Run a test event and check the response and logs.

The execution environment page explains why the client belongs outside the handler: objects declared outside it remain initialised when an environment is reused, and it recommends checking whether a connection exists before creating a new one.

What are the limits of serverless in Python?

LimitWhat it meansHow to respond
Cold startPer the Lambda docs, a cold start covers downloading code and setting up the environment; it typically occurs in under 1% of invocations and ranges from under 100 ms to over 1 secondKeep imports light, create clients once, measure Init time
Long-running workThe Lambda overview lists up to 15 minutes per invocation by defaultSplit the job, or use a workflow or container
Vendor dependenceHandler signatures, events and costs are provider-specific, and a platform API or price can changeKeep business logic in plain Python modules behind a thin handler
No shared stateEnvironments can be frozen, reused or replaced, and /tmp content may remain between runsStore durable state in a database; treat /tmp as a cache
Function sprawlHundreds of functions can pile up with no map of what each doesName, own and document every function; retire unused ones
Call overheadFunctions and microservices call each other over the network, never locallyDesign coarse, well-defined interfaces

If cold starts matter, the Lambda pricing page lists provisioned concurrency as a separately billed option, with no free tier applying to it. It also lists a free tier of one million requests and 400,000 GB-seconds per month, which suits a first prototype.

How do AI agents change serverless microservices in Python?

Python is the common language for agent tooling, so many functions become tools an agent calls. A function that once served a web page now sits inside a decision, which makes input validation, predictable errors and idempotent behaviour matter more. Give each tool function a narrow job and permissions, return structured responses, and log every call. ElixirClaw, the AI agents computing platform, is where XenonStack builds and runs agents that call such functions. For monitoring, see our use case on AIOps monitoring for Kubernetes and serverless.

What should you do first?

  1. Choose one small, stateless service and rebuild it as a function.
  2. Keep the handler thin and move logic into testable modules.
  3. Put configuration in environment variables and secrets in a managed store.
  4. Measure cold start and duration before tuning anything.
  5. Decide which functions agents may call and record their inputs and outputs.

To go further, read about the serverless framework Kubeless, the serverless computing guide and our serverless services and solutions.

To map one decision your agents make through a function, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is a serverless microservice in Python?

It is a small Python function packaged with its dependencies and run by a managed platform in response to an event, so the team does not manage servers.

How is a function different from a microservice?

A microservice is a separate service for one capability. A function breaks that service into smaller pieces that run when an event triggers them.

How do I deploy a Python function on AWS Lambda?

Save your .py files at the root of a directory, add dependencies, zip the contents, upload the archive and set the handler. Layers and container images are the other options listed in the Python on Lambda page.

What is a cold start?

A cold start is the time to download code and set up a new execution environment before your handler runs. The Lambda documentation says it typically occurs in under 1% of invocations.

When should I avoid serverless?

Avoid it for long-running work beyond the platform duration limit, or where you cannot accept dependence on one provider without isolating your business logic.

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

LinkedIn profile

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue