aws-samples / aws-sdk-js-notes-app

A simple note taking application using modular AWS SDK for JavaScript (v3)
Other
81 stars 30 forks source link

Delete fails for filesbucket if it is not empty when running yarn cdk destroy #21

Closed trivikr closed 3 years ago

trivikr commented 3 years ago

Describe the bug

The yarn cdk detroy command fails with the following error if the files bucket is not empty:

Are you sure you want to delete: aws-sdk-js-notes-app (y/n)? y
aws-sdk-js-notes-app: destroying...
10:57:33 PM | DELETE_FAILED        | AWS::S3::Bucket                          | files-bucket
The bucket you tried to delete is not empty (Service: Amazon S3; Status Code: 409; Error Code: BucketNotEmpty; Request ID: 7S3N5TAMAR1ZEY4P; S3 Extended Request ID: gWaSi+kDT/3zDqUMM3OzacQxZ3tCGKEKnaejBmT6koo54bREZAipsDaK6pq4nks2ori9PyPBduk=; Proxy: nul
l)

Steps to reproduce

Observed behavior

The command fails with the following error:

 ❌  aws-sdk-js-notes-app: destroy failed Error: The stack named aws-sdk-js-notes-app is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [filesbucket2B30D8E0]. )
    at Object.waitForStackDelete (/local/home/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-cdk/lib/api/util/cloudformation.ts:277:11)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Object.destroyStack (/local/home/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-cdk/lib/api/deploy-stack.ts:379:28)
    at CdkToolkit.destroy (/local/home/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-cdk/lib/cdk-toolkit.ts:252:9)
    at initCommandLine (/local/home/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-cdk/bin/cdk.ts:204:9)
The stack named aws-sdk-js-notes-app is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [filesbucket2B30D8E0]. )

Expected behavior

The command successfully deletes the AWS infrastructure.

trivikr commented 3 years ago

The workaround is to manually empty the files-bucket using CLI or AWS console, and run the yarn cdk destroy command.

Verified that deletion is successful:

Are you sure you want to delete: aws-sdk-js-notes-app (y/n)? y
aws-sdk-js-notes-app: destroying...
11:05:31 PM | DELETE_IN_PROGRESS   | AWS::CloudFormation::Stack               | aws-sdk-js-notes-app

 ✅  aws-sdk-js-notes-app: destroyed
trivikr commented 3 years ago

The existing removalPolicy for files-bucket is cdk.RemovalPolicy.DESTROY

https://github.com/aws-samples/aws-sdk-js-notes-app/blob/7cbe82e52c8d9451b17bbf2338f4438b1839f710/packages/infra/cdk/aws-sdk-js-notes-app-stack.ts#L72-L74

It should be changed to SNAPSHOT to delete the resource.

trivikr commented 3 years ago

This issue can't be fixed as AWS::S3::Bucket does not support deletion policy Snapshot

Following error was thrown when SNAPSHOT removalPolicy was attempted.

❌  aws-sdk-js-notes-app failed: Error [ValidationError]: Template error: resource type AWS::S3::Bucket does not support deletion policy Snapshot
    at Request.extractError (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/protocol/query.js:50:29)
    at Request.callListeners (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/request.js:690:12)
    at Request.callListeners (/Users/trivikr/workspace/aws-sdk-js-notes-app/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  code: 'ValidationError',
  time: 2021-08-05T21:03:14.278Z,
  requestId: 'a5c25cdc-1923-4b49-bb08-b13d83ae09fb',
  statusCode: 400,
  retryable: false,
  retryDelay: 692.6105252129084
}
Template error: resource type AWS::S3::Bucket does not support deletion policy Snapshot