aws-cdk remove resource from stack that used in another stack

2 min read 05-10-2024
aws-cdk remove resource from stack that used in another stack


Removing Resources from AWS CDK Stacks: A Guide for Developers

Problem: You have an AWS CDK stack that depends on resources created in a separate stack. You want to remove resources from the dependent stack, but you need to ensure that the removal doesn't break the functionality of your main stack.

Rephrased: Imagine you have two CDK stacks: "Stack A" creates a database, and "Stack B" uses that database. If you want to delete the database (in "Stack A"), you need to make sure that "Stack B" doesn't try to access it anymore. Otherwise, your application will fail!

Scenario and Code:

Let's say you have a "DatabaseStack" creating a database, and an "AppStack" that uses this database.

// DatabaseStack.ts
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';

export class DatabaseStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'Vpc');

    new rds.DatabaseInstance(this, 'Database', {
      engine: rds.DatabaseInstanceEngine.postgres({ version: rds.PostgresEngineVersion.LATEST }),
      vpc,
    });
  }
}

// AppStack.ts
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { DatabaseStack } from './database-stack';

export class AppStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const databaseStack = new DatabaseStack(this, 'Database');
    
    // Access database resources using databaseStack.
  }
}

Analysis and Solutions:

There are two main approaches to removing resources from a dependent stack:

  1. Destroy the dependent stack first: This is the simplest approach, but it might not be suitable for all scenarios.

    • Example: If you use the cdk destroy command with --all flag, it will destroy all stacks, including the dependent one. This will remove both the database and the application that uses it.
  2. Remove the dependency: This approach involves updating the dependent stack to remove the dependency on the resource you want to remove.

    • Example: If you want to remove the database in DatabaseStack, you would need to update AppStack to use a different database or remove the code accessing the database completely.

Important Considerations:

  • Dependency Management: Understanding the dependencies between stacks is crucial. Using CDK constructs like Stack and CfnOutput can help you manage dependencies and ensure consistent removal.
  • State Management: AWS CDK uses CloudFormation to manage the deployment and removal of resources. The state of your stacks is stored in CloudFormation stacks.
  • Deployment Order: Be mindful of the deployment order. When destroying stacks, you might need to destroy the dependent stack first.

Additional Tips:

  • CDK dependsOn: Use the dependsOn property in CDK to control the order of resource creation and deletion.
  • CDK Stack Inheritance: You can inherit from the Stack class in CDK to create reusable stack structures.
  • Test Thoroughly: Always test your code changes before deploying to production.

Resources and Further Reading:

Conclusion:

Removing resources from AWS CDK stacks that are used by other stacks requires careful planning and execution. By understanding dependencies, utilizing CDK constructs, and testing thoroughly, you can ensure a seamless and successful resource removal process.