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?
I want to resolve the "Already Exists" error I receive when I'm re-deploying my AWS Cloud Development Kit (AWS CDK) code.
"I want to resolve the "Already Exists" error I receive when I'm re-deploying my AWS Cloud Development Kit (AWS CDK) code.Short descriptionMost 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.ResolutionNote: 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 resource1.    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 deployChange the name of the retained resource1.    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 name1.    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 deploySet the removalPolicy to DESTROY1.    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 synthFollow"