How do I go about deleting/tearing down a stack across multiple environments?
Let’s say I have a stack called ec2 and I’m on my AWS development environment in a Git feature branch. I can run down ec2 from my local machine and it will tear down the stack. I can then delete the stack folder and merge my feature branch into main.
But when main is applied on the AWS staging or production environments it will not know about the missing stack folder.
So how do I do that when I’m in a CI/CD pipeline? The stack folder will not be on the main feature branch, and Terraspace can’t tear down a stack when its code is gone.
My assumption is, that every time I rename or delete a stack I need to run some special pipeline, that does the tearing down, on all environments first, and only then I can merge my feature branch into main.
Alternatively I’d have to script my way through this in the pipeline by checking for the existence of a stack, and if it’s not there I’d run down <STACK> on it or something like that.
RE: But when main is applied on the AWS staging or production environments it will not know about the missing stack folder.
Bummer.
RE: Alternatively I’d have to script my way through this in the pipeline by checking for the existence of a stack, and if it’s not there I’d run down on it or something like that.
Think that may be the only option. As a part of this script, you could create a “placeholder” stack just so it can do a delete. Thinking it would be nice to add this placeholder logic actual into terraspace for the specific case for terraspace down. IE: It automatically, creates a placeholder stack.
Unsure when will get to it. Working on a pretty significant release for Terraspace. Unsure when right now. It’s maybe about a month out. Might be able to squeeze this in. Also, will take a look at PRs. Of course, no sweat either way.
Unfortunately my Ruby-skills are non-existent, as in: never used it before Terraspace. Otherwise I would have looked into creating a PR myself
This is what I have built now. It needs to be called during each CI/CD run. The git command will recover all deleted folders under app/stacks/, run terraspace down on them and then delete them again. The git command only checks for them in the last commit, so PRs need to be squashed and consist of one commit only to make this work:
#!/bin/bash
# Description:
# Tears down deleted stacks by searching for them in the last commit.
#
# Usage:
# ./teardown_deleted_stacks.sh
# Function to tear down a stack
tear_down() {
local stack_name=$1
if [ -z "$stack_name" ]; then
echo "Please provide the stack name to tear down."
return 1
fi
# Temporarily recover the deleted stack folder
git checkout HEAD^ -- app/stacks/$stack_name
# Tear down the stack
echo "Tearing down the stack: $stack_name"
terraspace down $stack_name
# Remove the stack folder again
rm -rf app/stacks/$stack_name
}
# Get deleted folders under app/stacks/
deleted_stacks=$(git diff-tree -r --name-only --diff-filter=D HEAD | grep '^terraspace/app/stacks/' | awk -F/ '{print $4}' | uniq)
# Tear down each deleted stack
for stack_name in $deleted_stacks; do
tear_down $stack_name
done
P.S.: Changing the profile picture, or loading the one from Gravatar, doesn’t work here in the forum. I tried on Firefox and Chrome and a .JPG.