这是我的演示堆栈,
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'创建一个新的存储桶,但是当我执行'cdk destroy'时,它不会删除该存储桶。我做错什么了吗?
答案 0 :(得分:1)
默认情况下,删除堆栈时,S3存储桶配置为“孤立”的。将removalPolicy
设置为Destroy
会在删除时物理破坏存储桶。
答案 1 :(得分:0)
如果您需要自动销毁带有文件的存储桶,请查看以下CDK构造:https://www.npmjs.com/package/@mobileposse/auto-delete-bucket
如果您需要自动销毁一个预期为空的存储桶,请使用标准存储桶,并将removePolicy设置为DESTROY。 https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#removalpolicy
答案 2 :(得分:0)
您可以将 destroy
设置为 removalPolicy
,如果它是空的,它将删除存储桶:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#removalpolicy
如果你想销毁甚至非空的bucket,你也应该将autoDeleteObjects
属性设置为true
:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#autodeleteobjects
答案 3 :(得分:0)
在python中,在getting started之后,可以在实例化removal_policy=cdk.RemovalPolicy.DESTROY
对象时添加s3.Bucket
参数,这样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