【问题标题】:What is wrong in the following Lambda code that throws up module error?以下引发模块错误的 Lambda 代码有什么问题?
【发布时间】:2019-08-29 15:32:45
【问题描述】:

使用以下代码创建一个连接到 Amazon AWS 的 API。这是我使用的亚马逊 Lambda 代码-

导入 boto3 导入json 导入请求 从 requests_aws4auth 导入 AWS4Auth

region = 'us-east-1' 
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, 
service, session_token=credentials.token)

host = 'XXX.com'
index = 'items'
url = 'https://' + host + '/' + index + '/_search'

# Lambda execution starts here
def handler(event, context):

# Put the user query into the query DSL for more accurate search results.
# Note that certain fields are boosted (^).
query = {
    "query": {
    "multi_match": {
            "query": event['queryStringParameters']['q'],
            }
    }
}

# ES 6.x requires an explicit Content-Type header
headers = { "Content-Type": "application/json" }

# Make the signed HTTP request
r = requests.get(url, auth=awsauth, headers=headers, 
data=json.dumps(query))

# Create the response and add some extra content to support CORS
response = {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": '*'
    },
    "isBase64Encoded": False
}

# Add the search results to the response
response['body'] = r.text
return response

这应该连接到带有端点 XXX.com 的 AWS ES 集群

尝试测试时获取输出 -

START RequestId: f640016e-e4d6-469f-b74d-838b9402968b Version: $LATEST
Unable to import module 'index': Error
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
END RequestId: f640016e-e4d6-469f-b74d-838b9402968b
REPORT RequestId: f640016e-e4d6-469f-b74d-838b9402968b  Duration: 
44.49 ms    Billed Duration: 100 ms     Memory Size: 128 MB Max 
Memory Used: 58 MB

【问题讨论】:

  • 这意味着你的处理函数有语法错误。检查你的缩进是否正确。

标签: json aws-lambda xmlhttprequest


【解决方案1】:

在创建 Lambda 函数时,我们需要指定 handler,这是您代码中的一个函数,AWS Lambda 服务可以在执行给定的 Lambda 函数时调用该函数。

默认情况下,使用 lambda_function.lambda_handler 的处理程序创建 Python Lambda 函数,这表示服务必须调用包含在 lambda_function 模块中的 lambda_handler 函数。

从您收到的错误来看,handler 似乎被错误地配置为index.<something>,并且由于您的部署包中没有名为index 的 Python 模块,Lambda 无法导入相同的为了开始执行。

【讨论】:

    【解决方案2】:
    • 如果我能正确连接到 AWS ES 集群,你需要这样的东西
    import gitlab
    import logging
    from elasticsearch import Elasticsearch, RequestsHttpConnection
    from requests_aws4auth import AWS4Auth
    import boto3
    #from aws_requests_auth.aws_auth import AWSRequestsAuth
    
    LOGGER = logging.getLogger()
    ES_HOST = {'host':'search-testelasticsearch-xxxxxxxxxx.eu-west-2.es.amazonaws.com', 'port': 443}
    
    
    def lambda_handler(event, context):
        LOGGER.info('started')
        dump2={
            'number': 9
        }
    
        service = 'es'
        credentials = boto3.Session().get_credentials()
        print('-------------------------------------------')
        print(credentials.access_key)
        print(credentials.secret_key)
        print('--------------------------------------------------------')
        awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, "eu-west-2", service, session_token=credentials.token)
        es = Elasticsearch(hosts=[ES_HOST], http_auth = awsauth, use_ssl = True, verify_certs = True, connection_class = RequestsHttpConnection)
        DAVID_INDEX = 'test_index'
        response = es.index(index=DAVID_INDEX, doc_type='is_this_important?', body=dump2, id='4')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 2011-02-28
      相关资源
      最近更新 更多