Repository navigation
Expand file tree
/
Copy pathdestroy.sh
More file actions
executable file
·693 lines (586 loc) · 33.8 KB
/
Copy pathdestroy.sh
File metadata and controls
executable file
·693 lines (586 loc) · 33.8 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/bin/bash
set -uo pipefail
# MSP Assistant AgentCore Cleanup Script
# Usage: ./destroy.sh [--region us-east-1] [--force] [--keep-bootstrap]
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
REGION="us-east-1"
FORCE=false
KEEP_BOOTSTRAP=false
while [[ $# -gt 0 ]]; do
case $1 in
--region) REGION="$2"; shift 2 ;;
--force) FORCE=true; shift ;;
--keep-bootstrap) KEEP_BOOTSTRAP=true; shift ;;
*) echo "Usage: ./destroy.sh [--region us-east-1] [--force] [--keep-bootstrap]"; exit 1 ;;
esac
done
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
CFN_EXEC_ROLE="cdk-hnb659fds-cfn-exec-role-${ACCOUNT_ID}-${REGION}"
echo -e "${RED}╔════════════════════════════════════════════╗${NC}"
echo -e "${RED}║ MSP Assistant → Full Teardown ║${NC}"
echo -e "${RED}╚════════════════════════════════════════════╝${NC}"
echo ""
echo "Region: $REGION | Account: $ACCOUNT_ID"
echo ""
if [ "$FORCE" != true ]; then
echo -e "${YELLOW}⚠️ WARNING: This will delete ALL deployed resources!${NC}"
read -p "Are you sure? (yes/no): " confirm
[ "$confirm" != "yes" ] && echo "Cancelled." && exit 0
fi
# ─── Helpers ──────────────────────────────────────────────────────────────────
log() { echo -e "${GREEN}✓${NC} $1"; }
warn() { echo -e "${YELLOW}⚠️ $1${NC}"; }
step() { echo -e "\n${YELLOW}[$1] $2${NC}"; }
# Filter AWS CLI "None" output so for-loops iterate zero times on empty results
filter_none() { grep -v '^None$' || true; }
delete_versioned_bucket() {
local bucket=$1
aws s3api head-bucket --bucket "$bucket" --region "$REGION" 2>/dev/null || return 0
echo " Emptying $bucket..."
# Delete current objects (non-versioned) in batch
aws s3 rm "s3://$bucket" --recursive --region "$REGION" 2>/dev/null || true
# Delete all versions in batch (up to 1000 per call)
local versions=$(aws s3api list-object-versions --bucket "$bucket" --region "$REGION" \
--query '{Objects: Versions[].{Key:Key,VersionId:VersionId}}' --output json 2>/dev/null)
if [ -n "$versions" ] && [ "$versions" != "null" ] && echo "$versions" | jq -e '.Objects | length > 0' >/dev/null 2>&1; then
echo " Deleting $(echo "$versions" | jq '.Objects | length') object versions..."
echo "$versions" | aws s3api delete-objects --bucket "$bucket" --region "$REGION" --delete file:///dev/stdin >/dev/null 2>&1 || true
fi
# Delete all delete markers in batch (up to 1000 per call)
local markers=$(aws s3api list-object-versions --bucket "$bucket" --region "$REGION" \
--query '{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}' --output json 2>/dev/null)
if [ -n "$markers" ] && [ "$markers" != "null" ] && echo "$markers" | jq -e '.Objects | length > 0' >/dev/null 2>&1; then
echo " Deleting $(echo "$markers" | jq '.Objects | length') delete markers..."
echo "$markers" | aws s3api delete-objects --bucket "$bucket" --region "$REGION" --delete file:///dev/stdin >/dev/null 2>&1 || true
fi
aws s3api delete-bucket --bucket "$bucket" --region "$REGION" 2>/dev/null && log "Deleted bucket: $bucket" || warn "Bucket $bucket may already be deleted"
}
delete_iam_role() {
local role=$1
aws iam get-role --role-name "$role" >/dev/null 2>&1 || return 0
for arn in $(aws iam list-attached-role-policies --role-name "$role" --query 'AttachedPolicies[].PolicyArn' --output text 2>/dev/null | filter_none); do
aws iam detach-role-policy --role-name "$role" --policy-arn "$arn" 2>/dev/null
done
for p in $(aws iam list-role-policies --role-name "$role" --query 'PolicyNames[]' --output text 2>/dev/null | filter_none); do
aws iam delete-role-policy --role-name "$role" --policy-name "$p" 2>/dev/null
done
for ip in $(aws iam list-instance-profiles-for-role --role-name "$role" --query 'InstanceProfiles[].InstanceProfileName' --output text 2>/dev/null | filter_none); do
aws iam remove-role-from-instance-profile --instance-profile-name "$ip" --role-name "$role" 2>/dev/null
done
aws iam delete-role --role-name "$role" 2>/dev/null && log "Deleted role: $role"
}
delete_vpc() {
local vpc_id=$1
aws ec2 describe-vpcs --vpc-ids "$vpc_id" --region "$REGION" >/dev/null 2>&1 || return 0
echo " Tearing down VPC $vpc_id..."
local nat_ids=$(aws ec2 describe-nat-gateways --region "$REGION" \
--filter "Name=vpc-id,Values=$vpc_id" "Name=state,Values=available,pending" \
--query 'NatGateways[].NatGatewayId' --output text | filter_none)
for nat in $nat_ids; do
aws ec2 delete-nat-gateway --nat-gateway-id "$nat" --region "$REGION" >/dev/null 2>&1
echo " Deleting NAT $nat..."
done
if [ -n "$nat_ids" ]; then
echo " Waiting for NAT gateways (up to 90s)..."
local elapsed=0
while [ $elapsed -lt 90 ]; do
local remaining=$(aws ec2 describe-nat-gateways --region "$REGION" \
--filter "Name=vpc-id,Values=$vpc_id" "Name=state,Values=available,pending,deleting" \
--query 'NatGateways[].NatGatewayId' --output text | filter_none)
[ -z "$remaining" ] && break
sleep 10; elapsed=$((elapsed + 10))
done
fi
for ep in $(aws ec2 describe-vpc-endpoints --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'VpcEndpoints[].VpcEndpointId' --output text | filter_none); do
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids "$ep" --region "$REGION" >/dev/null 2>&1
done
for fl in $(aws ec2 describe-flow-logs --region "$REGION" --filter "Name=resource-id,Values=$vpc_id" \
--query 'FlowLogs[].FlowLogId' --output text | filter_none); do
aws ec2 delete-flow-logs --flow-log-ids "$fl" --region "$REGION" >/dev/null 2>&1
done
for sg in $(aws ec2 describe-security-groups --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'SecurityGroups[?GroupName!=`default`].GroupId' --output text | filter_none); do
for rule in $(aws ec2 describe-security-group-rules --region "$REGION" --filters "Name=group-id,Values=$sg" \
--query 'SecurityGroupRules[?!IsEgress].SecurityGroupRuleId' --output text | filter_none); do
aws ec2 revoke-security-group-ingress --group-id "$sg" --security-group-rule-ids "$rule" --region "$REGION" >/dev/null 2>&1
done
for rule in $(aws ec2 describe-security-group-rules --region "$REGION" --filters "Name=group-id,Values=$sg" \
--query 'SecurityGroupRules[?IsEgress].SecurityGroupRuleId' --output text | filter_none); do
aws ec2 revoke-security-group-egress --group-id "$sg" --security-group-rule-ids "$rule" --region "$REGION" >/dev/null 2>&1
done
done
for sg in $(aws ec2 describe-security-groups --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'SecurityGroups[?GroupName!=`default`].GroupId' --output text | filter_none); do
aws ec2 delete-security-group --group-id "$sg" --region "$REGION" 2>/dev/null
done
for eni in $(aws ec2 describe-network-interfaces --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'NetworkInterfaces[].NetworkInterfaceId' --output text | filter_none); do
aws ec2 delete-network-interface --network-interface-id "$eni" --region "$REGION" 2>/dev/null
done
for s in $(aws ec2 describe-subnets --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'Subnets[].SubnetId' --output text | filter_none); do
aws ec2 delete-subnet --subnet-id "$s" --region "$REGION" 2>/dev/null
done
for rt in $(aws ec2 describe-route-tables --region "$REGION" --filters "Name=vpc-id,Values=$vpc_id" \
--query 'RouteTables[?Associations[0].Main!=`true`].RouteTableId' --output text | filter_none); do
for assoc in $(aws ec2 describe-route-tables --region "$REGION" --route-table-ids "$rt" \
--query 'RouteTables[0].Associations[?!Main].RouteTableAssociationId' --output text | filter_none); do
aws ec2 disassociate-route-table --association-id "$assoc" --region "$REGION" 2>/dev/null
done
aws ec2 delete-route-table --route-table-id "$rt" --region "$REGION" 2>/dev/null
done
for igw in $(aws ec2 describe-internet-gateways --region "$REGION" \
--filters "Name=attachment.vpc-id,Values=$vpc_id" --query 'InternetGateways[].InternetGatewayId' --output text | filter_none); do
aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc_id" --region "$REGION" 2>/dev/null
aws ec2 delete-internet-gateway --internet-gateway-id "$igw" --region "$REGION" 2>/dev/null
done
for eip in $(aws ec2 describe-addresses --region "$REGION" \
--filters "Name=tag:aws:cloudformation:stack-name,Values=MSPAssistant*" \
--query 'Addresses[].AllocationId' --output text | filter_none); do
aws ec2 release-address --allocation-id "$eip" --region "$REGION" 2>/dev/null
done
aws ec2 delete-vpc --vpc-id "$vpc_id" --region "$REGION" 2>/dev/null && log "Deleted VPC $vpc_id" || warn "VPC $vpc_id may need manual cleanup"
}
delete_cfn_stack() {
local stack=$1
local status=$(aws cloudformation describe-stacks --stack-name "$stack" --region "$REGION" \
--query 'Stacks[0].StackStatus' --output text 2>&1) || return 0
echo " $stack ($status)"
if ! aws cloudformation delete-stack --stack-name "$stack" --region "$REGION" 2>/dev/null; then
warn "$stack delete failed (likely missing exec role)"
return 1
fi
local elapsed=0
while [ $elapsed -lt 120 ]; do
sleep 10; elapsed=$((elapsed + 10))
status=$(aws cloudformation describe-stacks --stack-name "$stack" --region "$REGION" \
--query 'Stacks[0].StackStatus' --output text 2>&1) || { log "$stack deleted"; return 0; }
[[ "$status" == *"does not exist"* ]] && { log "$stack deleted"; return 0; }
[ "$status" = "DELETE_FAILED" ] && break
done
if [ "$status" = "DELETE_FAILED" ]; then
local retain=$(aws cloudformation list-stack-resources --stack-name "$stack" --region "$REGION" \
--query 'StackResourceSummaries[?ResourceStatus!=`DELETE_COMPLETE`].LogicalResourceId' --output text 2>/dev/null | filter_none)
if [ -n "$retain" ]; then
aws cloudformation delete-stack --stack-name "$stack" --region "$REGION" --retain-resources $retain 2>/dev/null
sleep 15
aws cloudformation describe-stacks --stack-name "$stack" --region "$REGION" >/dev/null 2>&1 || { log "$stack deleted (with retain)"; return 0; }
fi
warn "$stack may still exist in DELETE_FAILED state"
fi
}
delete_secrets_by_prefix() {
local prefix=$1
echo " Deleting secrets with prefix: $prefix"
local secrets=$(aws secretsmanager list-secrets --region "$REGION" \
--filters Key=name,Values="${prefix}" \
--query 'SecretList[].Name' --output text 2>/dev/null | filter_none)
[ -z "$secrets" ] && echo " No secrets found" && return 0
for secret in $secrets; do
aws secretsmanager delete-secret --secret-id "$secret" --force-delete-without-recovery \
--region "$REGION" >/dev/null 2>&1 && log "Deleted secret: $secret" || warn "Failed: $secret"
done
}
delete_cloudwatch_resource_policy() {
local policy_name=$1
echo " Deleting CloudWatch resource policy: $policy_name"
aws logs delete-resource-policy --policy-name "$policy_name" --region "$REGION" >/dev/null 2>&1 && \
log "Deleted policy: $policy_name" || echo " Policy may not exist"
}
delete_dynamodb_table() {
local table_name=$1
echo " Deleting DynamoDB table: $table_name"
aws dynamodb describe-table --table-name "$table_name" --region "$REGION" >/dev/null 2>&1 || return 0
aws dynamodb delete-table --table-name "$table_name" --region "$REGION" >/dev/null 2>&1 && \
log "Deleted table: $table_name" || warn "Failed: $table_name"
}
cleanup_local_files() {
echo " Cleaning up local files..."
[ -f "frontend/.env" ] && rm -f "frontend/.env" && log "Deleted frontend/.env"
[ -f "infrastructure/cdk/outputs.json" ] && rm -f "infrastructure/cdk/outputs.json" && log "Deleted outputs.json"
[ -f "/tmp/jira-openapi.json" ] && rm -f "/tmp/jira-openapi.json" && log "Deleted /tmp/jira-openapi.json"
for runtime_dir in agents/runtime agents/runtime_*; do
[ -f "$runtime_dir/env_config.txt" ] && rm -f "$runtime_dir/env_config.txt" && log "Deleted $runtime_dir/env_config.txt"
done
}
# ─── Step 1: Delete AgentCore resources ───────────────────────────────────────
step "1/9" "Deleting AgentCore resources..."
for gw_id in $(aws bedrock-agentcore-control list-gateways --region "$REGION" \
--query 'items[].gatewayId' --output text 2>/dev/null | filter_none); do
echo " Deleting gateway $gw_id targets..."
for target_id in $(aws bedrock-agentcore-control list-gateway-targets \
--gateway-identifier "$gw_id" --region "$REGION" --query 'items[].targetId' --output text 2>/dev/null | filter_none); do
if ! aws bedrock-agentcore-control delete-gateway-target \
--gateway-identifier "$gw_id" --target-id "$target_id" --region "$REGION" 2>&1; then
warn "Failed to delete gateway target: $target_id"
fi
done
# Wait for ALL targets to fully delete (critical for runtime deletion)
echo " Waiting for targets to delete (up to 120s)..."
elapsed=0
while [ $elapsed -lt 120 ]; do
remaining=$(aws bedrock-agentcore-control list-gateway-targets \
--gateway-identifier "$gw_id" --region "$REGION" \
--query 'items[].targetId' --output text 2>/dev/null | filter_none | wc -w | tr -d ' ')
[ "$remaining" -eq 0 ] && break
sleep 10; elapsed=$((elapsed + 10))
done
if [ "$remaining" -ne 0 ]; then
warn "Gateway targets still exist after 120s, proceeding anyway"
else
log "All gateway targets deleted"
fi
# Delete gateway
if ! aws bedrock-agentcore-control delete-gateway --gateway-identifier "$gw_id" --region "$REGION" 2>&1; then
warn "Failed to delete gateway: $gw_id"
else
log "Gateway deletion initiated: $gw_id"
fi
# Wait for gateway to fully delete (critical for runtime deletion)
echo " Waiting for gateway to delete (up to 120s)..."
elapsed=0
while [ $elapsed -lt 120 ]; do
gw_status=$(aws bedrock-agentcore-control get-gateway --gateway-identifier "$gw_id" --region "$REGION" \
--query 'status' --output text 2>&1)
if [[ "$gw_status" == *"ResourceNotFoundException"* ]] || [[ "$gw_status" == *"does not exist"* ]]; then
log "Gateway fully deleted: $gw_id"
break
fi
sleep 10; elapsed=$((elapsed + 10))
done
if [ $elapsed -ge 120 ]; then
warn "Gateway may still exist after 120s: $gw_id"
fi
done
for cred_name in $(aws bedrock-agentcore-control list-api-key-credential-providers --region "$REGION" \
--query 'credentialProviders[].name' --output text 2>/dev/null | filter_none); do
if ! aws bedrock-agentcore-control delete-api-key-credential-provider --name "$cred_name" --region "$REGION" 2>&1; then
warn "Failed to delete API key: $cred_name"
else
log "Deleted API key: $cred_name"
fi
done
# Delete Cedar Policy Engine (policies must be deleted before engine)
for engine_id in $(aws bedrock-agentcore-control list-policy-engines --region "$REGION" \
--query "policyEngines[?name=='msp-policy-engine'].policyEngineId" --output text 2>/dev/null | filter_none); do
for policy_id in $(aws bedrock-agentcore-control list-policies \
--policy-engine-id "$engine_id" --region "$REGION" \
--query "policies[].policyId" --output text 2>/dev/null | filter_none); do
aws bedrock-agentcore-control delete-policy \
--policy-engine-id "$engine_id" --policy-id "$policy_id" --region "$REGION" 2>/dev/null && \
log "Deleted Cedar policy: $policy_id" || warn "Failed to delete Cedar policy: $policy_id"
done
aws bedrock-agentcore-control delete-policy-engine \
--policy-engine-id "$engine_id" --region "$REGION" 2>/dev/null && \
log "Deleted Cedar policy engine: $engine_id" || warn "Failed to delete Cedar policy engine: $engine_id"
done
# Delete Bedrock Guardrail
for guardrail_id in $(aws bedrock list-guardrails --region "$REGION" \
--query "guardrails[?name=='msp-ops-topic-guardrail'].id" --output text 2>/dev/null | filter_none); do
aws bedrock delete-guardrail --guardrail-identifier "$guardrail_id" --region "$REGION" 2>/dev/null && \
log "Deleted guardrail: $guardrail_id" || warn "Failed to delete guardrail: $guardrail_id"
done
for cred_name in $(aws bedrock-agentcore-control list-oauth2-credential-providers --region "$REGION" \
--query 'credentialProviders[].name' --output text 2>/dev/null | filter_none); do
if ! aws bedrock-agentcore-control delete-oauth2-credential-provider --name "$cred_name" --region "$REGION" 2>&1; then
warn "Failed to delete OAuth2: $cred_name"
else
log "Deleted OAuth2: $cred_name"
fi
done
for mem_id in $(aws bedrock-agentcore-control list-memories --region "$REGION" \
--query 'memories[].id' --output text 2>/dev/null | filter_none); do
if ! aws bedrock-agentcore-control delete-memory --memory-id "$mem_id" --region "$REGION" 2>&1; then
warn "Failed to delete memory: $mem_id"
fi
done
log "Deleted all memories"
# First attempt: Delete all runtimes
echo " Deleting runtimes (first attempt)..."
FAILED_RUNTIMES=""
for rt_id in $(aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null | filter_none); do
DELETE_OUTPUT=$(aws bedrock-agentcore-control delete-agent-runtime --agent-runtime-id "$rt_id" --region "$REGION" 2>&1)
if [ $? -ne 0 ]; then
warn "Failed to delete runtime: $rt_id"
echo " Error: $DELETE_OUTPUT"
FAILED_RUNTIMES="$FAILED_RUNTIMES $rt_id"
else
log "Deleted runtime $rt_id"
fi
done
# Wait for successful deletions to propagate
echo " Waiting for runtime deletions (up to 120s)..."
RT_WAIT=0
while [ $RT_WAIT -lt 120 ]; do
RT_REMAINING=$(aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null | filter_none | wc -w | tr -d ' ')
[ "$RT_REMAINING" -eq 0 ] && break
sleep 10; RT_WAIT=$((RT_WAIT + 10))
done
# Retry failed runtimes if any remain
if [ "$RT_REMAINING" -gt 0 ]; then
echo ""
echo " $RT_REMAINING runtimes still exist, retrying with extended wait..."
echo " (Gateway dependencies may need more time to fully clear)"
# Additional wait for gateway dependencies to clear
sleep 30
# Retry deletion for remaining runtimes
for rt_id in $(aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null | filter_none); do
echo " Retrying runtime: $rt_id"
DELETE_OUTPUT=$(aws bedrock-agentcore-control delete-agent-runtime --agent-runtime-id "$rt_id" --region "$REGION" 2>&1)
if [ $? -ne 0 ]; then
warn "Retry failed for runtime: $rt_id"
echo " Error: $DELETE_OUTPUT"
else
log "Successfully deleted runtime on retry: $rt_id"
fi
done
# Final wait after retry
echo " Waiting for retry deletions (up to 120s)..."
RT_WAIT=0
while [ $RT_WAIT -lt 120 ]; do
RT_REMAINING=$(aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null | filter_none | wc -w | tr -d ' ')
[ "$RT_REMAINING" -eq 0 ] && break
sleep 10; RT_WAIT=$((RT_WAIT + 10))
done
if [ "$RT_REMAINING" -gt 0 ]; then
warn "$RT_REMAINING runtimes still exist after retry"
echo " Remaining runtime IDs:"
aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null | filter_none | tr '\t' '\n' | sed 's/^/ /'
else
log "All runtimes successfully deleted after retry"
fi
fi
# ─── Step 2: Delete ECS ───────────────────────────────────────────────────────
step "2/9" "Deleting ECS resources..."
for cluster_arn in $(aws ecs list-clusters --region "$REGION" \
--query 'clusterArns[?contains(@, `MSP`) || contains(@, `Backend`)]' --output text | filter_none); do
for svc_arn in $(aws ecs list-services --cluster "$cluster_arn" --region "$REGION" \
--query 'serviceArns[]' --output text 2>/dev/null | filter_none); do
aws ecs update-service --cluster "$cluster_arn" --service "$svc_arn" --desired-count 0 --region "$REGION" >/dev/null 2>&1 || true
aws ecs delete-service --cluster "$cluster_arn" --service "$svc_arn" --force --region "$REGION" >/dev/null 2>&1 || true
done
aws ecs delete-cluster --cluster "$cluster_arn" --region "$REGION" >/dev/null 2>&1 || true
log "Deleted ECS cluster"
done
echo " Waiting for ECS tasks to drain (30s)..."
sleep 30
# ─── Step 3: Delete ALB, Target Groups, API Gateway ──────────────────────────
step "3/9" "Deleting ALB, Target Groups, API Gateway..."
for alb_arn in $(aws elbv2 describe-load-balancers --region "$REGION" \
--query 'LoadBalancers[?contains(LoadBalancerName, `MSP`) || contains(LoadBalancerName, `Backe`)].LoadBalancerArn' --output text | filter_none); do
for listener in $(aws elbv2 describe-listeners --load-balancer-arn "$alb_arn" --region "$REGION" \
--query 'Listeners[].ListenerArn' --output text 2>/dev/null | filter_none); do
aws elbv2 delete-listener --listener-arn "$listener" --region "$REGION" 2>/dev/null
done
aws elbv2 delete-load-balancer --load-balancer-arn "$alb_arn" --region "$REGION" 2>/dev/null
log "Deleted ALB"
done
for tg in $(aws elbv2 describe-target-groups --region "$REGION" \
--query 'TargetGroups[?contains(TargetGroupName, `MSP`) || contains(TargetGroupName, `Backe`)].TargetGroupArn' --output text | filter_none); do
aws elbv2 delete-target-group --target-group-arn "$tg" --region "$REGION" 2>/dev/null
done
for api_id in $(aws apigateway get-rest-apis --region "$REGION" \
--query 'items[?contains(name, `msp`) || contains(name, `MSP`) || contains(name, `Backend`)].id' --output text | filter_none); do
aws apigateway delete-rest-api --rest-api-id "$api_id" --region "$REGION" 2>/dev/null
log "Deleted API Gateway $api_id"
done
echo " Waiting for ALB to drain (45s)..."
sleep 45
# ─── Step 4: Delete S3 buckets ───────────────────────────────────────────────
step "4/9" "Deleting S3 buckets..."
for bucket in $(aws s3 ls | awk '{print $3}' | grep -iE 'mspassistant|msp-assistant|bedrock-agentcore-codebuild-sources-'"$ACCOUNT_ID"'-'"$REGION"''); do
delete_versioned_bucket "$bucket"
done
# ─── Step 5: Delete ECR, Lambda, CloudWatch, Cognito, DynamoDB ───────────────
step "5/9" "Deleting ECR, Lambda, CloudWatch, Cognito, DynamoDB..."
for repo in $(aws ecr describe-repositories --region "$REGION" \
--query 'repositories[?contains(repositoryName, `msp`)].repositoryName' --output text 2>/dev/null | filter_none); do
aws ecr delete-repository --repository-name "$repo" --force --region "$REGION" >/dev/null 2>&1 && log "Deleted ECR: $repo"
done
for fn in $(aws lambda list-functions --region "$REGION" \
--query 'Functions[?contains(FunctionName, `MSPAssistant`)].FunctionName' --output text | filter_none); do
aws lambda delete-function --function-name "$fn" --region "$REGION" 2>/dev/null
log "Deleted Lambda $fn"
done
for lg in $(aws logs describe-log-groups --region "$REGION" \
--query 'logGroups[?contains(logGroupName, `MSP`) || contains(logGroupName, `msp`) || contains(logGroupName, `bedrock-agentcore`)].logGroupName' --output text | filter_none); do
aws logs delete-log-group --log-group-name "$lg" --region "$REGION" 2>/dev/null
done
log "Deleted CloudWatch Log Groups"
for policy_name in $(aws logs describe-resource-policies --region "$REGION" \
--query 'resourcePolicies[?contains(policyName, `TransactionSearch`)].policyName' --output text 2>/dev/null | filter_none); do
delete_cloudwatch_resource_policy "$policy_name"
done
for pool_id in $(aws cognito-idp list-user-pools --max-results 60 --region "$REGION" \
--query 'UserPools[?contains(Name, `msp`)].Id' --output text | filter_none); do
domain=$(aws cognito-idp describe-user-pool --user-pool-id "$pool_id" --region "$REGION" \
--query 'UserPool.Domain' --output text 2>/dev/null | filter_none)
[ -n "$domain" ] && aws cognito-idp delete-user-pool-domain --user-pool-id "$pool_id" --domain "$domain" --region "$REGION" 2>/dev/null
aws cognito-idp delete-user-pool --user-pool-id "$pool_id" --region "$REGION" 2>/dev/null
log "Deleted Cognito pool $pool_id"
done
for table in $(aws dynamodb list-tables --region "$REGION" \
--query 'TableNames[?contains(@, `msp-assistant`)]' --output text 2>/dev/null | filter_none); do
delete_dynamodb_table "$table"
done
# ─── Step 5.5: Delete Secrets ─────────────────────────────────────────────────
step "5.5/9" "Deleting Secrets Manager secrets..."
delete_secrets_by_prefix "msp-credentials/"
# ─── Step 6: Delete VPC ──────────────────────────────────────────────────────
step "6/9" "Deleting VPC..."
for vpc_id in $(aws ec2 describe-vpcs --region "$REGION" \
--filters "Name=tag:aws:cloudformation:stack-name,Values=MSPAssistant*" \
--query 'Vpcs[].VpcId' --output text | filter_none); do
delete_vpc "$vpc_id"
done
# ─── Step 7: Delete CloudFormation stacks ─────────────────────────────────────
step "7/9" "Deleting CloudFormation stacks..."
if ! aws iam get-role --role-name "$CFN_EXEC_ROLE" >/dev/null 2>&1; then
warn "CFN exec role missing — recreating..."
aws iam create-role --role-name "$CFN_EXEC_ROLE" \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"cloudformation.amazonaws.com"},"Action":"sts:AssumeRole"}]}' >/dev/null 2>&1
aws iam attach-role-policy --role-name "$CFN_EXEC_ROLE" \
--policy-arn arn:aws:iam::aws:policy/AdministratorAccess 2>/dev/null
echo " Waiting 15s for IAM propagation..."
sleep 15
log "CFN exec role recreated"
fi
# Discover and delete stacks in dependency order (Frontend → Backend → AgentCore → CDKToolkit)
ALL_STACKS=$(aws cloudformation list-stacks --region "$REGION" \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE \
--query 'StackSummaries[?contains(StackName, `MSPAssistant`)].StackName' --output text | filter_none)
# Delete Frontend first (imports from Backend)
for stack in $ALL_STACKS; do
case $stack in
*Frontend*)
delete_cfn_stack "$stack"
# Wait for Frontend to fully delete before proceeding
echo " Waiting for Frontend stack deletion to complete..."
WAIT_COUNT=0
while [ $WAIT_COUNT -lt 60 ]; do
STACK_STATUS=$(aws cloudformation describe-stacks --stack-name "$stack" --region "$REGION" \
--query 'Stacks[0].StackStatus' --output text 2>&1)
[[ "$STACK_STATUS" == *"does not exist"* ]] && break
sleep 10; WAIT_COUNT=$((WAIT_COUNT + 10))
done
;;
esac
done
# Delete Backend second
for stack in $ALL_STACKS; do
case $stack in
*Backend*) delete_cfn_stack "$stack" ;;
esac
done
# Delete AgentCore third
for stack in $ALL_STACKS; do
case $stack in
*AgentCore*) delete_cfn_stack "$stack" ;;
esac
done
# Delete CDKToolkit last (after MSP stacks, before IAM cleanup)
if [ "$KEEP_BOOTSTRAP" = false ]; then
delete_cfn_stack "CDKToolkit"
# Wait for CDKToolkit to delete its resources (S3, ECR, IAM roles)
echo " Waiting for CDKToolkit deletion to complete..."
WAIT_COUNT=0
while [ $WAIT_COUNT -lt 60 ]; do
STACK_STATUS=$(aws cloudformation describe-stacks --stack-name "CDKToolkit" --region "$REGION" \
--query 'Stacks[0].StackStatus' --output text 2>&1)
[[ "$STACK_STATUS" == *"does not exist"* ]] && break
sleep 10; WAIT_COUNT=$((WAIT_COUNT + 10))
done
else
echo " Skipping CDKToolkit (--keep-bootstrap)"
fi
# ─── Step 8: Delete IAM roles (cleanup any orphaned roles) ───────────────────
step "8/9" "Deleting IAM roles..."
for role in $(aws iam list-roles --query 'Roles[?contains(RoleName, `MSPAssistant`)].RoleName' --output text | filter_none); do
delete_iam_role "$role"
done
for role in $(aws iam list-roles --query 'Roles[?contains(RoleName, `msp-gateway`)].RoleName' --output text | filter_none); do
delete_iam_role "$role"
done
# Clean up any remaining CDK roles (should be none if CDKToolkit deleted successfully)
for role in $(aws iam list-roles --query 'Roles[?contains(RoleName, `cdk-hnb659fds`)].RoleName' --output text | filter_none); do
delete_iam_role "$role"
done
# ─── Step 9: Final cleanup ───────────────────────────────────────────────────
step "9/9" "Final cleanup..."
# Clean up CDK assets bucket and ECR repo if they still exist (fallback)
for bucket in $(aws s3 ls | awk '{print $3}' | grep -E "cdk-hnb659fds-assets-${ACCOUNT_ID}-${REGION}"); do
delete_versioned_bucket "$bucket"
done
for repo in $(aws ecr describe-repositories --region "$REGION" \
--query 'repositories[?contains(repositoryName, `cdk-hnb659fds-container-assets`)].repositoryName' --output text 2>/dev/null | filter_none); do
aws ecr delete-repository --repository-name "$repo" --force --region "$REGION" >/dev/null 2>&1 && log "Deleted CDK ECR: $repo"
done
cleanup_local_files
# ─── Verification ────────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}=== Verification ===${NC}"
CLEAN=true
check() {
local label=$1 result=$2
if [ -z "$result" ]; then
echo -e " ${GREEN}✓${NC} $label: None"
else
echo -e " ${RED}✗${NC} $label: $result"
CLEAN=false
fi
}
check "CloudFormation" "$(aws cloudformation list-stacks --region "$REGION" \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE DELETE_FAILED UPDATE_ROLLBACK_COMPLETE \
--query 'StackSummaries[?contains(StackName, `MSP`) || contains(StackName, `CDKToolkit`)].StackName' --output text 2>/dev/null)"
check "S3 Buckets" "$(aws s3 ls 2>/dev/null | grep -iE 'msp|cdk-hnb659fds|bedrock-agentcore' || true)"
check "IAM Roles" "$(aws iam list-roles \
--query 'Roles[?contains(RoleName, `MSP`) || contains(RoleName, `cdk-hnb659fds`) || contains(RoleName, `msp-gateway`)].RoleName' --output text 2>/dev/null)"
check "Log Groups" "$(aws logs describe-log-groups --region "$REGION" \
--query 'logGroups[?contains(logGroupName, `MSP`) || contains(logGroupName, `msp`)].logGroupName' --output text 2>/dev/null)"
check "ECR" "$(aws ecr describe-repositories --region "$REGION" \
--query 'repositories[?contains(repositoryName, `msp`)].repositoryName' --output text 2>/dev/null)"
check "ECS" "$(aws ecs list-clusters --region "$REGION" \
--query 'clusterArns[?contains(@, `MSP`) || contains(@, `Backend`)]' --output text 2>/dev/null)"
check "AgentCore Gateways" "$(aws bedrock-agentcore-control list-gateways --region "$REGION" \
--query 'items[].gatewayId' --output text 2>/dev/null)"
check "AgentCore Memories" "$(aws bedrock-agentcore-control list-memories --region "$REGION" \
--query 'memories[].id' --output text 2>/dev/null)"
check "AgentCore Runtimes" "$(aws bedrock-agentcore-control list-agent-runtimes --region "$REGION" \
--query 'agentRuntimes[].agentRuntimeId' --output text 2>/dev/null)"
check "AgentCore API Key Creds" "$(aws bedrock-agentcore-control list-api-key-credential-providers --region "$REGION" \
--query 'credentialProviders[].name' --output text 2>/dev/null)"
check "Cedar Policy Engines" "$(aws bedrock-agentcore-control list-policy-engines --region "$REGION" \
--query "policyEngines[?name=='msp-policy-engine'].policyEngineId" --output text 2>/dev/null)"
check "Bedrock Guardrails" "$(aws bedrock list-guardrails --region "$REGION" \
--query "guardrails[?name=='msp-ops-topic-guardrail'].id" --output text 2>/dev/null)"
check "AgentCore OAuth2 Creds" "$(aws bedrock-agentcore-control list-oauth2-credential-providers --region "$REGION" \
--query 'credentialProviders[].name' --output text 2>/dev/null)"
check "Secrets Manager" "$(aws secretsmanager list-secrets --region "$REGION" \
--filters Key=name,Values=msp-credentials/ \
--query 'SecretList[].Name' --output text 2>/dev/null)"
check "DynamoDB Tables" "$(aws dynamodb list-tables --region "$REGION" \
--query 'TableNames[?contains(@, `msp-assistant`)]' --output text 2>/dev/null)"
check "CloudWatch Policies" "$(aws logs describe-resource-policies --region "$REGION" \
--query 'resourcePolicies[?contains(policyName, `TransactionSearch`)].policyName' --output text 2>/dev/null)"
check "Local Files" "$(ls -1 frontend/.env infrastructure/cdk/outputs.json /tmp/jira-openapi.json agents/runtime*/env_config.txt 2>/dev/null || true)"
echo ""
if [ "$CLEAN" = true ]; then
echo -e "${GREEN}╔════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Cleanup Complete! ✓ ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════╝${NC}"
echo ""
echo "All MSP Assistant resources removed from $REGION."
else
echo -e "${YELLOW}╔════════════════════════════════════════════╗${NC}"
echo -e "${YELLOW}║ Cleanup mostly complete — check above ║${NC}"
echo -e "${YELLOW}╚════════════════════════════════════════════╝${NC}"
fi