How do I resolve the "Already Exists" error that I receive when I redeploy my AWS CDK code after I delete the stack from the deployment?

3 minute read
0

I want to resolve the "Already Exists" error that I receive when I redeploy my AWS Cloud Development Kit (AWS CDK) code.

Resolution

Most stateful resources in the AWS CDK Construct Library accept the removalPolicy property with RETAIN as the default. For more information, see enum RemovalPolicy on the AWS CDK website.

If you don't set the removalPolicy property on your resources, then the resources become orphaned. They remain in your AWS account after you delete the stack, and transition to the DELETE_COMPLETE state. If you specified a custom name for the resources, then the "Already Exists" error appears when you redeploy the same code.

Note: The following steps use an example Amazon Simple Storage Service (Amazon S3) bucket resource that's associated with the s3.Bucket class in AWS CDK. By default, the resource's removalPolicy is set to RETAIN. When you delete the associated stack, the resource is retained in your account. When you update the stack, the resource is removed.

The following is an example of a custom-named resource:

const s3Bucket = new s3.Bucket(this, 's3-bucket', {
 bucketName: 'DOC-EXAMPLE-BUCKET1',
 versioned: false,
 encryption: s3.BucketEncryption.S3_MANAGED
 });

Manually delete the retained resource

Complete the following steps:

  1. Sign in to the AWS Management Console.
  2. Choose the service of the resources that you don't want to retain.
  3. Manually delete the resources. For example, to remove the s3.bucket resource, delete the Amazon S3 bucket.
  4. To redeploy the AWS CDK, run the following command:
    cdk deploy

Change the name of the retained resource

Complete the following steps:

  1. Open the AWS CDK code of the resource that you want to change the name of.
  2. Update the name of the resource to a unique value that doesn't conflict with the name of the retained resource:
    const s3Bucket = new s3.Bucket(this, 's3-bucket', {
     bucketName: 'EXAMPLE-NEW-NAME-S3-BUCKET',
     versioned: false,
     encryption: s3.BucketEncryption.S3_MANAGED
     });
    Note: The preceding example uses the bucketName parameter to change the name of the s3.Bucket resource.

Delete the resource name

Delete the resource name so that AWS CDK can generate a unique name.

Complete the following steps:

  1. Remove the resource name from AWS CDK:
    const s3Bucket = new s3.Bucket(this, 's3-bucket', {
     versioned: false,
     encryption: s3.BucketEncryption.S3_MANAGED
     });
    Note: The preceding example code removes the bucketName property to allow AWS CDK to generate a new name.
  2. To redeploy AWS CDK, run the following command:
    cdk deploy

Set the removalPolicy to DESTROY

Before you delete a stack, set the resource's removalPolicy to DESTROY.

Complete the following steps:

  1. Open the AWS CDK code of the resources that you don't want to retain.

  2. Set the removalPolicy property to DESTROY:

    const s3Bucket = new s3.Bucket(this, 's3-bucket', {
     bucketName: 'EXAMPLE-S3-BUCKET',
     removalPolicy: RemovalPolicy.DESTROY
     });
  3. To access the AWS CloudFormation template, run the following cdk synth command:

    cdk synth

    Then, verify that the DeletionPolicy and UpdateReplacePolicy is set to Delete.

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago
8 Comments

This article does not explain the usual action following retaining, which is the intent to re-use the retained resource in the next stack creation. Regardless of using a custom or auto-generated name, how does one import them? The ".from" static functions on the CDK resources for imports are only pointers to the account resources and cannot be resumed with ongoing modifications to the stack resources.

Will
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a year ago

Unfortunately, this post is very disappointing. Is there a way AWS CDK can be smart to automatically import these retained stateful resources back into the stack? That was the resolution I was hoping to see. Correct me if I'm wrong, but if we'd like to keep the date stored in these resources, we'd have to craft a workaround? Perhaps, rename retained resource and write some script / lambda function to import items?

replied 10 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 10 months ago

What if I want to reuse the resource? That's the main purpose of retaining it in my case also.

Hasan
replied 8 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 8 months ago

CDK should automatically import resources that already exist, otherwise it's largely useless for IaC for anything that isn't stateless.

dossy
replied 7 months ago

Echoing the replies you have already received on this article. The usual desired behavior is to re-import these already existing resources. You should either explain how to do that manually or implement the behavior for cdk to do this.

Fawaz
replied 20 days ago