1
votes

I'm attempting to create a CloudFormation custom resource using a Lambda function. This is my function:

    "SubnetToVpcFunction": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "ZipFile" : { "Fn::Join" : ["\n", [
                    "import cfnresponse",
                    "import json, boto3 ",
                    "def handler(event, context): ",
                    "   ec2 = boto3.resource('ec2') ",
                    "   subnet = ec2.Subnet(event['ResourceProperties']['Subnet']) ",
                    "   vpc_id = subnet.vpc_id ",
                    "   responsedata = { 'VPCID' : vpc_id } ",
                    "   cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, \"CustomResourcePhysicalID\") "
                ] ] }
            },
            "Handler": "index.handler",
            "Runtime": "python2.7",
            "Timeout": "30",
            "Role": { "Fn::Join" : [ "", [ "arn:aws:iam::", { "Ref" : "AWS::AccountId" }, ":role/", { "Fn::FindInMap" : [ "AccountMapping", { "Ref" : "AWS::AccountId" }, "Role" ] } ] ] },
        }
    }

When I attempt to use this function for a custom resource in another template, the custom resource fails to stabilize, and I see this error in the CloudWatch logs for the function:

Unable to import module 'index': No module named cfnresponse

According to the AWS documentation here, the cfnresponse python package is available for importing in Lambda functions when the function code is specified inline using the ZipFile property. So why can't it load the package?

1

1 Answers

1
votes

I did use terraform to deploy the lambda function and had to embed the module myself. Either get it from https://pypi.python.org/pypi/cfn-response, using pip and virtualenv or using the file attached to the AWS documentation you mentioned.

The documentation suggest, that if you use cloudformation and inline code (as ZipFile) but I didn't test that.