Intermediate

DevOps and Infrastructure as Code

Lesson 4 of 4 Estimated Time 45 min

DevOps and Infrastructure as Code

Infrastructure as Code (IaC) is the practice of managing infrastructure through code rather than manual configuration. Whether using Terraform, CloudFormation, Docker, or Kubernetes, AI can help you generate, optimize, and troubleshoot infrastructure code.

Docker and Containerization

Generating Dockerfiles

You: "Generate a Dockerfile for:
     - Python 3.11 application
     - FastAPI server
     - Gunicorn production server
     - Multi-stage build for optimization
     - Non-root user for security"

AI: [Generates production-ready Dockerfile]

Optimizing Docker Images

You: "This Docker image is 800MB. Optimize it:
     [paste Dockerfile]

     Constraints:
     - Must include Python dependencies
     - Must run production server
     - Should be as small as possible"

AI: [Shows optimization techniques]
     [Uses multi-stage builds, better base images]

Infrastructure as Code

Terraform Configuration

You: "Generate Terraform to deploy:
     - VPC with public/private subnets
     - RDS PostgreSQL database
     - ECS service running Docker
     - Load balancer
     - Auto-scaling

     Provider: AWS"

AI: [Generates complete Terraform configuration]

CloudFormation Templates

You: "Generate CloudFormation template for:
     - EC2 instance
     - S3 bucket with public access
     - Security groups
     - IAM roles

     Provider: AWS"

AI: [Generates CloudFormation YAML/JSON]

CI/CD Pipelines

GitHub Actions Workflow

You: "Generate GitHub Actions workflow for:
     - Run tests on every push
     - Build Docker image
     - Push to ECR
     - Deploy to ECS
     - Run smoke tests

     Language: Python
     Framework: FastAPI"

AI: [Generates complete GitHub Actions workflow]

GitLab CI Pipeline

You: "Generate .gitlab-ci.yml for:
     - Lint code
     - Run unit tests
     - Build and push Docker
     - Deploy to Kubernetes
     - Monitor deployment"

AI: [Generates GitLab CI configuration]

Kubernetes

Kubernetes Manifests

You: "Generate Kubernetes manifests for:
     - Deployment (3 replicas)
     - Service (LoadBalancer)
     - ConfigMap for configuration
     - Secret for credentials
     - HPA for auto-scaling

     App: Node.js service
     Container: node:18"

AI: [Generates complete K8s manifests]

Helm Charts

You: "Generate Helm chart for:
     - Multi-tier application
     - Database
     - Cache
     - Web service
     - Worker service

     Configurable values:
     - Replicas
     - Image versions
     - Resource limits"

AI: [Generates Helm chart structure]

Infrastructure Monitoring

Monitoring Setup

You: "Set up monitoring for:
     - Application logs to CloudWatch
     - Metrics to Prometheus
     - Alerts for errors
     - Dashboard in Grafana

     Platform: AWS"

AI: [Generates monitoring configuration]

Alert Rules

You: "Define alert rules for:
     - High error rate (>5% in 5 min)
     - Database query time (>1s)
     - Memory usage (>80%)
     - Disk space (>90%)

     Tool: Prometheus"

AI: [Generates alert rule definitions]

Security in Infrastructure

Security Groups

You: "Configure security groups for:
     - Web tier (HTTP/HTTPS only)
     - App tier (internal only)
     - Database tier (restricted ports)

     Allow traffic between tiers only
     Provider: AWS"

AI: [Generates security group rules]

IAM Policies

You: "Generate IAM policy for:
     - Application needs: read S3, write CloudWatch Logs
     - Least privilege principle
     - No wildcards in actions

     Provider: AWS"

AI: [Generates restrictive IAM policy]

Infrastructure Troubleshooting

Debugging Deployment Issues

You: "Deployment fails with error:
     [error message]

     Logs:
     [paste logs]

     Infrastructure:
     [describe setup]

     What's wrong?"

AI: [Analyzes error]
     [Provides debugging steps]
     [Suggests fixes]

Performance Issues

You: "Application running on ECS is slow.
     CloudWatch metrics:
     [paste metrics]

     Configuration:
     [paste task definition]

     What's the bottleneck?"

AI: [Identifies performance issue]
     [Suggests optimization]

Real-World Scenarios

Scenario 1: Blue-Green Deployment

You: "Implement blue-green deployment for:
     - Zero-downtime updates
     - Rollback capability
     - Load balancer switching

     Framework: Terraform + AWS"

AI: [Generates blue-green strategy]

Scenario 2: Database Migration

You: "Plan database migration:
     - Prod: PostgreSQL on EC2
     - New: RDS PostgreSQL
     - Zero downtime
     - Rollback plan

     Data size: 500GB"

AI: [Shows safe migration strategy]

Scenario 3: Multi-Region Deployment

You: "Deploy application to 3 regions:
     - US East
     - US West
     - Europe

     High availability
     Disaster recovery

     Framework: Terraform"

AI: [Generates multi-region infrastructure]

Infrastructure Best Practices

State Management

You: "How should I manage Terraform state?
     - Remote state?
     - Locking?
     - Backup?
     - Secrets in state?"

AI: [Explains state management best practices]

Code Organization

You: "How should I organize Terraform code for:
     - Multiple environments (dev, staging, prod)
     - Multiple regions
     - Shared modules
     - Easy maintenance"

AI: [Shows directory structure and patterns]

Exercises

  1. Docker Optimization: Write a Dockerfile. Ask AI to:

    • Identify improvements
    • Reduce image size
    • Improve security
    • Add multi-stage builds
  2. Terraform Module: Create Terraform for:

    • VPC infrastructure
    • Ask AI to extract into reusable module
    • Use module in multiple environments
  3. CI/CD Pipeline: Design pipeline for your project:

    • Ask AI to generate workflow for your platform
    • Include: test, build, deploy
    • Add: notifications, rollback
  4. Kubernetes Deployment: Deploy application to K8s:

    • Generate manifests from Dockerfile
    • Set up service, configmap, secrets
    • Deploy and verify
  5. Infrastructure Audit: Review existing infrastructure:

    • Ask AI for security issues
    • Identify optimization opportunities
    • Document improvements needed