Removing AWS-CDK Pipelines Stacks Across Multiple Accounts

Currently, I am utilizing pipelines in aws-cdk to streamline the process of automating builds and deployments across various accounts. However, I have encountered an issue where upon destroying the pipeline or stacks within it, the respective stacks are not being removed from the target account. Is there a specific setting for removal/destruction within the pipeline that ensures destroyed stacks are also removed? I have noticed that items are successfully removed from the main account where the pipeline is executed, but this isn't the case for other associated accounts.

Answer №1

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'
]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting Typescript fat arrow syntax to regular Javascript syntax

I recently started learning typescript and I'm having trouble understanding the => arrow function. Could someone clarify the meaning of this JavaScript code snippet for me: this.dropDownFilter = values => values.filter(option => option.value ...

Tips for extracting year, month, and day from a date type in Typescript

I'm currently working with Angular 7 and I'm facing some challenges when it comes to extracting the year, month, and day from a Date type variable. Additionally, I am utilizing Bootstrap 4 in my project. Can anyone assist me with this? Below is ...

Steps for Signing Up for a Collaboration Service

I'm struggling to understand how to monitor for new values in an observable from a service. My ultimate objective is to integrate an interceptor, a service, and a directive to show loading information to the user. I have set up an interceptor to liste ...

Is it recommended to include modules in the Imports section of SharedModule in Angular?

Welcome to my SharedModule! import { CommonModule } from "@angular/common"; import { NgModule } from "@angular/core"; import { FormsModule, ReactiveFormsModule } from "@angular/forms"; import { IconModule } from "@my-app/components/icon/icon.module"; impo ...

Issues with sending emails through Nodemailer in a Next.js project using Typescript

I'm currently working on a personal project using Nodemailer along with Next.js and Typescript. This is my first time incorporating Nodemailer into my project, and I've encountered some issues while trying to make it work. I've been followin ...

Extract values from a deeply nested object while retaining the type information

How can I map all object values of the first obj while preserving the generic type for the Wrapped object? I attempted this using a mapped type, but encountered two issues: I'm struggling to represent a nested Object in the MapToWrappedType I can&ap ...

Tips for accessing the type of a nested union in TypeScript

My graphql codegen has produced this type for me: export type GetOffersForMembershipQuery = { __typename?: "Query"; offers: | { __typename?: "BaseError" } | { __typename?: "QueryOffersSuccess"; data ...

RxJS - Only emit if another source does not emit within a specified time frame

Imagine having two observables. Whenever the first one emits, there should be a 2-second pause to check if the other observable emits something within that timeframe. If it does, then no emission should occur. However, if it doesn't emit anything, the ...

Issue with Angular Reactive form: Checkbox checked property not binding correctly when the page initially loads

Looking to achieve Two-way data binding of Checkbox in Angular Reactive forms. After checking the checkbox, I am updating the 'isdateChkd' variable and storing it in the state. Despite the variable being set to TRUE, the checkbox does not get aut ...

I'm diving into the world of Typescript and trying to figure out how to use tooltips for my d3 stacked bar chart. Any guidance on implementing mouseover effects in Typescript would be greatly

I am currently facing some issues with the code below and need guidance on how to proceed. I am new to this and unsure of how to call createtooltip. Any assistance would be greatly appreciated. The error message states that createtooltip is declared but n ...

Leveraging TypeScript to Access Parameters in React Router

Currently, I am delving into the realm of TypeScript usage in my React projects and I have encountered a stumbling block when it comes to implementing React Router's useParams() feature. My import statement looks like this: import { useParams } from ...

The command "ng test" threw an error due to an unexpected token 'const' being encountered

Any assistance with this matter would be greatly appreciated. I am in the process of constructing an Angular 5 project using angular/cli. The majority of the project has been built without any issues regarding the build or serve commands. However, when a ...

The act of exporting an enum from a user-defined TypeScript path leads to the error message "Module not

I have set up a custom path as explained in this particular discussion. "baseUrl": ".", "paths": { "@library/*": [ "./src/myFolder/*" ], } Within this module, I am exporting an Enum. export enum EN ...

Error in Directive: NgControl Provider Not Found

I encountered an issue with my Directive while attempting to inject 'NgControl' and received a 'No provider for NgControl' error. Here is the structure of my File Directory: app folder |--directives folder |--myDirec ...

Steps for incorporating moment.js into an Angular 2 project

Having trouble importing moment.js into my angular2 application despite following various guides and solutions provided. Even though the package is present in my IDE (Visual Studio) and the moment.d.ts file is easily found, I keep encountering errors when ...

What is the reason for Google Chrome extension popup HTML automatically adding background.js and content.js files?

While using webpack 5 to bundle my Google Chrome extension, I encountered an issue with the output popup HTML. It seems to include references to background.js and content.js even though I did not specify these references anywhere in the configuration file. ...

The type definition file for '@wdio/globals/types' is nowhere to be found

I'm currently utilizing the webdriverio mocha framework with typescript. @wdio/cli": "^7.25.0" NodeJs v16.13.2 NPM V8.1.2 Encountering the following error in tsconfig.json JSON schema for the TypeScript compiler's configuration fi ...

Can the default JSON input for "Start Execution" be configured using AWS CDK for Step Functions in AWS?

We are currently in the process of streamlining the Step Function generation for our Cloud Ops team to call upon. I am wondering if there is a way to define the default input parameters for the Execution using the SDK. The current default setting is as sho ...

Optimizing performance in React: A guide to utilizing the Context and useCallback API in child

Utilizing the Context API, I am fetching categories from an API to be used across multiple components. Given this requirement, it makes sense to leverage context for managing this data. In one of the child components, the categories can be expanded using ...

What steps can be taken to ensure that all object properties become reactive?

Let's dive into this simplified scenario: interface Pup { name: string; age: number; } const puppy: Pup = { name: 'Rex', age: 3, }; The goal here is to establish a reactive link for each attribute within the puppy object. The usua ...