Your Pipeline does not have a built-in feature to automatically delete its deployed stacks, whether in CDK or with the CodePipeline APIs.
However, you can create a script using SDKs to handle pipeline stack deletion. Utilize the provided script below to retrieve a list of a Pipeline's deployed stack ARNs from the Pipeline's state.
The CloudFormation SDK offers a delete_stack command for deleting the stacks effectively.
import re
import boto3
from mypy_boto3_codepipeline.type_defs import ActionStateTypeDef
session = boto3.Session(profile_name='pipeline', region_name='us-east-1')
client = session.client('codepipeline')
# Obtain a list of the pipeline stages and actions
res = client.get_pipeline_state(name="QueenbPipeline")
stack_arns = []
# Function to extract the ARN from an action type
def extract_arn(a: ActionStateTypeDef) -> str:
url = a['latestExecution']['externalExecutionUrl']
return re.match(r'^.*stackId=(.*)(?:/[a-f0-9-]{36})$', url).group(1)
# Extract the ARNs
deploy_states = [s for s in res['stageStates'] if ".Deploy" in s['stageName']]
for state in deploy_states:
actions = [extract_arn(a) for a in state['actionStates'] if ".Deploy" in a['actionName']]
stack_arns.extend(actions)
print(stack_arns)
Output - List of the Pipeline's deployed stacks
[
'arn:aws:cloudformation:us-west-1:123456789012:stack/ReplicationStack',
'arn:aws:cloudformation:us-central-1:123456789012:stack/CoreStack',
'arn:aws:cloudformation:us-central-1:123456789012:stack/ApiStack'
]