{
  "id": "POffv5hVuoeobllp",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AWS EC2 Health Check & Self-Healing Workflow",
  "tags": [],
  "nodes": [
    {
      "id": "1fe26a10-f8db-4d29-a7e7-fe8670182ed0",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1008,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 */5 * * *"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "60c75175-8955-486a-9da5-cbdc727f1d0a",
      "name": "Loop Over Instances",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -560,
        320
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "ee3a6583-4f75-4f67-b412-7d4f364521be",
      "name": "Check Health Status",
      "type": "n8n-nodes-base.if",
      "position": [
        -112,
        320
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.State.Name }}",
              "rightValue": "running"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "cdc0d0a1-a031-4aef-825d-8b94902e67dc",
      "name": "Analyze Health Data",
      "type": "n8n-nodes-base.code",
      "position": [
        112,
        320
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const instanceId = $input.item.json.InstanceId;\nconst instanceState = $input.item.json.State.Name;\nconst instanceType = $input.item.json.InstanceType;\nconst launchTime = $input.item.json.LaunchTime;\n\n// Check if instance has been unhealthy for more than 10 minutes\nconst now = new Date();\nconst launch = new Date(launchTime);\nconst uptimeMinutes = (now - launch) / (1000 * 60);\n\nreturn {\n  instanceId,\n  instanceState,\n  instanceType,\n  uptimeMinutes: Math.round(uptimeMinutes),\n  isUnhealthy: instanceState !== 'running',\n  actionRequired: instanceState !== 'running' && uptimeMinutes > 10,\n  timestamp: now.toISOString()\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "f5e70419-3daf-4ea0-b230-33957395e6a5",
      "name": "Send WhatsApp Alert",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        560,
        128
      ],
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/{{ $credentials.accountSid }}/Messages.json",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "whatsapp:+1234567890"
            },
            {
              "name": "From",
              "value": "whatsapp:+1234567890"
            },
            {
              "name": "Body",
              "value": "\ud83d\udea8 EC2 Alert: Instance {{ $json.instanceId }} was unhealthy ({{ $json.instanceState }}) and has been restarted. Uptime: {{ $json.uptimeMinutes }} mins"
            }
          ]
        },
        "nodeCredentialType": "twilioApi"
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "c38a83a0-7b42-4023-9bab-8271bcbe6d95",
      "name": "Send Email Alert",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        560,
        320
      ],
      "parameters": {
        "options": {},
        "subject": "EC2 Health Alert - Instance Restarted",
        "toEmail": "user@example.com",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "885409bd-ce9b-4618-a2c1-7676acc63729",
      "name": "Log to AlertsLog Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        560,
        512
      ],
      "parameters": {
        "columns": {
          "value": {},
          "schema": [],
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "your-spreadsheet-id",
          "cachedResultName": "EC2 Health Logs"
        },
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "958ee33b-5761-4e68-8d48-0e0c3c4d0fb2",
      "name": "End Workflow",
      "type": "n8n-nodes-base.noOp",
      "position": [
        784,
        320
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3846c7aa-fa51-494b-bb69-19ffd7006b0b",
      "name": "Sticky Note 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1100,
        -32
      ],
      "parameters": {
        "color": 5,
        "width": 936,
        "height": 512,
        "content": "## \ud83d\udccb Workflow Configuration\n\n**Runs every 5 minutes via cron schedule**\n\n- Checks all Production EC2 instances\n- Monitors health status\n- Auto-remediation enabled"
      },
      "typeVersion": 1
    },
    {
      "id": "9257144a-0f0d-4b7c-bbf0-6de3bc61f7c2",
      "name": "Sticky Note 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        -208
      ],
      "parameters": {
        "color": 5,
        "width": 368,
        "height": 884,
        "content": "## \ud83d\udce7 Alert Notifications\n\n**Sends detailed report to team:**\n- Email with full details\n- WhatsApp for instant alerts\n- Logs to Google Sheets\n\n**All alerts include:**\n- Instance ID\n- Health status\n- Action taken\n- Timestamp"
      },
      "typeVersion": 1
    },
    {
      "id": "f8151614-7dcc-48f0-a0c0-4999cf449916",
      "name": "Sticky Note 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -144,
        -32
      ],
      "parameters": {
        "color": 5,
        "width": 604,
        "height": 508,
        "content": "## \u2699\ufe0f Self-Healing Actions\n\n**Automated remediation:**\n1. Detects unhealthy instances\n2. Attempts restart\n3. Monitors recovery\n4. Alerts team\n\n**Fail-safe:** Only acts on instances unhealthy for 10+ minutes"
      },
      "typeVersion": 1
    },
    {
      "id": "cce52f9c-9750-46c8-a709-3c340e21b368",
      "name": "Get EC2 Instances",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -784,
        320
      ],
      "parameters": {
        "url": "https://devops.oneclicksales.xyz/ec2",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "3f4d342e-c049-4250-8276-936539971101",
      "name": "Check Instance Status",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -336,
        248
      ],
      "parameters": {
        "url": "https://devops.oneclicksales.xyz/ec2status",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "b8ca3d5e-a070-4dcb-a06f-cecabc0adafb",
      "name": "Restart Instance",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        336,
        320
      ],
      "parameters": {
        "url": "https://devops.oneclicksales.xyz/ec2restart",
        "options": {}
      },
      "typeVersion": 4.2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "482dd056-21c7-46ef-96c9-6399996902b5",
  "connections": {
    "Restart Instance": {
      "main": [
        [
          {
            "node": "Send WhatsApp Alert",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send Email Alert",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log to AlertsLog Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get EC2 Instances",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email Alert": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get EC2 Instances": {
      "main": [
        [
          {
            "node": "Loop Over Instances",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Health Data": {
      "main": [
        [
          {
            "node": "Restart Instance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Health Status": {
      "main": [
        [
          {
            "node": "Analyze Health Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Instances",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Instances": {
      "main": [
        [],
        [
          {
            "node": "Check Instance Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Alert": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Instance Status": {
      "main": [
        [
          {
            "node": "Check Health Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log to AlertsLog Sheet": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}