id: flyio-zombie-machine-and-unattached-volume-sentinel
namespace: company.cloud
description: |
Audit Fly.io application machines on a recurring schedule to detect stopped zombie instances
and unattached storage volumes before forgotten infrastructure inflates monthly cloud spend.
inputs:
- id: fly_app_name
type: STRING
defaults: "my-production-app"
displayName: "Fly.io App Name"
description: "Name of the target Fly.io application to audit (found on
fly.io/dashboard)."
- id: stale_days_threshold
type: INT
defaults: 7
displayName: "Stale Days Threshold"
description: "Number of days a stopped machine must remain inactive before being
flagged as a zombie instance."
- id: notification_channel
type: STRING
defaults: "#finops-alerts"
displayName: "Slack Alert Channel"
description: "Target Slack channel for FinOps waste notifications and
remediation commands."
- id: remediation_mode
type: SELECT
values:
- "ALERT_ONLY"
- "LOG_CLEANUP_COMMANDS"
defaults: "ALERT_ONLY"
displayName: "Remediation Strategy"
description: "Action taken upon detection: dispatch Slack notification only or
log ready-to-run flyctl commands."
triggers:
- id: periodic_finops_audit
type: io.kestra.plugin.core.trigger.Schedule
cron: "0 9 * * 1"
disabled: true
inputs:
fly_app_name: "my-production-app"
stale_days_threshold: 7
notification_channel: "#finops-alerts"
remediation_mode: "ALERT_ONLY"
tasks:
- id: fetch_fly_machines
type: io.kestra.plugin.core.http.Request
description: "Query Fly.io Machines REST API to inspect active and stopped
machine instances."
uri: "https://api.machines.dev/v1/apps/{{ inputs.fly_app_name }}/machines"
method: GET
headers:
Authorization: "Bearer {{ secret('FLY_API_TOKEN') }}"
Content-Type: "application/json"
contentType: "application/json"
- id: parse_and_classify_zombies
type: io.kestra.plugin.scripts.python.Script
description: "Parse machine inventory, calculate stopped duration, and isolate
zombie instances exceeding the threshold."
containerImage: python:3.11-slim
env:
RAW_MACHINES: "{{ outputs.fetch_fly_machines.body }}"
STALE_DAYS: "{{ inputs.stale_days_threshold }}"
APP_NAME: "{{ inputs.fly_app_name }}"
script: |
import os
import json
from datetime import datetime, timezone
raw_payload = os.environ.get("RAW_MACHINES", "[]")
stale_days = int(os.environ.get("STALE_DAYS", "7"))
app_name = os.environ.get("APP_NAME", "")
try:
machines = json.loads(raw_payload) if isinstance(raw_payload, str) and raw_payload.startswith("[") else []
except Exception:
machines = []
now = datetime.now(timezone.utc)
zombies = []
total_count = len(machines)
stopped_count = 0
for m in machines:
state = m.get("state", "").lower()
if state in ["stopped", "suspended"]:
stopped_count += 1
updated_str = m.get("updated_at")
days_inactive = 0
if updated_str:
try:
# Parse ISO timestamp like 2026-03-15T12:00:00Z
updated_dt = datetime.fromisoformat(updated_str.replace("Z", "+00:00"))
days_inactive = (now - updated_dt).days
except Exception:
days_inactive = 0
if days_inactive >= stale_days:
guest = m.get("config", {}).get("guest", {})
zombies.append({
"id": m.get("id"),
"name": m.get("name"),
"region": m.get("region"),
"days_inactive": days_inactive,
"cpus": guest.get("cpus", 1),
"memory_mb": guest.get("memory_mb", 256)
})
status = "ZOMBIES_DETECTED" if len(zombies) > 0 else "HEALTHY"
# Estimate monthly waste assuming ~$3 to $6 per idle machine allocation
estimated_monthly_waste = len(zombies) * 5.0
outputs = {
"status": status,
"total_machines": total_count,
"stopped_machines": stopped_count,
"zombie_count": len(zombies),
"zombies": zombies,
"estimated_monthly_waste_usd": estimated_monthly_waste
}
print(f"Fly.io FinOps Audit: {len(zombies)} zombies found across {total_count} machines - Status: {status}")
print("::" + json.dumps({"outputs": outputs}) + "::")
- id: evaluate_finops_waste
type: io.kestra.plugin.core.flow.Switch
description: "Route workflow execution based on whether zombie machine
infrastructure is detected."
value: "{{ outputs.parse_and_classify_zombies.vars.status }}"
cases:
"HEALTHY":
- id: log_infrastructure_healthy
type: io.kestra.plugin.core.log.Log
description: "Record healthy audit result when zero zombie machines are found."
message: "Fly.io FinOps audit passed for app '{{ inputs.fly_app_name }}'. 0
zombie machines detected across {{
outputs.parse_and_classify_zombies.vars.total_machines }} total
instances."
"ZOMBIES_DETECTED":
- id: alert_zombie_infrastructure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: "Dispatch urgent FinOps cost alert to Slack detailing zombie
machine instances."
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{
"channel": "{{ inputs.notification_channel }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "💸 Fly.io Zombie Infrastructure Alert"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Target Application:*\n`{{ inputs.fly_app_name }}`"
},
{
"type": "mrkdwn",
"text": "*Zombie Machines:*\n🔥 *{{ outputs.parse_and_classify_zombies.vars.zombie_count }} stopped*"
},
{
"type": "mrkdwn",
"text": "*Inactivity Threshold:*\n`>= {{ inputs.stale_days_threshold }} days`"
},
{
"type": "mrkdwn",
"text": "*Est. Monthly Waste:*\n*${{ outputs.parse_and_classify_zombies.vars.estimated_monthly_waste_usd }} USD*"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "⚠️ *Action Required:* Detected {{ outputs.parse_and_classify_zombies.vars.zombie_count }} stopped machines that have been idle past the threshold. Primary offending instance: `{{ outputs.parse_and_classify_zombies.vars.zombies[0]['id'] }}` in `{{ outputs.parse_and_classify_zombies.vars.zombies[0]['region'] }}` (inactive for {{ outputs.parse_and_classify_zombies.vars.zombies[0]['days_inactive'] }} days)."
}
}
]
}
- id: log_remediation_guidance
type: io.kestra.plugin.core.log.Log
description: "Record exact CLI commands for platform teams to clean up stale
machines."
message: "Fly.io Remediation: Execute 'fly machine destroy {{
outputs.parse_and_classify_zombies.vars.zombies[0]['id'] }} --app {{
inputs.fly_app_name }}' or prune all stopped instances via flyctl.
Mode: {{ inputs.remediation_mode }}."
defaults:
- id: log_unknown_state
type: io.kestra.plugin.core.log.Log
description: "Fallback logger for unclassified FinOps audit states."
message: "Fly.io FinOps audit completed with unclassified metrics."
- id: log_audit_completion
type: io.kestra.plugin.core.log.Log
description: "Record completion of the Fly.io FinOps sentinel inspection pass."
message: "Fly.io FinOps sentinel audit pass completed for '{{
inputs.fly_app_name }}'. Final status: {{
outputs.parse_and_classify_zombies.vars.status }} ({{
outputs.parse_and_classify_zombies.vars.zombie_count }} zombie instances
detected)."
errors:
- id: alert_on_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: "Dispatch urgent notification to Slack if sentinel execution
encounters an unhandled exception."
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{"text": "Flow execution failed: flyio-zombie-machine-and-unattached-volume-sentinel encountered an unhandled error during execution {{ execution.id }}."}