diff --git a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/02-a2a-protocol/cleanup.py b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/02-a2a-protocol/cleanup.py index 152c648ed..4649fc95d 100644 --- a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/02-a2a-protocol/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/02-a2a-protocol/cleanup.py @@ -7,6 +7,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -31,15 +32,17 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete S3 code artifact @@ -47,7 +50,7 @@ def main(): bucket = f"agentcore-code-{account_id}-{region}" s3.delete_object(Bucket=bucket, Key=f"{agent_name}/code.zip") print(" Deleted S3 code artifact") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") role_name = f"agentcore-{agent_name}-role" @@ -56,8 +59,8 @@ def main(): 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") if os.path.exists("runtime_config.json"): os.remove("runtime_config.json") diff --git a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/03-ag-ui-protocol/cleanup.py b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/03-ag-ui-protocol/cleanup.py index 06cc5be9a..9f6c2d814 100644 --- a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/03-ag-ui-protocol/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/03-ag-ui-protocol/cleanup.py @@ -7,6 +7,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -31,15 +32,17 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete S3 code artifact @@ -47,7 +50,7 @@ def main(): bucket = f"agentcore-code-{account_id}-{region}" s3.delete_object(Bucket=bucket, Key=f"{agent_name}/code.zip") print(" Deleted S3 code artifact") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") role_name = f"agentcore-{agent_name}-role" @@ -56,8 +59,8 @@ def main(): 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") if os.path.exists("runtime_config.json"): os.remove("runtime_config.json") diff --git a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/04-observability-with-strands/cleanup.py b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/04-observability-with-strands/cleanup.py index 4f385aa28..623e8a665 100644 --- a/01-features/02-host-your-agent/01-runtime/01-hosting-agents/04-observability-with-strands/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/01-hosting-agents/04-observability-with-strands/cleanup.py @@ -16,6 +16,7 @@ import time import boto3 +from botocore.exceptions import ClientError # ── Load Config ──────────────────────────────────────────────────────────────── @@ -49,11 +50,13 @@ def cleanup(config): eps = control.list_agent_runtime_endpoints(agentRuntimeId=runtime_id) for ep in eps.get("runtimeEndpoints", []): ep_name = ep["name"] - control.delete_agent_runtime_endpoint(agentRuntimeId=runtime_id, name=ep_name) + if ep_name == "DEFAULT": + continue # DEFAULT endpoint is auto-deleted with the runtime + control.delete_agent_runtime_endpoint(agentRuntimeId=runtime_id, endpointName=ep_name) print(f" Deleted endpoint: {ep_name}") # Wait for deletion time.sleep(10) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete runtime @@ -61,7 +64,7 @@ def cleanup(config): try: control.delete_agent_runtime(agentRuntimeId=runtime_id) print(" Runtime deletion initiated") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete IAM role @@ -72,7 +75,7 @@ def cleanup(config): iam.delete_role_policy(RoleName=role_name, PolicyName=p) iam.delete_role(RoleName=role_name) print(f" Deleted role: {role_name}") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete S3 artifacts @@ -80,7 +83,7 @@ def cleanup(config): try: s3.delete_object(Bucket=s3_bucket, Key=s3_prefix) print(" Deleted S3 object") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") print("\nCleanup complete.") diff --git a/01-features/02-host-your-agent/01-runtime/02-hosting-tools/01-mcp-server-basics/cleanup.py b/01-features/02-host-your-agent/01-runtime/02-hosting-tools/01-mcp-server-basics/cleanup.py index e387e82f4..0bb568dee 100644 --- a/01-features/02-host-your-agent/01-runtime/02-hosting-tools/01-mcp-server-basics/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/02-hosting-tools/01-mcp-server-basics/cleanup.py @@ -7,6 +7,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -31,15 +32,17 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # Delete S3 code artifact @@ -47,7 +50,7 @@ def main(): bucket = f"agentcore-code-{account_id}-{region}" s3.delete_object(Bucket=bucket, Key=f"{agent_name}/code.zip") print(" Deleted S3 code artifact") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") role_name = f"agentcore-{agent_name}-role" @@ -56,8 +59,8 @@ def main(): 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") if os.path.exists("runtime_config.json"): os.remove("runtime_config.json") diff --git a/01-features/02-host-your-agent/01-runtime/02-hosting-tools/02-mcp-server-features/cleanup.py b/01-features/02-host-your-agent/01-runtime/02-hosting-tools/02-mcp-server-features/cleanup.py index 8366f8e8d..da81430bd 100644 --- a/01-features/02-host-your-agent/01-runtime/02-hosting-tools/02-mcp-server-features/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/02-hosting-tools/02-mcp-server-features/cleanup.py @@ -7,6 +7,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -29,23 +30,25 @@ def main(): 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") role_name = f"agentcore-{agent_name}-role" iam = boto3.client("iam", region_name=region) @@ -53,8 +56,8 @@ def main(): 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") if os.path.exists("runtime_config.json"): os.remove("runtime_config.json") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/01-streaming-responses/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/01-streaming-responses/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/01-streaming-responses/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/01-streaming-responses/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/02-session-management/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/02-session-management/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/02-session-management/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/02-session-management/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/01_weekly_report_generator_async/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/01_weekly_report_generator_async/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/01_weekly_report_generator_async/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/01_weekly_report_generator_async/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/02_async_data_analysis/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/02_async_data_analysis/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/02_async_data_analysis/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/04-async-agents/02_async_data_analysis/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/05-execute-commands/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/05-execute-commands/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/05-execute-commands/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/05-execute-commands/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/06-multi-agent/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/06-multi-agent/cleanup.py index d7269486d..0cb95ac5c 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/06-multi-agent/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/06-multi-agent/cleanup.py @@ -12,6 +12,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -38,22 +39,24 @@ def main(): # Delete endpoints 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"]) - except Exception as e: + except ClientError as e: print(f" Warning (endpoints): {e}") # Delete runtime try: control.delete_agent_runtime(agentRuntimeId=runtime_id) print(" ✓ Runtime deleted") - except Exception as e: + except ClientError as e: print(f" Warning (runtime): {e}") # Delete S3 code try: s3.delete_object(Bucket=bucket, Key=f"{agent_name}/code.zip") - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # Delete IAM role role_name = f"agentcore-{agent_name}-role" @@ -62,8 +65,8 @@ def main(): iam.delete_role_policy(RoleName=role_name, PolicyName=p) iam.delete_role(RoleName=role_name) print(" ✓ IAM role deleted") - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # Wait for deletions to propagate print("\n Waiting for deletions to complete...") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/07-persistent-filesystems/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/07-persistent-filesystems/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/07-persistent-filesystems/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/07-persistent-filesystems/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/08-connect-to-vpc-resources/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/08-connect-to-vpc-resources/cleanup.py index 6030ace7c..24115e82c 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/08-connect-to-vpc-resources/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/08-connect-to-vpc-resources/cleanup.py @@ -20,6 +20,7 @@ import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def load_config() -> dict: @@ -51,11 +52,13 @@ def main(): for ep in endpoints.get("runtimeEndpoints", []): name = ep["name"] print(f" Deleting endpoint: {name}") + if name == "DEFAULT": + continue # DEFAULT endpoint is auto-deleted with the runtime control.delete_agent_runtime_endpoint(agentRuntimeId=runtime_id, endpointName=name) if endpoints.get("runtimeEndpoints"): print(" Waiting for endpoint deletion...") time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # 2. Delete runtime @@ -64,7 +67,7 @@ def main(): control.delete_agent_runtime(agentRuntimeId=runtime_id) print(" Waiting for runtime deletion...") time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # 3. Delete S3 code artifact @@ -73,7 +76,7 @@ def main(): try: s3.delete_object(Bucket=bucket_name, Key=s3_key) print(f" Deleted s3://{bucket_name}/{s3_key}") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # 4. Delete IAM role @@ -86,7 +89,7 @@ def main(): print(f" Deleted IAM role: {role_name}") except iam.exceptions.NoSuchEntityException: print(f" IAM role not found: {role_name}") - except Exception as e: + except ClientError as e: print(f" Warning: {e}") # 5. Destroy CDK infrastructure diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/01-server-e2e/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/01-server-e2e/cleanup.py index 3901895a9..c4ac78855 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/01-server-e2e/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/01-server-e2e/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -28,29 +30,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # Delete DynamoDB table try: @@ -59,7 +63,7 @@ def main(): table.delete() table.wait_until_not_exists() print(f"✓ DynamoDB table '{dynamo_table}' deleted") - except Exception as e: + except ClientError as e: print(f" Warning (DynamoDB): {e}") if os.path.exists("runtime_config.json"): diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/02-client-e2e/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/02-client-e2e/cleanup.py index 3901895a9..c4ac78855 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/02-client-e2e/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/02-client-e2e/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -28,29 +30,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # Delete DynamoDB table try: @@ -59,7 +63,7 @@ def main(): table.delete() table.wait_until_not_exists() print(f"✓ DynamoDB table '{dynamo_table}' deleted") - except Exception as e: + except ClientError as e: print(f" Warning (DynamoDB): {e}") if os.path.exists("runtime_config.json"): diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/03-utilities-e2e/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/03-utilities-e2e/cleanup.py index 3901895a9..c4ac78855 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/03-utilities-e2e/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/09-mcp-e2e/03-utilities-e2e/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -28,29 +30,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") # Delete DynamoDB table try: @@ -59,7 +63,7 @@ def main(): table.delete() table.wait_until_not_exists() print(f"✓ DynamoDB table '{dynamo_table}' deleted") - except Exception as e: + except ClientError as e: print(f" Warning (DynamoDB): {e}") if os.path.exists("runtime_config.json"): diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/10-mcp-dynamic-client-registration/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/10-mcp-dynamic-client-registration/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/10-mcp-dynamic-client-registration/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/10-mcp-dynamic-client-registration/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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") diff --git a/01-features/02-host-your-agent/01-runtime/03-advanced/11-middleware-support/cleanup.py b/01-features/02-host-your-agent/01-runtime/03-advanced/11-middleware-support/cleanup.py index e016914c0..567eb135e 100644 --- a/01-features/02-host-your-agent/01-runtime/03-advanced/11-middleware-support/cleanup.py +++ b/01-features/02-host-your-agent/01-runtime/03-advanced/11-middleware-support/cleanup.py @@ -4,8 +4,10 @@ import os import sys import time + import boto3 from boto3.session import Session +from botocore.exceptions import ClientError def main(): @@ -27,29 +29,31 @@ def main(): 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 Exception as e: + except ClientError as e: print(f" Warning: {e}") try: control.delete_agent_runtime(agentRuntimeId=runtime_id) time.sleep(30) - except Exception as e: + except ClientError as e: print(f" Warning: {e}") try: boto3.client("s3", region_name=region).delete_object( Bucket=f"agentcore-code-{account_id}-{region}", Key=f"{agent_name}/code.zip" ) - except Exception: - pass + except ClientError: + print(" Warning: cleanup resource not found or already deleted") 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 Exception: - pass + 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")