AWS Lambda 函数无法导入模块“lambda_function”:没有名为“lambda_function”的模块错误

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

在 AWS 上,我正在创建一个如下所示的 Lambda 函数:

from __future__ import print_function
import time
import uuid
import sys
import socket
import elasticache_auto_discovery
from pymemcache.client.hash import HashClient

# ElastiCache settings
elasticache_config_endpoint = "my-endpoint"
nodes = elasticache_auto_discovery.discover(elasticache_config_endpoint)
nodes = map(lambda x: (x[1], int(x[2])), nodes)
memcache_client = HashClient(nodes)

######
# This function puts into memcache and get from it.
# Memcached is hosted using elasticache
######
def handler(event, context):

    # Create a random UUID... this will be the sample element we add to the cache.
    uuid_in = uuid.uuid4().hex
    
    # Put the UUID to the cache.
    memcache_client.set('uuid', uuid_in)
    
    # Get the item (UUID) from the cache.
    uuid_out = memcache_client.get('uuid')
    
    # Print the results
    if uuid_out == uuid_in:
        # this print should see the CloudWatch Logs and Lambda console.
        print "Success: Inserted: %s. Fetched %s from memcache." %(uuid_in, uuid_out)
    else:
        raise Exception("Bad value retrieved :(. Expected %s got %s." %(uuid_in, uuid_out)) 

    return "Fetched value from Memcached"

我在执行时收到此错误:

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

我已经遵循了这个教程: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/Lambda.html

关于问题根源有什么建议吗?

python amazon-web-services aws-lambda redis elastic-cache
1个回答
0
投票

如果您的 Python 文件名称不同,请将其重命名为 lambda_function.py。 压缩包含 lambda_function.py 的目录内容并将其上传到您的 Lambda 函数。 验证 Lambda 设置中的处理程序配置并确保其与函数名称(处理程序)匹配。

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