From 059ada350390bb5bbc746ac91c28335c19e3078b Mon Sep 17 00:00:00 2001 From: Workshop Participant Date: Sat, 29 Aug 2026 01:13:45 +0000 Subject: [PATCH 1/2] fix: cleanup scripts skip DEFAULT endpoint to prevent resource leaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The AgentCore API now auto-manages DEFAULT endpoints — they cannot be explicitly deleted. cleanup.py scripts that attempt to delete all endpoints (including DEFAULT) get a ConflictException, which then prevents the runtime from being deleted. Resources are leaked despite the script reporting "Cleanup complete". Fix: Skip DEFAULT endpoints during explicit deletion, matching the pattern already used in 01-strands-bedrock/cleanup.py. Also fixes the observability cleanup which used wrong parameter name (name= instead of endpointName=). Verified by live deployment testing: deploy → invoke → cleanup now succeeds without leaking runtimes. Co-Authored-By: Claude Opus 4.6 --- .../01-runtime/01-hosting-agents/02-a2a-protocol/cleanup.py | 2 ++ .../01-runtime/01-hosting-agents/03-ag-ui-protocol/cleanup.py | 2 ++ .../04-observability-with-strands/cleanup.py | 4 +++- .../02-hosting-tools/01-mcp-server-basics/cleanup.py | 2 ++ .../02-hosting-tools/02-mcp-server-features/cleanup.py | 2 ++ .../01-runtime/03-advanced/01-streaming-responses/cleanup.py | 2 ++ .../01-runtime/03-advanced/02-session-management/cleanup.py | 2 ++ .../01_weekly_report_generator_async/cleanup.py | 2 ++ .../04-async-agents/02_async_data_analysis/cleanup.py | 2 ++ .../01-runtime/03-advanced/05-execute-commands/cleanup.py | 2 ++ .../01-runtime/03-advanced/06-multi-agent/cleanup.py | 2 ++ .../03-advanced/07-persistent-filesystems/cleanup.py | 2 ++ .../03-advanced/08-connect-to-vpc-resources/cleanup.py | 2 ++ .../03-advanced/09-mcp-e2e/01-server-e2e/cleanup.py | 2 ++ .../03-advanced/09-mcp-e2e/02-client-e2e/cleanup.py | 2 ++ .../03-advanced/09-mcp-e2e/03-utilities-e2e/cleanup.py | 2 ++ .../03-advanced/10-mcp-dynamic-client-registration/cleanup.py | 2 ++ .../01-runtime/03-advanced/11-middleware-support/cleanup.py | 2 ++ 18 files changed, 37 insertions(+), 1 deletion(-) 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..f84c5db68 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 @@ -31,6 +31,8 @@ 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: 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..c6a7cdc10 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 @@ -31,6 +31,8 @@ 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: 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..a3c5113df 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 @@ -49,7 +49,9 @@ 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) 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..7ad478d4a 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 @@ -31,6 +31,8 @@ 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: 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..4d03d7b0a 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 @@ -29,6 +29,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..44ef0dec7 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 @@ -38,6 +38,8 @@ 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: print(f" Warning (endpoints): {e}") 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..ddc8f6efd 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 @@ -51,6 +51,8 @@ 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...") 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..d7b2873dd 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 @@ -28,6 +28,8 @@ 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: 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..d7b2873dd 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 @@ -28,6 +28,8 @@ 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: 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..d7b2873dd 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 @@ -28,6 +28,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: 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..4532132d9 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 @@ -27,6 +27,8 @@ 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: From be4849828bbb56c8755f67006b8f8db41c50390b Mon Sep 17 00:00:00 2001 From: Workshop User Date: Mon, 31 Aug 2026 22:05:15 +0000 Subject: [PATCH 2/2] fix: resolve ruff linting violations in cleanup scripts Replace broad `except Exception` with `except ClientError` from botocore.exceptions, replace bare `pass` with informative print statements, and fix import sorting. Co-Authored-By: Claude Opus 4.6 --- .../02-a2a-protocol/cleanup.py | 11 ++++++----- .../03-ag-ui-protocol/cleanup.py | 11 ++++++----- .../04-observability-with-strands/cleanup.py | 9 +++++---- .../01-mcp-server-basics/cleanup.py | 11 ++++++----- .../02-mcp-server-features/cleanup.py | 17 +++++++++-------- .../01-streaming-responses/cleanup.py | 14 ++++++++------ .../02-session-management/cleanup.py | 14 ++++++++------ .../01_weekly_report_generator_async/cleanup.py | 14 ++++++++------ .../02_async_data_analysis/cleanup.py | 14 ++++++++------ .../03-advanced/05-execute-commands/cleanup.py | 14 ++++++++------ .../03-advanced/06-multi-agent/cleanup.py | 13 +++++++------ .../07-persistent-filesystems/cleanup.py | 14 ++++++++------ .../08-connect-to-vpc-resources/cleanup.py | 9 +++++---- .../09-mcp-e2e/01-server-e2e/cleanup.py | 16 +++++++++------- .../09-mcp-e2e/02-client-e2e/cleanup.py | 16 +++++++++------- .../09-mcp-e2e/03-utilities-e2e/cleanup.py | 16 +++++++++------- .../cleanup.py | 14 ++++++++------ .../11-middleware-support/cleanup.py | 14 ++++++++------ 18 files changed, 135 insertions(+), 106 deletions(-) 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 f84c5db68..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(): @@ -35,13 +36,13 @@ def main(): 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 @@ -49,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" @@ -58,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 c6a7cdc10..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(): @@ -35,13 +36,13 @@ def main(): 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 @@ -49,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" @@ -58,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 a3c5113df..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 ──────────────────────────────────────────────────────────────── @@ -55,7 +56,7 @@ def cleanup(config): 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 @@ -63,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 @@ -74,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 @@ -82,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 7ad478d4a..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(): @@ -35,13 +36,13 @@ def main(): 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 @@ -49,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" @@ -58,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 4d03d7b0a..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(): @@ -33,21 +34,21 @@ def main(): 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) @@ -55,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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 44ef0dec7..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(): @@ -41,21 +42,21 @@ def main(): 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" @@ -64,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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 ddc8f6efd..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: @@ -57,7 +58,7 @@ def main(): 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 @@ -66,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 @@ -75,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 @@ -88,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 d7b2873dd..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(): @@ -32,27 +34,27 @@ def main(): 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: @@ -61,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 d7b2873dd..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(): @@ -32,27 +34,27 @@ def main(): 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: @@ -61,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 d7b2873dd..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(): @@ -32,27 +34,27 @@ def main(): 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: @@ -61,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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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 4532132d9..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(): @@ -31,27 +33,27 @@ def main(): 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")