我正在尝试使用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文档here,当使用cfnresponse
属性内联指定功能代码时,ZipFile
python包可用于导入Lambda函数。那么为什么不能加载包呢?
答案 0 :(得分:1)
我确实使用terraform来部署lambda函数,并且必须自己嵌入模块。要么使用pip和virtualenv从https://pypi.python.org/pypi/cfn-response获取它,要么使用附加到您提到的AWS文档的文件。
文档建议,如果你使用cloudformation 和内联代码(如ZipFile),但我没有测试它。