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
asked 2 years ago332 views
2 Answers
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
EXPERT
answered 2 years ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions