Pass the value of “is_exist” variable from ASG by fetching the active stack name and run the script.
Review the logic before use, may required modification as per your need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/bin/bash REGION="us-east-2" a=$(aws cloudformation describe-stacks --region $REGION --query 'Stacks[*].{StackName:StackName}' --output text) declare -a arr=("ore-nikt") arr_stack=() is_exist="ore-nikt-10123asd44" for stack in $a;do for name in $arr;do if [[ "$stack" == $name* ]]; then arr_stack+=($stack) for isexist in ${arr_stack[*]}; do if [ "$is_exist" != $isexist ];then echo "Deleteing the Stacks" aws cloudformation delete-stack --region $REGION --stack-name $isexist fi done fi done done |