|
1 | | -# cdk-self-destruct |
2 | | - |
| 1 | +<p align="center"> |
| 2 | + <img src="logo.png" alt="vacuum" height="300px"/> |
| 3 | +</p> |
| 4 | + |
| 5 | +[](https://badge.fury.io/js/cdk-self-destruct) |
| 6 | + |
| 7 | + |
| 8 | +A solid tool for destroying temporary CDK environments, which are commonly used in automated testing. With its scheduling feature for stack destruction, you can easily set a time and date or generate a url for the automatic removal of unnecessary stacks, freeing up resources and optimizing the testing workflow. It also removes resources that are impeding stack deletion, such as non-empty S3 buckets. |
| 9 | + |
| 10 | +Inspired by [cdk-time-bomb](https://github.com/jmb12686/cdk-time-bomb), rewritten with aws-cdk v2 and the new [AWS EventBridge Scheduler](https://aws.amazon.com/de/blogs/compute/introducing-amazon-eventbridge-scheduler). |
| 11 | + |
| 12 | +## Installing |
| 13 | + |
| 14 | +**requires aws-cdk: "^2.51.0"** |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install cdk-self-destruct |
| 18 | + |
| 19 | +# or |
| 20 | + |
| 21 | +yarn add cdk-self-destruct |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Include it at the end of your stack. Behind the scenes it uses [CDK Aspects](https://docs.aws.amazon.com/cdk/v2/guide/aspects.html) to capture all resources automatically. |
| 27 | + |
| 28 | +```ts |
| 29 | +import { type StackProps, Stack, Duration } from 'aws-cdk-lib' |
| 30 | +import { type Construct } from 'constructs' |
| 31 | +import { SelfDestruct } from 'cdk-self-destruct' |
| 32 | + |
| 33 | +export class AwesomeStack extends Stack { |
| 34 | + public constructor( |
| 35 | + scope: Construct, |
| 36 | + id: string, |
| 37 | + props: StackProps |
| 38 | + ) { |
| 39 | + super(scope, id, props) |
| 40 | + |
| 41 | + new SelfDestruct(this, 'SelfDestruct', { |
| 42 | + defaultBehavior: { |
| 43 | + destoryAllResources: true, |
| 44 | + purgeResourceDependencies: true, |
| 45 | + }, |
| 46 | + trigger: { |
| 47 | + scheduled: { |
| 48 | + afterDuration: Duration.days(1), |
| 49 | + enabled: true, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Features |
| 58 | + |
| 59 | +1. Set `RemovalPolicy` for all resources inside a stack |
| 60 | +2. Destroy resource dependencies that are blocking the stack deletion |
| 61 | + - Purge S3 buckets before deletion |
| 62 | + - Stop all running state-machine executions |
| 63 | + - *more coming soon* |
| 64 | +3. Schedule stack deletions after a given duration or at a given timestamp |
| 65 | +4. Create a [Lambda function URL](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) to delete the stack easily from the pipeline |
| 66 | + |
| 67 | +## Options |
| 68 | + |
| 69 | +### Select per individual resource |
| 70 | + |
| 71 | +A list of all available options can be found [here](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) |
| 72 | + |
| 73 | +```ts |
| 74 | +new SelfDestruct(this, 'SelfDestruct', { |
| 75 | + // ... |
| 76 | + byResource: { |
| 77 | + resourcesToDestroy: ['AWS::S3::Bucket'], |
| 78 | + resourcesToRetain: ['AWS::DynamoDB::Table'], |
| 79 | + }, |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +### Schedule for a given Date |
| 84 | + |
| 85 | +Stack deletion may be scheduled for a given UTC timestamp. |
| 86 | + |
| 87 | +```ts |
| 88 | +new SelfDestruct(this, 'SelfDestruct', { |
| 89 | + // ... |
| 90 | + trigger: { |
| 91 | + scheduled: { |
| 92 | + atTimestamp: new Date('2023-01-01T00:00:00Z').getTime(), |
| 93 | + enabled: true, |
| 94 | + }, |
| 95 | + }, |
| 96 | +}) |
| 97 | +``` |
| 98 | + |
| 99 | +### Invoke via a lambda function url |
| 100 | + |
| 101 | +Function urls allow to start the stack deletion manually via an http request. |
| 102 | +Authentication is available via IAM or via unauthenticated requests. |
| 103 | + |
| 104 | +```ts |
| 105 | +new SelfDestruct(this, 'SelfDestruct', { |
| 106 | + // ... |
| 107 | + trigger: { |
| 108 | + addFunctionUrl: { |
| 109 | + cloudformationOutput: { |
| 110 | + description: 'URL to invoke the self-destruct function', |
| 111 | + exportName: 'SelfDestructUrl', |
| 112 | + }, |
| 113 | + enabled: true, |
| 114 | + options: { |
| 115 | + // Allow unauthenticated requests |
| 116 | + authType: cdk.aws_lambda.FunctionUrlAuthType.NONE, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | +}) |
| 121 | +``` |
0 commit comments