2016-11-02 12 views

उत्तर

13

आप अपने बाल्टी को साफ और एक CustomResource का उपयोग कर अपने CloudFormation ढेर से अपने लैम्ब्डा आह्वान करने के लिए एक लैम्ब्डा समारोह बना सकते हैं।

एक लैम्ब्डा उदाहरण नीचे

अपने बाल्टी सफाई:

#!/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)) 

आप ऊपर लैम्ब्डा बनाने के बाद, बस अपने CloudFormation ढेर में CustomResource डाल:

--- 
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 

एक भूमिका संलग्न करने के लिए याद रखें अपने लैम्ब्डा को जिसमें आपकी बाल्टी से वस्तुओं को हटाने की अनुमति है।

इसके अलावा ध्यान रखें कि आप एक लैम्ब्डा समारोह है कि लैम्ब्डा समारोह cli2cloudformation का उपयोग कर CLI कमांड लाइन को स्वीकार करता है बना सकते हैं में रहते हैं। आप here से डाउनलोड और इंस्टॉल कर सकते हैं। लेकिन तो मैं जवाब के रूप में चिह्नित नहीं कर सकता (मैं -

"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

यह एक बढ़िया तरीका CloudFormation से बाल्टी को हटाने को संभालने के लिए है, लेकिन मुझे लगता है कि इस सवाल का जवाब अभी नहीं है: का उपयोग करना है कि आप बस bellow तरह एक CustomResource बनाने की जरूरत इसे ऊपर उठाया) - धन्यवाद –

+0

यह उत्तर –

+0

पोस्ट करने के लिए बहुत बढ़िया धन्यवाद है इस विषय पर अधिक गहराई से ब्लॉग पोस्ट: https://community.alfresco.com/community/platform/blog/2016/10/13/how- ए-लैम्ब्डा-समर्थित-कस्टम-संसाधन-बचाया गया दिन – vincent

5

नहीं, मुझे नहीं लगता कि ऐसा करने का कोई तरीका है। यह document पुष्टि करता है कि।

संबंधित मुद्दे