I have an array of AWS Cloudformation stacks names in stackList
. Now this contains all the stacks in my AWS account, I want to delete them, but in a particular order.
Lets say the stackList
contains the following string array:
aaa-bbb-ccc
aaa-ccc-ddd
aaa-ggg-ddd
bbb-ccc-ddd
mmm-nnn-yyy
I have some patterns like the cloudformation stack starts with mmm*
should get deleted first, the stacks ending with *ddd
should be second etc.
What is the easiest and most efficient way in doing this?
EDIT
To better explain the question, I am adding the some of the code below:
stackList="$(aws cloudformation describe-stacks | jq -r '.Stacks[].StackName')"
for stack_name in ${stackList}; do
stack_id="$(aws cloudformation describe-stacks --stack-name ${stack_name} | jq -r '.Stacks[].StackId')"
aws cloudformation delete-stack --stack-name "${stack_id}"
echo "Deleting cloudformation stack ${stack_name}"
aws cloudformation wait stack-delete-complete --stack-name "${stack_id}"
done
Now I want cloudformation stacks related to infrastructure users and credentials to be deleted last, For example, below is the order in which I want it to get deleted but the describestacks
can also contain other entries, as well as the order, could be different in other landscapes.
aws-persistence-infrastructure
aws-persistence-users
aws-firewall-infrastructure
aws-network-infrastructure
aws-infrastructure-users
bootstrap-credentials-aws
I can use grep
as answered below but if there is any more efficient method, it would be nice.