AWS EC2 健康检查与自愈工作流
这是一个DevOps领域的自动化工作流,包含 14 个节点。主要使用 If, Code, EmailSend, HttpRequest, GoogleSheets 等节点。 监控 AWS EC2 实例健康状态并实现多通道告警自愈
- •可能需要目标 API 的认证凭证
- •Google Sheets API 凭证
分类
{
"id": "POffv5hVuoeobllp",
"meta": {
"instanceId": "dd69efaf8212c74ad206700d104739d3329588a6f3f8381a46a481f34c9cc281",
"templateCredsSetupCompleted": true
},
"name": "AWS EC2 健康检查与自愈工作流",
"tags": [],
"nodes": [
{
"id": "1fe26a10-f8db-4d29-a7e7-fe8670182ed0",
"name": "计划触发器",
"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": "循环遍历实例",
"type": "n8n-nodes-base.splitInBatches",
"position": [
-560,
320
],
"parameters": {
"options": {}
},
"typeVersion": 3
},
{
"id": "ee3a6583-4f75-4f67-b412-7d4f364521be",
"name": "检查健康状态",
"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": "分析健康数据",
"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": "发送 WhatsApp 警报",
"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:+14155238886"
},
{
"name": "Body",
"value": "🚨 EC2 Alert: Instance {{ $json.instanceId }} was unhealthy ({{ $json.instanceState }}) and has been restarted. Uptime: {{ $json.uptimeMinutes }} mins"
}
]
},
"nodeCredentialType": "twilioApi"
},
"credentials": {
"twilioApi": {
"id": "4XZXejd5UmXan7os",
"name": "Twilio account-test"
}
},
"typeVersion": 4.2
},
{
"id": "c38a83a0-7b42-4023-9bab-8271bcbe6d95",
"name": "发送邮件警报",
"type": "n8n-nodes-base.emailSend",
"position": [
560,
320
],
"webhookId": "9cea1304-5fed-4a8b-84f4-00f6a6578ab6",
"parameters": {
"options": {},
"subject": "EC2 Health Alert - Instance Restarted",
"toEmail": "devops-team@yourcompany.com",
"fromEmail": "alerts@yourcompany.com"
},
"credentials": {
"smtp": {
"id": "G1kyF8cSWTZ4vouN",
"name": "SMTP -test"
}
},
"typeVersion": 2.1
},
{
"id": "885409bd-ce9b-4618-a2c1-7676acc63729",
"name": "记录到 AlertsLog 表格",
"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": {
"id": "ScSS2KxGQULuPtdy",
"name": "Google Sheets- test"
}
},
"typeVersion": 4.5
},
{
"id": "958ee33b-5761-4e68-8d48-0e0c3c4d0fb2",
"name": "结束工作流",
"type": "n8n-nodes-base.noOp",
"position": [
784,
320
],
"parameters": {},
"typeVersion": 1
},
{
"id": "3846c7aa-fa51-494b-bb69-19ffd7006b0b",
"name": "便签 1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1100,
-32
],
"parameters": {
"color": 5,
"width": 936,
"height": 512,
"content": "## 📋 工作流配置"
},
"typeVersion": 1
},
{
"id": "9257144a-0f0d-4b7c-bbf0-6de3bc61f7c2",
"name": "便签 2",
"type": "n8n-nodes-base.stickyNote",
"position": [
544,
-208
],
"parameters": {
"color": 5,
"width": 368,
"height": 884,
"content": "## 📧 警报通知"
},
"typeVersion": 1
},
{
"id": "f8151614-7dcc-48f0-a0c0-4999cf449916",
"name": "便签 3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-144,
-32
],
"parameters": {
"color": 5,
"width": 604,
"height": 508,
"content": "## ⚙️ 自愈操作"
},
"typeVersion": 1
},
{
"id": "cce52f9c-9750-46c8-a709-3c340e21b368",
"name": "获取 EC2 实例",
"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": "检查实例状态",
"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": "重启实例",
"type": "n8n-nodes-base.httpRequest",
"position": [
336,
320
],
"parameters": {
"url": "https://devops.oneclicksales.xyz/ec2restart",
"options": {}
},
"typeVersion": 4.2
}
],
"active": false,
"pinData": {},
"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
}
]
]
}
}
}如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
中级 - 开发运维
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
Oneclick AI Squad
@oneclick-aiThe AI Squad Initiative is a pioneering effort to build, automate and scale AI-powered workflows using n8n.io. Our mission is to help individuals and businesses integrate AI agents seamlessly into their daily operations from automating tasks and enhancing productivity to creating innovative, intelligent solutions. We design modular, reusable AI workflow templates that empower creators, developers and teams to supercharge their automation with minimal effort and maximum impact.
分享此工作流