1
votes

I'm trying to create a custom resource in a CFT that will run a lambda function on creation of said template. I've looked at the AWS documentation for Lambda-Backed Custom Resources, but I'm still a bit confused on the topic as the documentation was not particularly verbose. I've included the JSON for my custom resource, and I'm just wondering if there's anything else I have to do in order to ensure that this resource will call on the function upon creating the template.

"LambdaRunner": {
            "Type": "AWS::CloudFormation::CustomResource",
            "Properties": {
                "ServiceToken": {
                    "Fn::GetAtt": [
                        "DistroDBPop",
                        "Arn"
                    ]
                }
            }

Note: The Lambda function it references takes a CSV from an S3 resource and uses the information to create and populate a DynamoDB table.

1

1 Answers

4
votes

That looks sufficient as far as calling the function, assuming that the CloudFormation template contains a Lambda function called DistroDBPop.

If you look at Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation, you'll see that several other elements are also needed:

  • The Lambda function
  • A Role for the Lambda function
  • A special callback in the Lambda function to indicate that it has completed

There's some good example Lambda code at: stelligent/cloudformation-custom-resources - GitHub

There is also a cfnresponse module that makes it easier to callback at the end of the Lambda function. See: cfn-response Module

Finally, make sure the Lambda function understands that it might be called at Create, Update and Delete of the stack, so it might need to 'ignore' certain events unless they are relevant.