我可以强制CloudFormation删除非空的S3 Bucket吗?

时间:2016-11-02 15:13:57

标签: amazon-web-services amazon-s3 aws-cli

有没有办法强制CloudFormation删除非空的S3 Bucket?

6 个答案:

答案 0 :(得分:25)

您可以创建一个 lambda函数来清理您的存储桶,并使用 CustomResource 从您的CloudFormation堆栈调用您的lambda。

在清理你的水桶的lambda示例下面:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import boto3
from botocore.vendored import requests


def lambda_handler(event, context):
    try:
        bucket = event['ResourceProperties']['BucketName']

        if event['RequestType'] == 'Delete':
            s3 = boto3.resource('s3')
            bucket = s3.Bucket(bucket)
            for obj in bucket.objects.filter():
                s3.Object(bucket.name, obj.key).delete()

        sendResponseCfn(event, context, "SUCCESS")
    except Exception as e:
        print(e)
        sendResponseCfn(event, context, "FAILED")


def sendResponseCfn(event, context, responseStatus):
    response_body = {'Status': responseStatus,
                     'Reason': 'Log stream name: ' + context.log_stream_name,
                     'PhysicalResourceId': context.log_stream_name,
                     'StackId': event['StackId'],
                     'RequestId': event['RequestId'],
                     'LogicalResourceId': event['LogicalResourceId'],
                     'Data': json.loads("{}")}

    requests.put(event['ResponseURL'], data=json.dumps(response_body))

在上面创建lambda之后,只需将CustomResource放入CloudFormation堆栈中:

 ---
 AWSTemplateFormatVersion: '2010-09-09'

 Resources:

   myBucketResource:
     Type: AWS::S3::Bucket
     Properties:
       BucketName: my-test-bucket-cleaning-on-delete
     DependsOn: cleanupBucketOnDelete

   cleanupBucketOnDelete:
     Type: Custom::cleanupbucket
     Properties:
       ServiceToken: arn:aws:lambda:eu-west-1:123456789012:function:clean-bucket-lambda
       BucketName: my-test-bucket-cleaning-on-delete
  

请记住将一个角色附加到您的lambda,该角色有权从您的存储桶中删除对象。

此外,请记住,您可以使用lambda函数 cli2cloudformation 创建一个接受CLI命令行的lambda函数。您可以从here下载并安装。使用它你只需要创建一个类似于下面的CustomResource:

"removeBucket": {
        "Type": "Custom::cli2cloudformation",
        "Properties": {
          "ServiceToken": "arn:aws:lambda:eu-west-1:123456789000:function:custom-lambda-name",
          "CliCommandDelete": "aws s3 rb s3://bucket-name --force",
        }
}

答案 1 :(得分:3)

我认为您的DependsOn使用了错误的资源,至少它对我不起作用,因为在堆栈删除(通过控制台)上,它将首先尝试强制删除存储桶,这将失败,然后失败将尝试删除自定义资源,这会触发lambda清空存储桶。这将清空存储桶,但堆栈删除将失败,因为它试图在清空存储桶之前将其删除。我们要先启动自定义资源删除,然后在删除自定义资源后尝试删除存储桶,因此我做到了这一点,并且对我有用:

myBucketResource:
  Type: AWS::S3::Bucket
  Properties:
    BucketName: my-test-bucket-cleaning-on-delete

cleanupBucketOnDelete:
  Type: Custom::cleanupbucket
  Properties:
    ServiceToken: arn:aws:lambda:eu-west-1:123456789012:function:clean-bucket-lambda
    BucketName: my-test-bucket-cleaning-on-delete
  DependsOn: myBucketResource

这样,您可以确保不会首先删除存储桶,因为有另一个资源依赖于此存储桶,因此,先删除依赖资源(这会触发lambda清空存储桶),然后再删除存储桶。 希望有人觉得有帮助。

答案 2 :(得分:1)

botocore.vendored已过时,将于2021/01/30后从Lambda中删除。

这是更新版本

Type: AWS::Lambda::Function
Properties:
  Code: 
    ZipFile: 
      !Sub |
        import json, boto3, logging
        import cfnresponse
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)

        def lambda_handler(event, context):
            logger.info("event: {}".format(event))
            try:
                bucket = event['ResourceProperties']['BucketName']
                logger.info("bucket: {}, event['RequestType']: {}".format(bucket,event['RequestType']))
                if event['RequestType'] == 'Delete':
                    s3 = boto3.resource('s3')
                    bucket = s3.Bucket(bucket)
                    for obj in bucket.objects.filter():
                        logger.info("delete obj: {}".format(obj))
                        s3.Object(bucket.name, obj.key).delete()

                sendResponseCfn(event, context, cfnresponse.SUCCESS)
            except Exception as e:
                logger.info("Exception: {}".format(e))
                sendResponseCfn(event, context, cfnresponse.FAILED)

        def sendResponseCfn(event, context, responseStatus):
            responseData = {}
            responseData['Data'] = {}
            cfnresponse.send(event, context, responseStatus, responseData, "CustomResourcePhysicalID")            

  Handler: "index.lambda_handler"
  Runtime: python3.7
  MemorySize: 128
  Timeout: 60
  Role: !GetAtt TSIemptyBucketOnDeleteFunctionRole.Arn    

答案 3 :(得分:1)

要在python 3.8上运行它需要一点时间,所以我将其与社区共享。

Python 3.8 lambda不再支持from botocore.vendored import requests

您可以使用下面的代码通知Cloudformation。

import urllib

request = urllib.request.Request(event['ResponseURL'],
                                 method="PUT",
                                 data=json.dumps(response_body).encode('utf-8'),
                                 headers={'Content-Type': "''"})

with urllib.request.urlopen(request) as response:
    print("Status code: " + response.reason)

另一个小注意事项:当lambda收到来自cloudformation的create请求时,您可以在PhysicalResourceId响应中放入任何唯一值。如果是UPDATE / DELETE,则您还会从CloudFormation接收该参数,并且必须在响应中重用来自输入的值。

答案 4 :(得分:0)

怎么样:

import boto3

s3 = boto3.client('s3')
res = boto3.resource('s3')

buckets = s3.list_buckets()

try:
    for bk in buckets['Buckets']:
            bucket = res.Bucket(bk['Name'])
            bucket.objects.all().delete()
            bucket.delete()

except Exception as e:
    print e

答案 5 :(得分:0)

您应该清空存储桶:

$ aws s3 rm s3://bucket-name --recursive

然后删除存储桶

$ aws cloudformation delete-stack --stack-name mys3stack