Skip to content

alyrlee/python-devops-course

Β 
Β 

Repository files navigation

Python DevOps CI/CD Pipeline

CircleCI

Python DevOps Course

A comprehensive repository for learning Python DevOps practices with enterprise-grade CI/CD pipeline, security scanning, and AWS integration.

πŸ“ Project Structure

python-devops-course/
β”œβ”€β”€ src/                           # Source code
β”‚   β”œβ”€β”€ aws/                      # AWS management tools
β”‚   β”‚   └── aws_iam_manager.py    # Complete AWS IAM role & policy management
β”‚   β”œβ”€β”€ cli/                      # Command line interfaces
β”‚   β”‚   β”œβ”€β”€ helloclick.py         # Tokenizer CLI tool
β”‚   β”‚   β”œβ”€β”€ hello-click2.py       # Enhanced CLI with name processing
β”‚   β”‚   β”œβ”€β”€ gcli.py               # File search utility
β”‚   β”‚   β”œβ”€β”€ hello.py              # AWS S3 buckets listing tool
β”‚   β”‚   └── lambda_function.py    # AWS Lambda function example
β”‚   └── utils/                    # Utility functions
β”‚       β”œβ”€β”€ magic_stuff.py        # Magic utility functions
β”‚       └── marco.py              # Marco utility functions
β”œβ”€β”€ tests/                        # Test suite
β”‚   β”œβ”€β”€ test_helloclick.py       # Tokenizer tests
β”‚   └── test_gcli.py              # File search tests
β”œβ”€β”€ scripts/                      # Deployment and automation scripts
β”‚   β”œβ”€β”€ deploy_lambda.py          # Full Lambda deployment with role creation
β”‚   β”œβ”€β”€ deploy_lambda_simple.py  # Simplified Lambda deployment
β”‚   └── deploy_with_role.py       # Lambda deployment with existing role
β”œβ”€β”€ lambda_packages/              # Lambda deployment packages
β”‚   └── python-devops-lambda.zip  # Current deployment package
β”œβ”€β”€ docs/                         # Documentation
β”‚   └── AWS_IAM_SETUP.md          # AWS setup guide
β”œβ”€β”€ requirements.txt              # Dependencies (pylint, click, pytest, boto3, ipython, pandas)
β”œβ”€β”€ Makefile                      # Build automation with 15+ commands
β”œβ”€β”€ pytest.ini                   # Test configuration
β”œβ”€β”€ .gitignore                    # Version control exclusions
β”œβ”€β”€ README.md                     # Project overview
└── PROJECT_STRUCTURE.md          # Detailed structure guide

πŸš€ Enhanced CI/CD Pipeline

πŸ”’ Security & Quality

  • Security Scanning: Bandit, Semgrep, Safety with SARIF uploads
  • Dependency Review: Automated vulnerability scanning for PRs
  • SARIF Integration: Security results surface in GitHub Security tab
  • Conditional Failure: Soft failures on dev, hard failures on main

⚑ Performance & Efficiency

  • Parallel Testing: pytest-xdist with conditional strategies
  • Build Once, Deploy Many: Artifact reuse across environments
  • Smart Caching: pip cache with dependency path monitoring
  • Fail-Fast Control: Resilient to single environment failures

🌍 Multi-Environment Deployment

  • GitHub Environments: Approval gates and environment-specific secrets
  • Matrix Strategies: Parallel deployment to multiple environments
  • Environment-Specific: Different configurations per environment
  • URL Tracking: Deployment URL history and monitoring

πŸ› οΈ Developer Experience

  • Job Summaries: Rich markdown summaries in GitHub UI
  • Composite Actions: DRY principle for reusable workflows
  • Ruff Integration: 10-100x faster linting than pylint
  • Artifact Management: Conditional uploads with retention policies

πŸš€ Quick Start

# Install dependencies
make install

# Run tests
make test

# Setup AWS IAM
make aws-setup

# Get help
make help

πŸš€ Deployment Dashboard

A modern, responsive web interface for monitoring deployment status across all environments in real-time.

πŸ“Š Dashboard Features

  • Real-time Status Monitoring: Visual status indicators for all environments (ephemeral, dev, staging, prod)
  • Color-coded Environment Cards: Green (success), Red (failure), Yellow (pending)
  • Detailed Deployment Information: Region, last deployed time, duration, commit hash
  • Summary Statistics: Overview of successful, failed, and pending deployments
  • Responsive Design: Works on desktop and mobile devices
  • RESTful API: Complete API for deployment status management

🎯 Dashboard UI Example

Deployment Dashboard

The dashboard shows real-time deployment status with color-coded environment cards, detailed deployment information, and summary statistics.

πŸš€ Quick Start

# Install dependencies
pip install flask

# Run the dashboard
python run_dashboard.py

# Access the dashboard
open http://localhost:5001

πŸ”— API Endpoints

  • GET / - Main dashboard interface
  • GET /api/deployments - All deployment statuses
  • GET /api/deployments/<env> - Specific environment status
  • POST /api/deployments/<env>/status - Update environment status
  • GET /api/health - Health check
  • GET /api/summary - Deployment summary statistics

πŸ“± Dashboard Components

Environment Cards

Each environment displays:

  • Environment name and status badge
  • AWS region and deployment details
  • Last deployment timestamp
  • Deployment duration and commit hash
  • Direct link to environment URL

Summary Statistics

  • Total deployments across all environments
  • Successful deployment count
  • Failed deployment count
  • Pending deployment count

Interactive Features

  • Refresh button for real-time updates
  • Last updated timestamp
  • Responsive grid layout
  • Smooth animations and transitions

πŸ”§ Integration with CI/CD

The dashboard integrates seamlessly with the CI/CD pipeline:

  1. Automatic Status Updates: Deployment jobs update status via API
  2. Real-time Monitoring: View deployment progress across environments
  3. Failure Detection: Immediate visual feedback on deployment failures
  4. Environment Tracking: Monitor deployment history and performance

For detailed documentation, see DEPLOYMENT_DASHBOARD.md.

πŸ› οΈ Available Commands

Command Description
make install Install dependencies
make test Run all tests
make test-cli Run CLI tests
make test-aws Run AWS tests
make lint Lint all code
make format Format all code
make aws-setup Setup AWS IAM
make clean Clean up cache files
make help Show all commands

πŸ“š Modules

AWS Management (src/aws/)

  • AWS IAM Manager: Complete Cloud9 service role and policy management
  • Features: Role creation, policy attachment, user management
  • Documentation: See docs/AWS_IAM_SETUP.md

CLI Tools (src/cli/)

  • Hello Click: Interactive CLI tools with Click framework
  • Features: Tokenization, command line interfaces, AWS integration
  • Examples:
    • helloclick.py - Text tokenization CLI tool
    • gcli.py - File search utility with glob patterns
    • hello.py - AWS S3 buckets listing tool
    • hello-click2.py - Enhanced CLI with name processing
    • lambda_function.py - AWS Lambda function example

Web Application (src/web/)

  • Flask Application: Modern web application with deployment dashboard
  • Features: Real-time deployment monitoring, RESTful API, responsive UI
  • Components:
    • application.py - Main Flask web application
    • deployment_api.py - Deployment dashboard API server
    • templates/ - HTML templates for web interface
    • templates/deployment-dashboard.html - Modern deployment dashboard UI

Utilities (src/utils/)

  • Magic Functions: Utility functions and helpers
  • Features: Common functionality, helper scripts
  • Examples: magic_stuff.py, marco.py

πŸ”§ Development

Prerequisites

  • Python 3.8+
  • AWS CLI configured (for AWS features)
  • Virtual environment

Setup

# Clone repository
git clone <repository-url>
cd python-devops-course

# Install dependencies
make install

# Activate virtual environment
source venv/bin/activate

Testing

# Run all tests
make test

# Run specific test suites
make test-cli
make test-aws

# Run with coverage
make test

Code Quality

# Lint code
make lint

# Format code
make format

# Clean up
make clean

☁️ AWS Integration

AWS IAM Management

  • Cloud9 Service Role: Automated role creation with proper policies
  • User Management: IAM user creation and role attachment
  • Security: AWS managed policies and best practices

Setup AWS

# Configure AWS credentials
aws configure

# Setup AWS IAM
make aws-setup

# Test AWS functionality
make aws-test

AWS Features

  • βœ… Cloud9 service role creation
  • βœ… AWS managed policy attachment
  • βœ… User management and role assumption
  • βœ… Access key creation
  • βœ… Comprehensive error handling

πŸ”„ GitHub Actions CI/CD

CI Pipeline (.github/workflows/ci.yml)

# Security scanning with SARIF uploads
- name: Bandit (SARIF)
  run: bandit -r src -f sarif -o bandit.sarif

- name: Semgrep (SARIF)  
  run: semgrep ci --config p/ci --sarif --output semgrep.sarif

- name: Upload SARIF
  uses: github/codeql-action/upload-sarif@v3

Parallel Testing

# Fast feedback on PRs, comprehensive on main
- name: Test with pytest
  run: |
    if [ "${{ github.ref }}" == "refs/heads/main" ]; then
      pytest -q --maxfail=0 -n auto --durations=15
    else
      pytest -q --maxfail=1 -n auto --durations=15
    fi

Multi-Environment Deployment

strategy:
  fail-fast: false
  matrix:
    include:
      - environment: ephemeral
        aws_region: us-east-1
        timeout: 300
      - environment: dev
        aws_region: us-east-1
        timeout: 600
      - environment: staging
        aws_region: us-east-1
        timeout: 900
      - environment: prod
        aws_region: us-west-2
        timeout: 1200

Composite Actions

# DRY Python setup
- uses: ./.github/actions/python-setup

# Environment-specific post-deploy checks
- uses: ./.github/actions/post-deploy-checks
  with:
    environment: ${{ matrix.environment }}

πŸ“– Documentation

  • Project Structure: PROJECT_STRUCTURE.md
  • AWS Setup: docs/AWS_IAM_SETUP.md
  • Code Examples: See src/ directory

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Follow the project structure
  4. Write tests for new functionality
  5. Run linting and formatting
  6. Submit a pull request

πŸš€ AWS CodeBuild & Elastic Beanstalk Deployment

πŸ“¦ AWS CodeBuild Integration

Buildspec Configuration

Create buildspec.yml in your project root:

version: 0.2
phases:
  install:
    runtime-versions:
      python: 3.11
  pre_build:
    commands:
      - echo Installing dependencies...
      - pip install -r requirements.txt
  build:
    commands:
      - echo Running tests...
      - make test
      - echo Running linting...
      - make lint
      - echo Building web application...
      - cd src/web && python -c "import application; print('βœ… Web app ready')"
  post_build:
    commands:
      - echo Build completed successfully
artifacts:
  files:
    - '**/*'
  base-directory: .

CodeBuild Setup Commands

# Create CodeBuild project
aws codebuild create-project \
  --name python-devops-build \
  --source type=GITHUB,location=https://github.com/your-username/python-devops-course \
  --artifacts type=NO_ARTIFACTS \
  --environment type=LINUX_CONTAINER,image=aws/codebuild/python:3.11 \
  --service-role arn:aws:iam::YOUR_ACCOUNT:role/CodeBuildServiceRole

# Start build
aws codebuild start-build --project-name python-devops-build

🌐 Elastic Beanstalk Deployment

1. Create Application Package

# Create deployment package
mkdir -p eb-deploy
cp -r src/web/* eb-deploy/
cp requirements.txt eb-deploy/
cp Procfile eb-deploy/

# Create Procfile for Elastic Beanstalk
echo "web: gunicorn application:app --bind 0.0.0.0:8000" > eb-deploy/Procfile

# Create .ebextensions for configuration
mkdir -p eb-deploy/.ebextensions

2. Elastic Beanstalk Configuration

Create eb-deploy/.ebextensions/01_packages.config:

packages:
  yum:
    git: []

Create eb-deploy/.ebextensions/02_python.config:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: application:app
  aws:elasticbeanstalk:application:environment:
    PYTHONPATH: "/var/app/current"

3. Deployment Commands

# Install EB CLI
pip install awsebcli

# Initialize Elastic Beanstalk
eb init python-devops-app

# Create environment
eb create python-devops-env

# Deploy application
eb deploy

# Open in browser
eb open

4. Environment Configuration

# Set environment variables
eb setenv AWS_DEFAULT_REGION=us-east-1

# Scale application
eb scale 2

# View logs
eb logs

# Terminate environment
eb terminate

πŸ”„ CI/CD Pipeline Integration

GitHub Actions + CodeBuild + Elastic Beanstalk

# .github/workflows/ship.yml
name: Deploy to AWS
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
    
    - name: Deploy to Elastic Beanstalk
      run: |
        pip install awsebcli
        eb deploy python-devops-env

πŸ“Š Monitoring & Logs

CloudWatch Integration

# View application logs
aws logs describe-log-groups --log-group-name-prefix /aws/elasticbeanstalk

# Monitor metrics
aws cloudwatch get-metric-statistics \
  --namespace AWS/ElasticBeanstalk \
  --metric-name ApplicationRequestsTotal \
  --start-time 2024-01-01T00:00:00Z \
  --end-time 2024-01-02T00:00:00Z \
  --period 3600 \
  --statistics Sum

Health Monitoring

# Check application health
eb health

# View detailed health
eb health --refresh

# Monitor specific instances
eb status --verbose

πŸ› οΈ Advanced Configuration

Load Balancer Configuration

# .ebextensions/03_loadbalancer.config
option_settings:
  aws:elbv2:loadbalancer:
    IdleTimeout: 60
  aws:autoscaling:launchconfiguration:
    InstanceType: t3.micro

Database Integration

# .ebextensions/04_database.config
option_settings:
  aws:rds:dbinstance:
    DBInstanceClass: db.t3.micro
    DBAllocatedStorage: 20

πŸ“‹ Deployment Checklist

  • CodeBuild: Build and test automation
  • Elastic Beanstalk: Application deployment
  • CloudWatch: Monitoring and logging
  • Load Balancer: Traffic distribution
  • Auto Scaling: Handle traffic spikes
  • Health Checks: Application monitoring
  • SSL/TLS: Secure connections
  • Domain: Custom domain setup

πŸ“„ License

This project is part of the Python DevOps Course.

πŸŽ“ Pragmatic AI Labs | Join 1M+ ML Engineers

πŸ”₯ Hot Course Offers:

πŸš€ Level Up Your Career:

Learn end-to-end ML engineering from industry veterans at PAIML.COM

About

A repo for Python DevOps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Jupyter Notebook 99.3%
  • Other 0.7%