File size: 1,582 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""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