Skip to content

DevMLAI01/autonomous-sre-finops

Repository files navigation

Autonomous SRE & Cloud FinOps Orchestrator

🎯 CPU Threshold πŸ’° Cost Threshold πŸ“Š RAG Quality Gate πŸ”’ Safety
< 5% avg 7-day > $100/month Faithfulness β‰₯ 0.85 Zero autonomous terraform apply

An enterprise-grade agentic AI system that autonomously detects underutilized AWS resources, validates them against internal documentation via RAG, and raises Terraform IaC Pull Requests for human-approved remediation β€” with zero direct infrastructure changes.

100% free-tier infrastructure. Zero cloud spend beyond existing AWS resources.


πŸ—οΈ Architecture

A 5-node LangGraph orchestrator that detects underutilized AWS resources, classifies them via RAG, auto-generates Terraform PRs, and enforces mandatory human approval before any infrastructure change.

flowchart TD
    A([⏰ Trigger\nManual or Scheduled]) --> B

    subgraph GRAPH [LangGraph Orchestrator Β· SQLite Checkpoints]
        direction TB

        B[πŸ” Investigator\nEC2 DescribeInstances\nCloudWatch 7-day CPU avg]
        B --> C

        C[πŸ“š RAG Retriever\nQdrant vector search\nGemini classification]
        C --> D

        D{🚦 Decision Gate\nConfidence threshold}
        D -->|ORPHANED β‰₯ 0.70| E
        D -->|PROTECTED or\nconfidence < 0.70| SKIP

        E[πŸ”§ Remediator\nJinja2 Terraform patch\nGitHub PR Β· idempotency guard]
        E --> F

        F{πŸ‘€ HITL Gate\n⏸ Graph interrupt\nAwaits human decision}
    end

    F -->|βœ… python main.py --approved| G([βœ… PR ready to merge\nAudit trail logged])
    F -->|❌ python main.py --rejected| H([🚫 PR closed\nState archived])
    SKIP([πŸ›‘οΈ Resource protected\nNo action taken])

    subgraph AWS [AWS Β· Read-Only IAM]
        I[EC2 DescribeInstances]
        J[CloudWatch GetMetricStatistics]
    end

    subgraph OBS [Observability]
        K[LangSmith Traces]
        L[Ragas Evaluation\nFaithfulness β‰₯ 0.85]
    end

    B --- I & J
    C --- K
    C --- L

    style GRAPH fill:#1a1a2e,stroke:#4a9eff,color:#fff
    style AWS fill:#1a2e1a,stroke:#4aff9e,color:#fff
    style OBS fill:#2e2a1a,stroke:#ffcc4a,color:#fff
    style F fill:#2e1a2e,stroke:#cc4aff,color:#fff
    style D fill:#2e1a1a,stroke:#ff4a4a,color:#fff
Loading

πŸ”’ terraform apply never runs autonomously β€” all changes require explicit human PR approval


πŸ’Ό Why This Matters

Cloud waste from idle EC2 instances is one of the largest controllable infrastructure costs in enterprise AWS environments. This system is designed to autonomously surface orphaned resources (CPU < 5% over 7 days, cost > $100/month), validate them against internal policy via RAG, and raise reviewed Terraform PRs β€” with zero autonomous infrastructure changes and a full audit trail on every decision.


Tech Stack

Layer Tool Free Tier
Orchestration LangGraph + SQLite checkpointing Yes
LLM gemini-2.5-flash-lite (Google AI Studio) 1000 req/day
Embeddings gemini-embedding-001 (3072 dims) Yes
RAG Vector DB Qdrant Serverless 1 GB
Observability LangSmith 5000 traces/month
RAG Evaluation Ragas 0.4.3 Open source
AWS Access Boto3 via MCP (read-only IAM) IAM-controlled
IaC Terraform via GitHub PRs GitHub free
Tool Access Model Context Protocol (MCP) Open source

Total monthly cost: $0 (all free tiers)


Project Structure

autonomous-sre-finops/
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ llm_client.py          # Gemini LLM + embeddings + LangSmith tracing
β”‚   β”œβ”€β”€ investigator.py        # Node 1: AWS anomaly detection via MCP
β”‚   β”œβ”€β”€ rag_retriever.py       # Node 2: Qdrant RAG assessment
β”‚   β”œβ”€β”€ decision.py            # Node 3: REMEDIATE / SKIP / DONE routing
β”‚   β”œβ”€β”€ remediator.py          # Node 4: Terraform patch + GitHub PR via MCP
β”‚   └── hitl_gate.py           # Node 5: LangGraph interrupt + notification
β”œβ”€β”€ mcp_servers/
β”‚   β”œβ”€β”€ aws_mcp_server.py      # Read-only EC2 + CloudWatch (stdio MCP)
β”‚   └── github_mcp_server.py   # PR creation with idempotency guard (stdio MCP)
β”œβ”€β”€ graph/
β”‚   β”œβ”€β”€ state.py               # OrchestratorState TypedDict
β”‚   └── orchestrator.py        # Graph assembly + AsyncSqliteSaver checkpoint
β”œβ”€β”€ rag/
β”‚   β”œβ”€β”€ ingest.py              # Embed + upsert docs into Qdrant
β”‚   └── retriever.py           # RAG query + Gemini assessment
β”œβ”€β”€ evaluation/
β”‚   └── ragas_eval.py          # CI/CD quality gate (Faithfulness + AnswerRelevancy >= 0.85)
β”œβ”€β”€ notifications/
β”‚   └── notifier.py            # Slack webhook + email notification
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ sample_architecture.md # Architecture documentation
β”‚   β”œβ”€β”€ resource_registry.md   # Protected resource registry
β”‚   └── project_status.md      # Active/decommissioned projects
β”œβ”€β”€ terraform_templates/
β”‚   └── scale_down.tf.jinja    # Jinja2 template for Terraform patches
β”œβ”€β”€ tests/
β”œβ”€β”€ main.py                    # CLI entry point
β”œβ”€β”€ config.py                  # Centralised env var config
β”œβ”€β”€ requirements.txt
└── .env.example

Setup

Prerequisites

  • Python 3.11+
  • uv package manager

1. Install dependencies

uv pip install -r requirements.txt

2. Configure environment

cp .env.example .env
# Fill in your API keys

Required credentials:

Variable Where to get it
GOOGLE_API_KEY Google AI Studio (free)
LANGCHAIN_API_KEY LangSmith (free dev tier)
AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY Read-only IAM user (see policy below)
AWS_DEFAULT_REGION Your EC2 region (e.g. ap-south-1)
GITHUB_TOKEN GitHub PAT with repo scope
GITHUB_REPO_OWNER / GITHUB_REPO_NAME Terraform repo to open PRs against
QDRANT_URL / QDRANT_API_KEY Qdrant Cloud (free tier)
SLACK_WEBHOOK_URL Slack incoming webhook (optional)

3. Ingest internal docs into Qdrant

uv run python -m rag.ingest --docs-dir ./docs

This embeds your architecture docs, resource registry, and project status files into the vector store so the RAG retriever can assess whether a flagged resource is protected.

4. Run the RAG quality gate

uv run python -m evaluation.ragas_eval

Must score >= 0.85 on Faithfulness and Answer Relevancy before deploying to production.

5. Run the orchestrator

uv run python main.py

The workflow pauses at the HITL gate and prints a thread_id. The graph state is persisted to checkpoints.db (SQLite) so it survives process restarts.

6. Resume after human review

# Approve the PR and trigger terraform apply planning:
uv run python main.py --resume --thread-id <thread_id> --approved

# Reject (resource stays as-is):
uv run python main.py --resume --thread-id <thread_id> --rejected

How it works β€” step by step

  1. Investigate: The Investigator agent queries AWS via a read-only MCP server. It flags EC2 instances with average CPU < 5% over 7 days and estimated cost > $100/month.

  2. RAG Retrieve: For each flagged instance, the RAG Retriever embeds the resource metadata and searches Qdrant for relevant internal docs (architecture decisions, resource registry, active projects). A Gemini LLM then classifies the resource as PROTECTED or ORPHANED with a confidence score and justification.

  3. Decision: ORPHANED resources proceed to remediation. PROTECTED resources are skipped and logged. When all resources are processed, the graph terminates.

  4. Remediate: For ORPHANED resources, the Remediator:

    • Asks Gemini to identify the relevant .tf file in the GitHub repo
    • Asks Gemini to modify the file (sets count = 0 on the instance resource)
    • Creates a GitHub PR with full audit trail (CPU%, cost, RAG justification, LangSmith trace)
    • Includes idempotency guard: skips if a PR for this instance already exists
  5. HITL Gate: The graph interrupts (pauses execution). A notification is sent to Slack/email with the PR link. The SRE engineer reviews the PR and resumes the graph with --approved or --rejected. No infrastructure is modified until the PR is manually merged and terraform apply is run by a human.


Security & Guardrails

Guardrail Implementation
No direct execution terraform apply is strictly prohibited; all changes via GitHub PRs
Least privilege AWS credentials are read-only (EC2 Describe + CloudWatch only)
Conservative RAG Resource defaults to PROTECTED if LLM cannot parse context
PR idempotency Skips PR creation if an open PR already exists for the instance
RAG quality gate Ragas Faithfulness + AnswerRelevancy >= 0.85 required
HITL mandatory No change proceeds without explicit human approval via --approved
State persistence SQLite checkpointing β€” graph survives crashes, supports HITL resume
Audit trail Every PR includes LangSmith trace URL, CPU%, cost, RAG justification

AWS IAM Policy (Read-Only)

The AWS credentials only need these permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "cloudwatch:GetMetricStatistics",
        "ec2:DescribeInstances"
      ],
      "Resource": "*"
    }
  ]
}

Cost Explorer (ce:GetCostAndUsage) is not required. Monthly cost is estimated from on-demand pricing based on instance type β€” no additional IAM permissions needed.


Thresholds (configurable in .env)

Variable Default Description
CPU_UTILIZATION_THRESHOLD 5.0 Flag instances with avg CPU below this %
MONTHLY_COST_THRESHOLD 100.0 Only flag if estimated cost exceeds this USD/mo
LOOKBACK_DAYS 7 CloudWatch lookback window for CPU averages
RAGAS_MIN_FAITHFULNESS 0.85 Minimum RAG faithfulness score for CI gate
RAGAS_MIN_ANSWER_RELEVANCY 0.85 Minimum RAG answer relevancy for CI gate

LangSmith Observability

Every LLM call is traced to LangSmith automatically via LANGCHAIN_TRACING_V2=true. Each remediation PR includes the LangSmith trace URL for full audit trail.

Set LANGCHAIN_PROJECT in .env to organise traces by project.


License

This project is intended for educational and demonstration purposes only.

  • You may use, study, and modify this code for personal learning and non-commercial projects.
  • Do not deploy this system against production AWS infrastructure without thorough security review.
  • The author provides no warranties and accepts no liability for any damages arising from use of this code.
  • Third-party services used (Google Gemini, Qdrant, LangSmith, GitHub, AWS) are subject to their own terms of service.

About

Autonomous SRE & Cloud FinOps Orchestrator: LangGraph + Gemini + Qdrant + MCP to detect underutilized AWS resources and open Terraform PRs with HITL approval

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors