How do I resolve the "Already Exists" error that I receive when I re-deploy my CDK code after the stack from the initial deployment is deleted?

3 minute read
0

I want to resolve the "Already Exists" error I receive when I'm re-deploying my AWS Cloud Development Kit (AWS CDK) code.

Short description

Most stateful resources in the AWS CDK Construct Library accept the removalPolicy property with RETAIN as the default. Resources that don't have the removalPolicy set become orphan resources, and remain in the account after the stack is deleted. This occurs when the stack transitions to the DELETE_COMPLETE state. The behavior remains the same when the resource definition of similar resources is removed from the code during an update on the corresponding stack. If the retained resources are custom-named, then the "Already Exists" error appears when you re-deploy the same code.

To resolve this error, complete the following actions depending on your use case:

  • For unintentionally retained resources, manually delete the resources.
  • For intentionally retained resources, change the name of the resource in the AWS CDK code to a unique value.
  • Another method for intentionally retained resources is to delete the resource name from the AWS CDK code to let AWS CDK auto-generate a new name.
  • Before deleting a stack, confirm that the removalPolicy is set to DESTROY from the resource.

Resolution

Note: The following steps use an example Amazon Simple Storage Service (Amazon S3) bucket resource represented by the s3.Bucket class in AWS CDK. The removalPolicy of this resource in AWS CDK is set to RETAIN by default. This resource is retained in the account when its respective stack is deleted, or when the resource is removed during a stack update.

Example:

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

Manually delete the retained resource

1.    Sign in to the AWS Management Console and access the corresponding service of the resources you don't want to retain.

2.    Manually delete the resources that you don't want to retain.

Note: For this example, delete the Amazon S3 bucket to delete the s3.bucket resource.

3.    Re-deploy the AWS CDK code:

cdk deploy

Change the name of the retained resource

1.    Access 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.

Note: For this example, update the bucketName parameter to change the name of the s3.Bucket resource.

Example:

const s3Bucket = new s3.Bucket(this, 's3-bucket', {
 bucketName: ‘EXAMPLE-NEW-NAME-S3-BUCKET’,
 versioned: false,
 encryption: s3.BucketEncryption.S3_MANAGED
 });

Delete the resource name to allow AWS CDK to auto-generate a unique name

1.    Remove the resource name from AWS CDK.

Note: For this example, the bucketName property is removed to let AWS CDK auto-generate a new name.

Example:

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

2.    Re-deploy AWS CDK code:

cdk deploy

Set the removalPolicy to DESTROY

1.    Access 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.    Run cdk synth to access the AWS CloudFormation template, and then check that the DeletionPolicy and UpdateReplacePolicy is set to Delete:

cdk synth
AWS OFFICIAL
AWS OFFICIALUpdated a year ago
7 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 9 months ago

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

profile pictureAWS
MODERATOR
replied 9 months 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 4 months ago

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

profile pictureAWS
MODERATOR
replied 4 months ago

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

Hasan
replied 2 months ago

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

profile pictureAWS
MODERATOR
replied 2 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 a month ago