Download deployment/aws/lambda/budget_guard.py from Wizzas/DefectRAG: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/Wizzas/DefectRAG/resolve/main/deployment/aws/lambda/budget_guard.py
- Command line
-
hf download hf://Wizzas/DefectRAG/deployment/aws/lambda/budget_guard.py
-
curl -L -o budget_guard.py https://huggingface.co/Wizzas/DefectRAG/resolve/main/deployment/aws/lambda/budget_guard.py
1.58 kB
| """SNS-triggered cost guard: stops EC2 instances tagged Project=DefectRAG-lab. | |
| Runtime: Python 3.12. Handler: budget_guard.handler. Trigger: SNS topic that AWS Budgets publishes to. | |
| It never terminates anything and ignores every instance without the tag. | |
| Test safely with the event {"dry_run": true}: it lists the instances and stops nothing. | |
| """ | |
| import json | |
| import logging | |
| import boto3 | |
| log = logging.getLogger() | |
| log.setLevel(logging.INFO) | |
| TAG_KEY = "Project" | |
| TAG_VALUE = "DefectRAG-lab" | |
| def running_lab_instances(ec2): | |
| found = [] | |
| pages = ec2.get_paginator("describe_instances").paginate( | |
| Filters=[ | |
| {"Name": f"tag:{TAG_KEY}", "Values": [TAG_VALUE]}, | |
| {"Name": "instance-state-name", "Values": ["pending", "running"]}, | |
| ] | |
| ) | |
| for page in pages: | |
| for reservation in page["Reservations"]: | |
| found.extend(item["InstanceId"] for item in reservation["Instances"]) | |
| return found | |
| def handler(event, context): | |
| dry_run = bool(event.get("dry_run")) if isinstance(event, dict) else False | |
| from_budget = isinstance(event, dict) and "Records" in event | |
| if from_budget: | |
| for record in event["Records"]: | |
| log.info("Budget message: %s", str(record.get("Sns", {}).get("Message", ""))[:500]) | |
| ec2 = boto3.client("ec2") | |
| ids = running_lab_instances(ec2) | |
| result = {"matched": ids, "dry_run": dry_run, "stopped": []} | |
| if ids and not dry_run: | |
| ec2.stop_instances(InstanceIds=ids) | |
| result["stopped"] = ids | |
| log.info("Result: %s", json.dumps(result)) | |
| return result | |