- AWS Lambda functions in Python
- API Gateway integration
- CloudWatch logging
- Event-driven triggers (S3, EventBridge)
- Infrastructure as code with AWS SAM
- CI/CD pipeline using GitHub Actions
- Easy local testing and deployment
Before you start, ensure you have:
-
AWS Account
- Create an account: https://aws.amazon.com
-
AWS IAM User
-
AWS CLI Installed
- Install: https://aws.amazon.com/cli/
-
AWS SAM CLI Installed
-
Python 3.10+ installed
Configure AWS CLI with your IAM user credentials:
aws configureYou will be prompted for:
- AWS Access Key ID
- AWS Secret Access Key
- Default region (e.g.,
ap-south-1) - Default output format (
json)
Test configuration:
aws sts get-caller-identitygit clone https://github.com/vktrenga/lambda-python-sam.git
cd lambda-python-sampython3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r user/requirements.txtsam build- Open
user/app.pyand add your function:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({"message": "Hello from Lambda!"})
}- Add function to
template.yaml:
Resources:
UserApiFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.10
Handler: app.lambda_handler
CodeUri: user/
Timeout: 10
Events:
GetUsers:
Type: Api
Properties:
Path: /users
Method: get- Test locally:
sam local invoke UserApiFunction --event events/event.json
sam local start-apisam deploy --guided- Provide stack name, region, and confirm IAM role creation
- After deployment, you’ll get the API Gateway URL:
https://<api-id>.execute-api.<region>.amazonaws.com/Prod/users
Test in browser or Postman.
The project includes a workflow .github/workflows/sam-ci-cd.yml:
Pipeline steps:
- Linting & Code Checks: Ensure Python code quality
- Build:
sam build - Deploy:
sam deployautomatically onmainbranch - Notifications: Optional Slack/Email alerts
Example workflow snippet:
name: SAM CI/CD
on:
push:
branches: ["main"]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.10
- name: Install SAM CLI
run: |
pip install aws-sam-cli
- name: Build SAM app
run: sam build
- name: Deploy SAM app
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ap-south-1
run: sam deploy --no-confirm-changeset --stack-name lambda-python-sam --capabilities CAPABILITY_IAMTip: Add AWS credentials as GitHub secrets for secure CI/CD.
- Lambda logs are automatically sent to CloudWatch
- Optional: Add custom log statements in
app.py - Supports triggers: S3 events, EventBridge scheduled tasks
This project is licensed under MIT License. See LICENSE for details.
