AWS Lambda:没有名为“ cfnresponse”的模块

Den*_*boy 4 amazon-web-services aws-cloudformation aws-lambda

我已经部署了一个AWS Lambda,其中包含:

import json
import boto3
import cfnresponse
import urllib.request
from botocore.exceptions import ClientError

def lambda_handler(event, context):
...
cfnresponse.send(event, context, status, responseData, "CustomResourcePhysicalID")
Run Code Online (Sandbox Code Playgroud)

我试图cfnresponse在我的cloudformations中获取的值:

Resources:
  API:
    Type: Custom::API
    Version: '1.0'
    Properties:
      ServiceToken: arn:aws:lambda:eu-west-1:1234567891011:function:getCountry
Outputs:
  Status:
    Value:
       !GetAtt API.Data
Run Code Online (Sandbox Code Playgroud)

现在,cfn卡住了,我得到了错误:

Unable to import module 'lambda_function': No module named 'cfnresponse' 
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?当我检查文档时,似乎是这样做的方法。

kic*_*hik 7

您是否在 中指定了函数的源代码ZipFile?如果您指定实际的 zip 文件,则不会包含该模块。

当您使用ZipFile属性指定函数的源代码并且该函数与 AWS CloudFormation 自定义资源交互时,您可以加载 cfn-response 模块以向这些资源发送响应。该模块包含一个 send 方法,该方法通过 Amazon S3 预签名 URL(ResponseURL)将响应对象发送到自定义资源。

如果您需要使用实际的 zip 文件,或者由于任何其他原因这不起作用,您可以自己包含该模块。它的源代码可在您提供的链接中找到。对于 Python 3,它是:

#  Copyright 2016 Amazon Web Services, Inc. or its affiliates. All Rights Reserved.
#  This file is licensed to you under the AWS Customer Agreement (the "License").
#  You may not use this file except in compliance with the License.
#  A copy of the License is located at http://aws.amazon.com/agreement/ .
#  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
#  See the License for the specific language governing permissions and limitations under the License.

from botocore.vendored import requests
import json

SUCCESS = "SUCCESS"
FAILED = "FAILED"

def send(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False):
    responseUrl = event['ResponseURL']

    print(responseUrl)

    responseBody = {}
    responseBody['Status'] = responseStatus
    responseBody['Reason'] = 'See the details in CloudWatch Log Stream: ' + context.log_stream_name
    responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name
    responseBody['StackId'] = event['StackId']
    responseBody['RequestId'] = event['RequestId']
    responseBody['LogicalResourceId'] = event['LogicalResourceId']
    responseBody['NoEcho'] = noEcho
    responseBody['Data'] = responseData

    json_responseBody = json.dumps(responseBody)

    print("Response body:\n" + json_responseBody)

    headers = {
        'content-type' : '',
        'content-length' : str(len(json_responseBody))
    }

    try:
        response = requests.put(responseUrl,
                                data=json_responseBody,
                                headers=headers)
        print("Status code: " + response.reason)
    except Exception as e:
        print("send(..) failed executing requests.put(..): " + str(e))
Run Code Online (Sandbox Code Playgroud)


小智 5

Cfnresponse仅在python 2.7中默认在lambda中可用。可能您正在使用3.6。

  • 截至 2019 年 12 月,情况并不完全正确。CloudFormation 负责将“cfnresponse.py”包含为**本地模块**,但[仅当您使用时](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function -code-cfnresponsemodule.html) ZipFile 属性和确切的“import cfnresponse”语句。 (7认同)