自定义资源的 Lambda 函数出现问题:没有名为 cfnresponse 的模块

问题描述 投票:0回答:2

我正在尝试使用 Lambda 函数创建 CloudFormation 自定义资源。这是我的职能:

    "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" ] } ] ] },
        }
    }

当我尝试将此函数用于另一个模板中的自定义资源时,自定义资源无法稳定,并且我在该函数的 CloudWatch 日志中看到此错误:

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

根据 AWS 文档此处,当使用

cfnresponse
属性内联指定函数代码时,可将
ZipFile
python 包导入到 Lambda 函数中。那么为什么无法加载包呢?

amazon-web-services lambda aws-lambda aws-cloudformation
2个回答
1
投票

我确实使用 terraform 来部署 lambda 函数,并且必须自己嵌入该模块。使用 pip 和 virtualenv 从 https://pypi.python.org/pypi/cfn-response 获取它,或者使用您提到的 AWS 文档中附加的文件。

文档建议,如果您使用 cloudformation 内联代码(如 ZipFile),但我没有对此进行测试。


0
投票

如果您部署在cloudformation上,请使用内联代码

© www.soinside.com 2019 - 2024. All rights reserved.