Fix error when deleting stacks using capacityProvider

Deleting stacks using ECS clusters having capacityProviders (i.e.
dual-primary and primary-replica recipes), fails with:

```
The Cluster cannot be deleted while Container Instances are active
or draining.
```

This is an issue that manifests itself as well via terraform [1] or CDK
[2].

Explicitly deleting the Autoscaling Groups _before_ the ECS cluster
deletion fixes the problem, since it ensures that no instances are
active or draining, as the error suggests.

This is safe to do, because prior to deleting the Autoscaling Groups,
every ECS service has already been destroyed, thus no instance is
actually running.

[1] https://github.com/hashicorp/terraform-provider-aws/issues/4852
[2] https://github.com/aws/aws-cdk/issues/14732
Bug: Issue 14698
Change-Id: I216307ef88bd7b7317706d2dc0a6a6e6fb367bd4

Change-Id: I27ece0f6971b157a474d91d7f3d9243dcff596e6
diff --git a/Makefile.common b/Makefile.common
index 3c4ea94..00b43e8 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -215,4 +215,28 @@
 		echo "*** Git persistent stack '$(EFSREPLICA_EFS_STACK_NAME_STACK_NAME)' deleted" \
 		, \
 		echo "No Git persistent stack for replicas found. Nothing to do." \
+	)
+
+find-asg:
+	$(eval ASGS := $(shell $(AWS) autoscaling describe-auto-scaling-groups \
+		--query "AutoScalingGroups[? Tags[? (Key=='aws:cloudformation:stack-name') && Value=='$(CLUSTER_STACK_NAME)']]".AutoScalingGroupName  \
+		| jq -r '.[]'))
+
+	@echo "FOUND AUTOSCALING GROUPS in '$(CLUSTER_STACK_NAME)': '$(ASGS)'"
+
+delete-asg: find-asg
+	for asg in $(ASGS); \
+	do \
+		echo "Deleting $$asg Autoscaling group"; \
+		$(AWS) autoscaling delete-auto-scaling-group --force-delete --auto-scaling-group-name "$$asg"; \
+	done
+
+wait_for_asg_deletion: find-asg
+	$(if $(ASGS), \
+		while [[ $$($(AWS) autoscaling describe-auto-scaling-groups --auto-scaling-group-names $(ASGS) | jq '.AutoScalingGroups[]' | grep '[A-Z]') ]]; do \
+			echo "Wait for ASGs $(ASGS) to be deleted"; \
+			sleep 5; \
+		done; \
+		echo "Autoscaling groups $(ASGS) have been terminated.", \
+		echo "No Autoscaling groups found. Nothing to do." \
 	)
\ No newline at end of file
diff --git a/dual-primary/Makefile b/dual-primary/Makefile
index 5d71b93..c79a3c9 100644
--- a/dual-primary/Makefile
+++ b/dual-primary/Makefile
@@ -463,6 +463,7 @@
 						$(optional_dashboard_targets_deletion) \
 						$(optional_replication_targets_deletion) \
 						$(optional_git_gc_targets_deletion) \
+						delete-asg wait_for_asg_deletion \
 						delete-cluster wait-for-cluster-deletion
 
 delete-all-including-retained-stack: confirm-persistent-stack-deletion delete-all delete-git-primary-persistent-stack delete-git-replica-persistent-stack delete-network-persistent-stack delete-dynamodb-persistent-stack
diff --git a/primary-replica/Makefile b/primary-replica/Makefile
index 1bd3d25..0bc0dba 100644
--- a/primary-replica/Makefile
+++ b/primary-replica/Makefile
@@ -283,6 +283,7 @@
 delete-all: delete-dns-routing wait-for-dns-routing-deletion \
 						delete-service-replica wait-for-service-replica-deletion \
 						delete-service-primary wait-for-service-primary-deletion \
+						delete-asg wait_for_asg_deletion \
 						delete-cluster wait-for-cluster-deletion \
 						$(optional_git_gc_targets_deletion) \
 						delete-dashboard wait-for-dashboard-deletion