Skip to content

vktrenga/lambda-python-sam

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Features

  • 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

🛠 Prerequisites

Before you start, ensure you have:

  1. AWS Account

  2. AWS IAM User

    • Create an IAM user with programmatic access

    • Attach the following policies:

      alt text

    • Save Access Key ID and Secret Access Key

  3. AWS CLI Installed

  4. AWS SAM CLI Installed

  5. Python 3.10+ installed


⚙️ AWS Configuration

Configure AWS CLI with your IAM user credentials:

aws configure

You 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-identity

🏗 SAM Project Setup

1. Clone Repository

git clone https://github.com/vktrenga/lambda-python-sam.git
cd lambda-python-sam

2. Create Virtual Environment

python3 -m venv venv
source venv/bin/activate    # Windows: venv\Scripts\activate
pip install -r user/requirements.txt

3. Build SAM Application

sam build

📝 Create a Simple Lambda App

  1. Open user/app.py and add your function:
import json

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps({"message": "Hello from Lambda!"})
    }
  1. 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
  1. Test locally:
sam local invoke UserApiFunction --event events/event.json
sam local start-api

🚀 Deploy to AWS

sam 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.


📈 CI/CD Pipeline (GitHub Actions)

The project includes a workflow .github/workflows/sam-ci-cd.yml:

Pipeline steps:

  1. Linting & Code Checks: Ensure Python code quality
  2. Build: sam build
  3. Deploy: sam deploy automatically on main branch
  4. 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_IAM

Tip: Add AWS credentials as GitHub secrets for secure CI/CD.


🔧 Logging & Monitoring

  • Lambda logs are automatically sent to CloudWatch
  • Optional: Add custom log statements in app.py
  • Supports triggers: S3 events, EventBridge scheduled tasks

📜 License

This project is licensed under MIT License. See LICENSE for details.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages