Go Report Card License Latest GitHub Release

CFN Teardown

Cleanup CloudFormation stacks respecting the order of dependencies.

Features

  • Stack name pattern matching for deletion. Finds out dependent/importer/child stacks recursively from a root stack.

  • Builds dependency tree for faster teardown. Dependency tree also gives insight on loose/tight coupling of the stacks.

  • Multiple safety checks to prevent accidental deletion.

  • Supports slack notification for deletion status updates via webhook.


Install

Run downloader script:

curl -f https://raw.githubusercontent.com/nirdosh17/cfn-teardown/master/download.sh | sh

OR

Download binary manually from HERE.


Usage

Required global flags for all commands: STACK_PATTERN, AWS_REGION, AWS_PROFILE

  1. Run cfn-teardown -h and see available commands and needed parameters.

  2. Listing stack dependencies: cfn-teardown listDependencies

    Generates dependencies in stack_teardown_details.json file (printed in terminal as well)

  3. Tear down stacks: cfn-teardown deleteStacks

    Deletes matching stacks and updates status in the teardown details file as the script is running.


Selecting Stacks For Deletion

For stacks with consistent naming convention:

Let’s say you have stacks starting with the environment name followed by a hyphen:

  • qa-shared-networks
  • qa-service-user-management
  • qa-service-user-search

In this can, you need to set stack pattern as ^qa- to match stacks starting with qa-.

For stacks which do not follow any naming pattern:

Example:

  • qa-shared-networks
  • service-user-management (depends on shared networks stack)
  • user-search-service (depends on shared networks stack)

Use the root stack’s name as the stack pattern i.e. ^qa-base-infra. The script will find out all dependendent stacks from the root stack recursively until the leaf nodes have zero importer stacks.


Configuration

Configuration for this command can be set in three different ways in the precedence order defined below:

  1. Environment variables(same as flag name)

  2. Flags e.g. cfn-teardown deleteStacks --STACK_PATTERN=qaenv-

  3. Supplied YAML Config file (default: ~/.cfn-teardown.yaml)

    Minimal config file

    AWS_REGION: us-east-1
    AWS_PROFILE: staging
    STACK_PATTERN: qa-
    All configs present

    <div class="highlight highlight-source-yaml position-relative" data-snippet-clipboard-copy-content="AWS_REGION: us-east-1
    AWS_PROFILE: staging
    TARGET_ACCOUNT_ID: 121212121212
    STACK_PATTERN: qa-
    ABORT_WAIT_TIME_MINUTES: 20
    STACK_WAIT_TIME_SECONDS: 30
    MAX_DELETE_RETRY_COUNT: 5
    SLACK_WEBHOOK_URL: https://hooks.slack.com/services/dummy/dummy/long_hash
    ROLE_ARN: ""
    DRY_RUN: "false"
    “>

    AWS_REGION: us-east-1
    AWS_PROFILE: staging
    TARGET_ACCOUNT_ID: 121212121212
    STACK_PATTERN: qa-
    ABORT_WAIT_TIME_MINUTES: 20
    STACK_WAIT_TIME_SECONDS: 30
    MAX_DELETE_RETRY_COUNT: 5
    SLACK_WEBHOOK_URL: https://hooks.slack.com/services/dummy/dummy/long_hash
    ROLE_ARN: "<arn>"
    DRY_RUN: "false"