简体   繁体   English

“cdk destroy”没有按预期工作,或者我没有正确理解它?

[英]'cdk destroy' is not working as intended or I am not understanding it correctly?

Here is my demo stack,这是我的演示堆栈,

export class HelloCdkStack extends cdk.Stack {
  constructor(parent: cdk.App, id: string, props?: cdk.StackProps) {
    super(parent, id, props);
    new s3.Bucket(this, 'MyFirstBucket', {
      versioned: true,
      encryption: s3.BucketEncryption.KmsManaged,
    });
  }
}

'cdk deploy' creates a new bucket, but when I execute 'cdk destroy' it does not delete the bucket. “cdk deploy”创建了一个新的存储桶,但是当我执行“cdk destroy”时,它并没有删除这个存储桶。 Am I doing anything wrong?我做错了什么吗?

By default, S3 buckets are configured to be 'orphaned' when a stack is deleted.默认情况下,S3 存储桶配置为在删除堆栈时“孤立”。 Setting removalPolicy to Destroy will physically destroy the bucket on deletion.removalPolicy设置为Destroy将在删除时物理销毁存储桶。

If you need to automatically destroy a bucket with files in it, check out this CDK construct: https://www.npmjs.com/package/@mobileposse/auto-delete-bucket如果您需要自动销毁包含文件的存储桶,请查看此 CDK 构造: https : //www.npmjs.com/package/@mobileposse/auto-delete-bucket

If you need to automatically destroy a bucket that is expected to be empty, use the standard bucket and set removalPolicy to DESTROY.如果您需要自动销毁预计为空的存储桶,请使用标准存储桶并将 removePolicy 设置为 DESTROY。 https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#removalpolicy https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#removalpolicy

In my case the problem was, cdk was trying to fetch different accounts Credentials.在我的例子中,问题是cdk试图获取不同的帐户凭据。 Add the --verbose or -v flag to see if any exception is thrown internally.添加--verbose-v标志以查看内部是否抛出任何异常。

It's a shame that the exception was not getting logged to stdout or stderr (as it should for any tool)遗憾的是异常没有被记录到 stdout 或 stderr(任何工具都应该如此)

You can set destroy to removalPolicy , it will remove the bucket if it's empty: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#removalpolicy您可以将destroy设置为removalPolicy ,如果它是空的,它将删除存储桶: https : removalPolicy

If you want to destroy even non-empty bucket, you should also set autoDeleteObjects property to true : https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#autodeleteobjects如果您想销毁甚至非空存储桶,您还应该将autoDeleteObjects属性设置为truehttps : //docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html #自动删除对象

In python, following the getting started , you can add removal_policy=cdk.RemovalPolicy.DESTROY parameter when instantiate the s3.Bucket object, so the bucket will be delete on cdk destroy .在python中,按照入门步骤,可以在实例化s3.Bucket对象时添加removal_policy=cdk.RemovalPolicy.DESTROY参数,这样bucket会在cdk destroy被删除。

from aws_cdk import core as cdk
from aws_cdk import aws_s3 as s3


class HelloCdkStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        bucket = s3.Bucket(self,
                           "MyFirstBucket",
                           versioned=True,
                           removal_policy=cdk.RemovalPolicy.DESTROY)  # delete bucket on destroy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM