Build a Serverless Python App on AWS Lambda
Build a Serverless Python App on AWS Lambda (Free Tier Ready)
Imagine deploying a full web API that scales automatically from zero to thousands of requests, costs you nothing until someone uses it, and requires no server management. That’s the magic of serverless, and with AWS Lambda and Python, you can build it in under 30 minutes.
Let’s ditch the heavy infrastructure talk and get straight to building a real, working Python API you can deploy today.
Why Lambda + Python?
AWS Lambda is the industry standard for serverless computing, and Python is its most popular runtime. Together, they offer:
- Zero server management: AWS handles scaling, patching, and availability.
- Cost efficiency: You pay only per millisecond of execution (free tier: 1M requests/month).
- Instant scaling: From 1 to 10,000 concurrent users without configuration.
- Python simplicity: Clean syntax, rich libraries, and fast development cycles.
Whether you’re building a REST API, processing S3 uploads, or automating workflows, this stack is your fastest path to production.
Step 1: Set Up Your AWS Credentials
Before writing code, you need to authenticate your local environment with AWS.
- Install the AWS CLI:
pip install awscli
aws configure
This prompts for your Access Key ID, Secret Key, Region (e.g., us-east-1), and Output format (json). [2]
-
Create an IAM User (if you don’t have keys):
- Go to the IAM Console → Users → Create user.
- Enable Programmatic Access.
- Attach AdministratorAccess (for development) or a more restricted policy for production.
- Copy your Access Key ID and Secret Key. [6]
Configure Serverless Framework Credentials (optional but recommended):
serverless config credentials --provider aws --key <ACCESS_KEY_ID> --secret <SECRET_KEY>
This lets the Serverless Framework deploy without passing keys manually. [6]
Step 2: Create Your Serverless Project
We’ll use the Serverless Framework, a powerful tool that automates Lambda deployment, API Gateway setup, and environment management.
- Install Serverless globally:
npm install -g serverless
[2][6]
- Create a new Python service:
serverless create --template aws-python3 --path serverless-app
cd serverless-app
This generates a handler.py (your Lambda function) and a serverless.yml (deployment config). [2]
- Install local testing plugin (for dev):
npm install --save-dev serverless-offline
Then add it to serverless.yml:
plugins:
- serverless-offline
[2]
Step 3: Write Your First Lambda Function
Open handler.py and replace it with this production-ready API endpoint:
import json
def hello(event, context):
"""
Handle GET requests and return a JSON response.
event: Contains request data (headers, query params, body).
context: Contains Lambda runtime info (function name, memory, etc.).
"""
body = {
"message": "Go Serverless v2.0! Your Python Lambda function is working!",
"timestamp": "2026-07-14T15:00:00Z",
"user": event.get("queryStringParameters", {}).get("name", "Anonymous")
}
response = {
"statusCode": 200,
"body": json.dumps(body),
"headers": {
"Content-Type": "application/json"
}
}
return response
This function:
- Extracts a
namequery parameter (e.g.,?name=Alice). - Returns a JSON response with status 200.
- Handles missing parameters gracefully.
Step 4: Configure API Gateway Trigger
Your Lambda function needs an HTTP endpoint. We’ll use API Gateway to trigger it.
Open serverless.yml and ensure it includes:
service: serverless-app
provider:
name: aws
runtime: python3.12
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: /
method: get
cors: true
This:
- Sets Python 3.12 as the runtime. [5]
- Creates a
GET /endpoint. - Enables CORS for browser testing.
Step 5: Test Locally (Before Deploying)
Run a local server to test your function without AWS:
serverless offline
Then hit your endpoint:
curl http://localhost:3000
curl http://localhost:3000/?name=Dev
You’ll see JSON responses instantly. [2]
Step 6: Deploy to AWS
Now, push to the cloud:
serverless deploy
This:
- Zips your code + dependencies.
- Creates the Lambda function.
- Sets up API Gateway.
- Returns your API endpoint URL. [2][6]
Example output:
Service Information
service: serverless-app
stage: dev
region: us-east-1
api keys:
None
endpoints:
GET - https://abc123xyz.execute-api.us-east-1.amazonaws.com/dev/
functions:
hello: serverless-app-dev-hello
Test it live:
curl https://abc123xyz.execute-api.us-east-1.amazonaws.com/dev/
Bonus: Add Dependencies (e.g., boto3)
If you need AWS SDK access (e.g., to read from DynamoDB):
- Create
requirements.txt:
boto3
- Install locally (for testing):
pip install -r requirements.txt
- Serverless automatically bundles dependencies during
deploy. [2]
Note: For large libraries (like
requestsorPandas), use Lambda Layers to reduce deployment size. [5]
Troubleshooting Tips
| Issue | Fix |
|---|---|
ModuleNotFoundError |
Ensure dependencies are in requirements.txt and installed. [2] |
| API returns 500 | Check Lambda logs: serverless logs -f hello --tail [6] |
| CORS errors | Add cors: true in serverless.yml [2] |
| Slow cold starts | Use Python 3.12+ and ARM64 architecture (faster, cheaper) [5] |
What’s Next?
You now have a live, serverless Python API. Here’s how to level up:
-
Add DynamoDB integration: Store data using
boto3. [2] -
Deploy multiple endpoints: Add
/users,/posts, etc. inserverless.yml. - Enable CI/CD: Automate tests + deployment with AWS CodePipeline. [2]
- Monitor performance: Use the Monitor tab in Lambda Console for metrics. [7]
Your Turn: Deploy Today
Don’t let this sit in your bookmarks. Pick one idea:
- A personal blog API
- A webhook handler for GitHub
- A data processor for S3 uploads
Build it, deploy it with serverless deploy, and share your endpoint URL. The serverless world is waiting—no servers, no fuss, just code.
Ready to go? Run serverless create --template aws-python3 --path my-first-app and start coding. Your first serverless app is just 30 minutes away.
If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!
Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.
💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*
Top comments (0)