Part 5: Deployment and Best Practices
Production-Ready Lambda Functions
CI/CD Pipeline for Lambda
Complete CI/CD Flow
GitHub Actions Workflow
name: Deploy Lambda Function
on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
env:
AWS_REGION: us-east-1
PYTHON_VERSION: '3.12'
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Run linters
run: |
# Black for code formatting
black --check src/
# Flake8 for style guide
flake8 src/ --max-line-length=100
# MyPy for type checking
mypy src/
- name: Run unit tests
run: |
pytest tests/ -v --cov=src --cov-report=xml
- name: Security scan
run: |
# Bandit for security issues
bandit -r src/ -f json -o bandit-report.json
# Safety for dependency vulnerabilities
safety check --json
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml
deploy-test:
needs: test
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
environment: test
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up SAM CLI
uses: aws-actions/setup-sam@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Build SAM application
run: sam build --use-container
- name: Deploy to test
run: |
sam deploy \
--stack-name user-registration-test \
--s3-bucket ${{ secrets.SAM_BUCKET }} \
--s3-prefix test \
--capabilities CAPABILITY_IAM \
--parameter-overrides Environment=test \
--no-fail-on-empty-changeset
- name: Run integration tests
run: |
export API_ENDPOINT=$(aws cloudformation describe-stacks \
--stack-name user-registration-test \
--query 'Stacks[0].Outputs[?OutputKey==`ApiEndpoint`].OutputValue' \
--output text)
pytest tests/integration/ -v --api-endpoint=$API_ENDPOINT
deploy-prod:
needs: deploy-test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up SAM CLI
uses: aws-actions/setup-sam@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Build SAM application
run: sam build --use-container
- name: Deploy to production
run: |
sam deploy \
--stack-name user-registration-prod \
--s3-bucket ${{ secrets.SAM_BUCKET }} \
--s3-prefix prod \
--capabilities CAPABILITY_IAM \
--parameter-overrides Environment=production \
--no-fail-on-empty-changeset
- name: Create deployment marker
run: |
aws cloudwatch put-metric-data \
--namespace CustomMetrics/Deployments \
--metric-name Deployment \
--value 1 \
--dimensions Function=UserRegistration,Environment=production
- name: Smoke tests
run: |
export API_ENDPOINT=$(aws cloudformation describe-stacks \
--stack-name user-registration-prod \
--query 'Stacks[0].Outputs[?OutputKey==`ApiEndpoint`].OutputValue' \
--output text)
# Basic health check
curl -f $API_ENDPOINT/health || exit 1Infrastructure as Code Best Practices
Multi-Environment SAM Template
Monitoring and Observability
CloudWatch Dashboard
Custom Metrics with Lambda Powertools
Performance Optimization
Cold Start Reduction
Memory Optimization
Query CloudWatch Insights
Security Best Practices
Secrets Management
IAM Permissions
Cost Optimization
My Cost Optimization Checklist
Cost Monitoring Dashboard
Testing Strategy
Unit Tests
Integration Tests
Key Takeaways
Series Conclusion
Your Serverless Journey
Next Steps
Additional Resources
Last updated