【问题标题】:Cannot import cfnresponse AWS lambda无法导入 cfnresponse AWS lambda
【发布时间】:2021-01-09 20:51:26
【问题描述】:

我在执行 aws lambda 函数时遇到错误问题,即“无法导入模块 'custom_resource_lambda':没有名为 cfnresponse 的模块”。我看到了一个案例,但解决方案是将运行时 python 版本更改为 2.7,但这对我不起作用。请让我知道我做错了什么。干杯

import json
import boto3
import cfnresponse
from common_helpers.constants_v2 import LOGGER_NAME
import os
import logging



DEBUG_MODE = os.getenv('DEBUG_MODE', 'false')
ENV_NAME = os.getenv('COMPUTE_ENVIRONMENT_NAME')

batch_client = boto3.client('batch')

# Configure logging
LOGGER = logging.getLogger(LOGGER_NAME)
if DEBUG_MODE == 'true':
    LOGGER.setLevel(logging.DEBUG)
else:
    LOGGER.setLevel(logging.INFO)


def handler(event, context):
    LOGGER.info("Received event: " + json.dumps(event))
    try:
        if event['RequestType'] == 'Delete':
            send(event, context, cfnresponse.SUCCESS)
            return
        response = batch_client.describe_compute_environments(
            computeEnvironments=[
                ENV_NAME,
            ],
        )
        compute_env_details = response.get('computeEnvironments')
        cluster_arn = compute_env_details[0]['ecsClusterArn']
        cluster_name = cluster_arn.split('/')[1]
        data = dict(Data=dict(
            ClusterArn=cluster_arn,
            ClusterName=cluster_name))
        event.update(data)
        cfnresponse.send(event, context, cfnresponse.SUCCESS)
    except BaseException as e:
        send(event, context, cfnresponse.FAILED)

【问题讨论】:

    标签: amazon-web-services aws-lambda


    【解决方案1】:

    您需要在您的 lambda 根目录中安装该库(无论 python 版本如何)

    为此,您需要在 lambda 函数的根目录中运行此命令

    pip install cfnresponse -t .
    

    安装完成后,压缩您的 lambda 并再次上传。错误应该会消失。

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2018-09-18
      • 2016-07-14
      • 2019-12-23
      • 2018-01-01
      相关资源
      最近更新 更多