AWS Cloudformation

0

Hi, I need help in fixing the template. This is snippet of cloudformation template. Its creating the folder in the s3 bucket - datasource as required but Custom::S3CustomResource is stuck in CREATE_IN_PROGRESS. Please let know if there is a solution.

  S3CustomResource:
    Type: Custom::S3CustomResource
    Properties:
      ServiceToken: !GetAtt AWSLambdaFunction.Arn
      the_bucket: !Sub 'pe-ftv-${Environment}-dms-s3buckets'
  AWSLambdaFunction:
    Type: "AWS::Lambda::Function"
    Properties:
      FunctionName: !Sub 'pe-ftv-${Environment}-lambda'
      Handler: index.handler
      Role: !GetAtt AWSLambdaExecutionRole.Arn
      Timeout: 5
      Runtime: python3.9
      Code:
        ZipFile: |
          import boto3
          import cfnresponse
          def handler(event, context):
            bucket_name = event['ResourceProperties']['the_bucket']
            s_3 = boto3.client('s3')
            directory_name = "datasource"
            s_3.put_object(Bucket= bucket_name, Key=(directory_name+'/'))
            cfnresponse.send(event,
                                   context,
                                   cfnresponse.SUCCESS)
            return
질문됨 2년 전354회 조회
2개 답변
2

Is this a concrete use case or just an example to test Custom Resources? If this is a concrete use case, can you explain why do you need to create a directory in the bucket? With Amazon S3 folders are just determined implicitly from the keys of the objects you store. If you store an object with key alfa/beta/gamma.txt in an empty bucket when browsing the S3 bucket you will see alfa and beta represented as folders.

Regarding your question, the Cloud Formation stack might get stuck because your customer resource lambda code contains an error and does not return the proper failure message to CloudFormation (see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html)

AWS
전문가
답변함 2년 전
  • We need the directory/folder inside the s3 bucket. Is there a way to make cloudformation return the error, reason its doing what is suppose to do, creating the directory.

  • You need to catch possible errors in the lambda function and return a FAILED status. The cfnresponse module does not allow to specify the value of the Reason field, which is defined by the library itself. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html#w2ab1c23c23c16b9c15

  • Not sure why - we need to add empty dictionary in python string response_data = {} and send in response. cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)

  • Hi.
    As MassimilianoAWS says, first you need to set cfnresponse for error handling.

    And check the execution log of the Lambda function you created for your custom resource to see why the error is occurring.
    You may be able to get detailed information about the error.
    For example, if the Lambda execution role does not have permission to the target bucket.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠