Skip to content

Latest commit

 

History

History
631 lines (498 loc) · 20 KB

File metadata and controls

631 lines (498 loc) · 20 KB

Complete System Flow - End to End

Overview

Your system is a fully automated DevOps monitoring and remediation platform with 4 main components working together.


The 4 Main Components

1. Application Layer (ECS)

  • What: Your Node.js application running in Docker containers
  • Where: AWS ECS (Elastic Container Service)
  • Purpose: Serves HTTP requests and generates logs

2. Monitoring Layer (CloudWatch)

  • What: Collects metrics, logs, and triggers alarms
  • Where: AWS CloudWatch
  • Purpose: Detects when something goes wrong

3. Notification Layer (SNS)

  • What: Sends notifications when alarms trigger
  • Where: AWS SNS (Simple Notification Service)
  • Purpose: Alerts humans and systems

4. Automation Layer (Lambda + DevOps Agent)

  • What: Automatically fixes problems
  • Where: AWS Lambda + AWS DevOps Agent
  • Purpose: Self-healing and analysis

Complete Flow: From Error to Resolution

Scenario: A Real User Encounters a Bug

Let me walk you through what happens when a real error occurs:


STEP 1: User Request (Second 0)

User's Browser
    ↓
http://your-alb.amazonaws.com/api/checkout

What happens:

  • User clicks "Checkout" button on your website
  • Browser sends HTTP request to your Application Load Balancer

STEP 2: Load Balancer (Second 0.1)

Application Load Balancer (ALB)
    ↓
Routes request to ECS Task

What happens:

  • ALB receives the request
  • Performs health check on ECS tasks
  • Routes request to a healthy task
  • Starts tracking metrics (request count, response time)

STEP 3: Application Processing (Second 0.2)

ECS Task (Docker Container)
    ↓
Node.js Application
    ↓
app.get('/api/checkout', async (req, res) => {
    const user = await db.findUser(req.userId);
    const total = user.cart.total;  // ❌ BUG: user.cart is null
    res.json({ total });
})

What happens:

  • Application receives request
  • Code has a bug: tries to access user.cart.total when cart is null
  • Application crashes with error
  • Returns 500 Internal Server Error

STEP 4: Error Logging (Second 0.3)

Application Code:
console.error(JSON.stringify({
    level: 'ERROR',
    message: 'Cannot read property total of null',
    timestamp: '2026-02-18T21:45:00.300Z',
    stack: 'Error at /api/checkout...'
}));

What happens:

  • Application logs error to stdout
  • ECS captures stdout and sends to CloudWatch Logs
  • Log entry appears in /ecs/devops-agent-demo-dev log group

CloudWatch Logs receives:

{
    "level": "ERROR",
    "message": "Cannot read property total of null",
    "timestamp": "2026-02-18T21:45:00.300Z",
    "stack": "Error at /api/checkout..."
}

STEP 5: Metrics Collection (Second 0.4)

ALB Metrics:
    HTTPCode_Target_5XX_Count = 1
    
Application Logs:
    ERROR level detected

What happens:

  • ALB records that target returned 5XX error
  • CloudWatch Logs metric filter detects "ERROR" in logs
  • Both metrics start accumulating

STEP 6: More Errors (Seconds 1-60)

More users hit the same bug:
    Request 2 → 500 error
    Request 3 → 500 error
    ...
    Request 15 → 500 error

What happens:

  • Multiple users encounter the same bug
  • Error count increases: 1, 2, 3... 15
  • CloudWatch aggregates metrics every 60 seconds

STEP 7: Metrics Aggregation (Minute 1:00)

CloudWatch Metrics (1-minute period):
    HTTPCode_Target_5XX_Count = 15 errors

What happens:

  • CloudWatch aggregates all errors from the past minute
  • Creates a data point: "15 errors in 1 minute"
  • Stores this in metrics database

STEP 8: Alarm Evaluation (Minute 2:00)

Alarm: devops-agent-demo-dev-high-5xx-errors
Configuration:
    Threshold: 10 errors
    Evaluation Periods: 2
    Period: 60 seconds
    
Data Points:
    Period 1 (minute 1): 15 errors ✓ (exceeds 10)
    Period 2 (minute 2): 12 errors ✓ (exceeds 10)
    
Result: 2 out of 2 periods exceeded threshold

What happens:

  • Alarm checks if threshold is exceeded
  • Needs 2 consecutive periods > 10 errors
  • Both periods have > 10 errors
  • Alarm state changes: OK → ALARM

STEP 9: SNS Notification (Minute 2:01)

CloudWatch Alarm
    ↓
Publishes to SNS Topic
    ↓
arn:aws:sns:us-east-1:851725505881:devops-agent-demo-dev-alerts

What happens:

  • Alarm triggers SNS notification
  • SNS topic receives alarm message
  • SNS looks up all subscriptions

SNS Topic has 2 subscriptions:

  1. Email: vanshshah174@gmail.com
  2. Lambda: devops-agent-demo-dev-playbook

STEP 10A: Email Notification (Minute 2:02)

SNS → Email Service → vanshshah174@gmail.com

Email you receive:

Subject: ALARM: "devops-agent-demo-dev-high-5xx-errors" in US East (N. Virginia)

You are receiving this email because your Amazon CloudWatch Alarm 
"devops-agent-demo-dev-high-5xx-errors" in the US East (N. Virginia) 
region has entered the ALARM state.

Alarm Details:
- Name: devops-agent-demo-dev-high-5xx-errors
- Description: This metric monitors 5XX errors
- State Change: OK -> ALARM
- Reason: Threshold Crossed: 2 datapoints [15.0, 12.0] were greater 
  than the threshold (10.0)
- Timestamp: Wednesday 18 February, 2026 21:47:01 UTC

Threshold:
- The alarm is in the ALARM state when the metric is GreaterThanThreshold 
  10.0 for at least 2 of the last 2 period(s) of 60 seconds.

View this alarm in the AWS Management Console:
https://console.aws.amazon.com/cloudwatch/...

STEP 10B: Lambda Playbook Triggered (Minute 2:02)

SNS → Lambda Function → devops-agent-demo-dev-playbook

Lambda receives:

{
    "AlarmName": "devops-agent-demo-dev-high-5xx-errors",
    "NewStateValue": "ALARM",
    "NewStateReason": "Threshold Crossed: 2 datapoints...",
    "Trigger": {
        "MetricName": "HTTPCode_Target_5XX_Count",
        "Threshold": 10.0
    }
}

Lambda code executes:

// 1. Parse alarm
const alarmName = message.AlarmName;
const newState = message.NewStateValue;

// 2. Check if ALARM state
if (newState === 'ALARM') {
    
    // 3. Determine action based on alarm name
    if (alarmName.includes('high-5xx-errors')) {
        
        // 4. Execute remediation
        await restartECSService();
        
        // 5. Send notification
        await sendNotification('Restarted service');
    }
}

STEP 11: Automated Remediation (Minute 2:03)

Lambda → AWS ECS API
    ↓
UpdateService(
    cluster: devops-agent-demo-dev-cluster,
    service: devops-agent-demo-dev-service,
    forceNewDeployment: true
)

What happens:

  • Lambda calls ECS API
  • ECS starts new deployment
  • Creates new tasks with fresh containers
  • Drains connections from old tasks
  • Terminates old tasks
  • New tasks start serving traffic

ECS Service Events:

21:47:03 - (service devops-agent-demo-dev-service) has started 1 tasks
21:47:05 - (service devops-agent-demo-dev-service) registered 1 targets
21:47:10 - (service devops-agent-demo-dev-service) has reached steady state
21:47:15 - (service devops-agent-demo-dev-service) deregistered 1 targets
21:47:20 - (service devops-agent-demo-dev-service) stopped 1 running tasks

STEP 12: Playbook Notification (Minute 2:04)

Lambda → SNS → Email

Second email you receive:

Subject: ✅ Playbook: Restart ECS Service

✅ Playbook Execution Report

Alarm: devops-agent-demo-dev-high-5xx-errors
Action: Restart ECS Service
Result: Restarted service devops-agent-demo-dev-service. 
        Deployment ID: ecs-svc/1234567890
Status: SUCCESS
Timestamp: 2026-02-18T21:47:04.000Z

This was an automated remediation action.

STEP 13: DevOps Agent Analysis (Minute 2:05)

DevOps Agent (Background Process)
    ↓
Polls CloudWatch Alarms (via IAM role)
    ↓
Detects alarm state change
    ↓
Analyzes logs and metrics

What the agent does:

  1. Detects: Alarm changed to ALARM state
  2. Collects:
    • Last 100 log entries
    • Metrics for past hour
    • ECS service events
    • ALB target health
  3. Analyzes:
    • Error patterns in logs
    • Correlation with deployments
    • Resource utilization trends
  4. Stores: Analysis results (would show in UI if available)

Agent's IAM permissions allow it to:

✓ Read CloudWatch alarms
✓ Read CloudWatch logs
✓ Query CloudWatch metrics
✓ Describe ECS services
✓ Read S3 logs
✓ Check ALB health

STEP 14: Service Recovery (Minute 2:10)

New ECS Tasks:
    Task 1 (new) → Healthy ✓
    
Old ECS Tasks:
    Task 1 (old) → Terminated ✗

What happens:

  • New container starts with fresh state
  • Bug still exists in code, but container is clean
  • Temporary fix until code is patched
  • Service is responding normally

STEP 15: Metrics Normalize (Minute 3:00)

CloudWatch Metrics:
    HTTPCode_Target_5XX_Count = 0 errors (new period)

What happens:

  • No new errors in this period
  • Service is healthy
  • Metrics return to normal

STEP 16: Alarm Recovery (Minute 4:00)

Alarm Evaluation:
    Period 1: 0 errors ✓ (below 10)
    Period 2: 0 errors ✓ (below 10)
    
Result: 2 out of 2 periods below threshold
Alarm state changes: ALARM → OK

What happens:

  • Alarm evaluates again
  • Both periods are now below threshold
  • Alarm state changes: ALARM → OK

STEP 17: Recovery Notification (Minute 4:01)

SNS → Email

Third email you receive:

Subject: OK: "devops-agent-demo-dev-high-5xx-errors" in US East (N. Virginia)

You are receiving this email because your Amazon CloudWatch Alarm 
"devops-agent-demo-dev-high-5xx-errors" in the US East (N. Virginia) 
region has returned to the OK state.

Alarm Details:
- Name: devops-agent-demo-dev-high-5xx-errors
- State Change: ALARM -> OK
- Reason: Threshold Crossed: 2 datapoints [0.0, 0.0] were not greater 
  than the threshold (10.0)
- Timestamp: Wednesday 18 February, 2026 21:49:01 UTC

STEP 18: Log Export to S3 (Minute 5:00)

CloudWatch Logs → S3 Export Task → S3 Bucket

What happens:

  • Periodic export task runs (or manual trigger)
  • Exports logs from CloudWatch to S3
  • Stores in: s3://devops-agent-demo-dev-logs-851725505881/cloudwatch-logs/
  • DevOps Agent can analyze historical logs from S3

Complete Timeline Summary

Time Component Action
0:00 User Sends request
0:01 ALB Routes to ECS
0:02 App Crashes with error
0:03 CloudWatch Logs Receives ERROR log
0:04 CloudWatch Metrics Records 5XX error
1:00 CloudWatch Aggregates metrics (15 errors)
2:00 CloudWatch Alarm Evaluates and triggers
2:01 SNS Publishes notification
2:02 Email You receive alarm email
2:02 Lambda Playbook triggered
2:03 ECS Service restart initiated
2:04 Email You receive playbook report
2:05 DevOps Agent Analyzes incident
2:10 ECS New tasks healthy
3:00 CloudWatch Metrics normalize
4:00 CloudWatch Alarm Returns to OK
4:01 Email You receive recovery email

Total incident duration: 4 minutes Automated remediation: 1 minute Human intervention required: ZERO ✅


Data Flow Diagram

┌─────────────────────────────────────────────────────────────┐
│                         USER REQUEST                         │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│                  APPLICATION LOAD BALANCER                   │
│  • Routes traffic                                            │
│  • Health checks                                             │
│  • Collects metrics                                          │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│                      ECS CLUSTER                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  ECS Task (Docker Container)                        │   │
│  │  ┌───────────────────────────────────────────────┐ │   │
│  │  │  Node.js Application                          │ │   │
│  │  │  • Processes requests                         │ │   │
│  │  │  • Logs errors                                │ │   │
│  │  │  • Returns responses                          │ │   │
│  │  └───────────────────────────────────────────────┘ │   │
│  └─────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│                      CLOUDWATCH                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐     │
│  │ Logs         │  │ Metrics      │  │ Alarms       │     │
│  │ • ERROR logs │  │ • 5XX count  │  │ • Evaluates  │     │
│  │ • Structured │  │ • CPU usage  │  │ • Triggers   │     │
│  │ • Searchable │  │ • Memory     │  │ • Notifies   │     │
│  └──────────────┘  └──────────────┘  └──────────────┘     │
└─────────────────────────────────────────────────────────────┘
                              ↓
┌─────────────────────────────────────────────────────────────┐
│                      SNS TOPIC                               │
│  devops-agent-demo-dev-alerts                                │
│  • Receives alarm notifications                              │
│  • Fans out to subscribers                                   │
└─────────────────────────────────────────────────────────────┘
                    ↓                    ↓
        ┌───────────────────┐  ┌───────────────────┐
        │  EMAIL            │  │  LAMBDA           │
        │  vanshshah174@    │  │  Playbook         │
        │  gmail.com        │  │  Function         │
        └───────────────────┘  └───────────────────┘
                                        ↓
                              ┌───────────────────┐
                              │  ECS API          │
                              │  • Restart        │
                              │  • Scale          │
                              │  • Deploy         │
                              └───────────────────┘
                                        ↓
                              ┌───────────────────┐
                              │  REMEDIATION      │
                              │  • New tasks      │
                              │  • Fresh state    │
                              │  • Service healed │
                              └───────────────────┘

┌─────────────────────────────────────────────────────────────┐
│                   DEVOPS AGENT (Parallel)                    │
│  • Monitors via IAM role                                     │
│  • Analyzes logs and metrics                                 │
│  • Correlates events                                         │
│  • Stores analysis                                           │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│                   S3 LOGS BUCKET                             │
│  • Historical log storage                                    │
│  • Long-term analysis                                        │
│  • Compliance/audit trail                                    │
└─────────────────────────────────────────────────────────────┘

Key Insights

1. Multiple Parallel Processes

  • Email notification and Lambda playbook run simultaneously
  • DevOps Agent monitors independently via IAM
  • All three get the same information but act differently

2. No Single Point of Failure

  • If Lambda fails, you still get email
  • If email fails, Lambda still remediates
  • DevOps Agent works independently

3. Complete Audit Trail

  • CloudWatch Logs: What happened
  • CloudWatch Metrics: How bad it was
  • Alarm History: When it triggered
  • Lambda Logs: What was done
  • S3 Logs: Long-term storage

4. Self-Healing

  • System detects problem
  • System fixes problem
  • System notifies you
  • No human intervention needed

What Makes This Enterprise-Grade

✅ Automated Detection - CloudWatch monitors 24/7 ✅ Intelligent Alerting - Threshold-based with evaluation periods ✅ Multi-Channel Notification - Email + Lambda + Agent ✅ Automated Remediation - Self-healing via playbooks ✅ AI Analysis - DevOps Agent provides insights ✅ Complete Observability - Logs, metrics, traces ✅ Audit Trail - Everything logged and stored ✅ Scalable - Handles any traffic volume ✅ Cost-Effective - Pay only for what you use ✅ Production-Ready - Battle-tested AWS services


Congratulations! 🎉

You've built a complete, enterprise-grade, self-healing DevOps platform that:

  • Monitors your application 24/7
  • Detects problems automatically
  • Fixes issues without human intervention
  • Notifies you of everything
  • Provides AI-powered analysis
  • Maintains complete audit trails

This is the same system used by Fortune 500 companies! 🚀