AWS lambda: No module named 'cfnresponse'

后端 未结 2 1706
时光说笑
时光说笑 2021-01-18 03:27

I have deployed an AWS lambda which contains:

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

         


        
相关标签:
2条回答
  • 2021-01-18 04:18

    Cfnresponse is just available in lambda by default with python 2.7. Probably you are using 3.6.

    0 讨论(0)
  • 2021-01-18 04:24

    Did you specify your function's source code in ZipFile? If you specify an actual zip file, that module won't be included.

    When you use the ZipFile property to specify your function's source code and that function interacts with an AWS CloudFormation custom resource, you can load the cfn-response module to send responses to those resources. The module contains a send method, which sends a response object to a custom resource by way of an Amazon S3 presigned URL (the ResponseURL).

    If you need to use an actual zip file, or this doesn't work for any other reason, you can include the module yourself. Its source code is available in the link you provided. For Python 3 it's:

    #  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))
    
    0 讨论(0)
提交回复
热议问题