-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcleanup.py
More file actions
71 lines (57 loc) · 2.17 KB
/
Copy pathcleanup.py
File metadata and controls
71 lines (57 loc) · 2.17 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Clean up all A2A agent resources. Usage: python cleanup.py"""
import json
import os
import sys
import time
import boto3
from boto3.session import Session
from botocore.exceptions import ClientError
def main():
try:
with open("runtime_config.json") as f:
config = json.load(f)
except FileNotFoundError:
print("Error: runtime_config.json not found.")
sys.exit(1)
agent_name, runtime_id, region = (
config["agent_name"],
config["runtime_id"],
config["region"],
)
account_id = Session(region_name=region).client("sts").get_caller_identity()["Account"]
control = boto3.client("bedrock-agentcore-control", region_name=region)
s3 = boto3.client("s3", region_name=region)
print(f"Cleaning up: {agent_name}\n")
try:
for ep in control.list_agent_runtime_endpoints(agentRuntimeId=runtime_id).get("runtimeEndpoints", []):
if ep["name"] == "DEFAULT":
continue # DEFAULT endpoint is auto-deleted with the runtime
control.delete_agent_runtime_endpoint(agentRuntimeId=runtime_id, endpointName=ep["name"])
time.sleep(30)
except ClientError as e:
print(f" Warning: {e}")
try:
control.delete_agent_runtime(agentRuntimeId=runtime_id)
time.sleep(30)
except ClientError as e:
print(f" Warning: {e}")
# Delete S3 code artifact
try:
bucket = f"agentcore-code-{account_id}-{region}"
s3.delete_object(Bucket=bucket, Key=f"{agent_name}/code.zip")
print(" Deleted S3 code artifact")
except ClientError as e:
print(f" Warning: {e}")
role_name = f"agentcore-{agent_name}-role"
iam = boto3.client("iam", region_name=region)
try:
for p in iam.list_role_policies(RoleName=role_name).get("PolicyNames", []):
iam.delete_role_policy(RoleName=role_name, PolicyName=p)
iam.delete_role(RoleName=role_name)
except ClientError:
print(" Warning: cleanup resource not found or already deleted")
if os.path.exists("runtime_config.json"):
os.remove("runtime_config.json")
print("✓ Cleanup complete")
if __name__ == "__main__":
main()