| π― 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.
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
π
terraform applynever runs autonomously β all changes require explicit human PR approval
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.
| 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)
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
- Python 3.11+
uvpackage manager
uv pip install -r requirements.txtcp .env.example .env
# Fill in your API keysRequired 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) |
uv run python -m rag.ingest --docs-dir ./docsThis 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.
uv run python -m evaluation.ragas_evalMust score >= 0.85 on Faithfulness and Answer Relevancy before deploying to production.
uv run python main.pyThe 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.
# 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-
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.
-
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
PROTECTEDorORPHANEDwith a confidence score and justification. -
Decision:
ORPHANEDresources proceed to remediation.PROTECTEDresources are skipped and logged. When all resources are processed, the graph terminates. -
Remediate: For
ORPHANEDresources, the Remediator:- Asks Gemini to identify the relevant
.tffile in the GitHub repo - Asks Gemini to modify the file (sets
count = 0on 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
- Asks Gemini to identify the relevant
-
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
--approvedor--rejected. No infrastructure is modified until the PR is manually merged andterraform applyis run by a human.
| 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 |
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.
| 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 |
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.
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.