【问题标题】:AWS Lambda interact with elastic searchAWS Lambda 与弹性搜索交互
【发布时间】:2018-07-26 02:15:46
【问题描述】:

我是 AWS 领域的新手,并尝试开发一个概念证明,允许 lambda 函数与 elasticSearch(AWS 服务)和 S3 存储桶进行交互。

我不确定它是如何工作的。我们必须设置一个与 Lambda 函数相关联的 IAM 角色,但没有用户,所以第一个问题:

  • 运行 Lambda 函数的用户是谁?

我这样设置我的弹性搜索连接(使用https://github.com/DavidMuller/aws-requests-auth):

def connectES(esEndPoint):
print ('Connecting to the ES Endpoint {0}'.format(esEndPoint))
try:
    # let's talk to our AWS Elasticsearch cluster
    auth = BotoAWSRequestsAuth(
                aws_host='vpc-poc-lambda-3opu7gwdw7bwvtmmazjmdgo7gi.eu-west-3.es.amazonaws.com',
                aws_region='eu-west-3',
                aws_service='es')
    esClient = Elasticsearch(
        hosts=[{'host': esEndPoint, 'port': 443}],
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection,
        http_auth=auth)
    return esClient
except Exception as E:
    print("Unable to connect to {0}".format(esEndPoint))
    print(E)
    exit(3)

它似乎可以工作,但我不明白它使用什么凭据。 然后我成功创建了一个索引:

esClient.indices.create('test')

但是当我尝试索引这样的文档时,我得到了错误:

esClient.index(index='test', doc_type='post', id=123456, body={
            'author': 'John Doe',
            'blog': 'Learning Elasticsearch',
            'title': 'Using Python with Elasticsearch',
            'tags': ['python', 'elasticsearch', 'tips'],
        })


[WARNING]   2018-02-15T10:39:28.460Z    7f1cc69d-123c-11e8-be8b-cbbfad79068b    PUT https://vpc-****-****.eu-west-3.es.amazonaws.com:443/test/post/123456 [status:406 request:0.039s]
Document not indexed
Error:  string indices must be integers

我真的不明白为什么它不起作用,希望有一种简单的方法可以与 Lambda 的其他 AWS 服务进行交互。

你能帮帮我吗? 谢谢

【问题讨论】:

  • Amazon ElasticSearch 支持 IAM 凭证。我假设您已将 ES 配置为允许从运行 Lambda 函数的 IAM 角色进行访问。关于“字符串索引”错误,ES 文档建议索引函数的参数是一个字典,如 client.index({ index: 'myindex', type: 'mytype', id: '1', body: { 。 .. },标签:[...] })

标签: python amazon-web-services elasticsearch aws-lambda


【解决方案1】:

在我的一位同事的帮助下,我终于找到了问题所在。 当您尝试在旧版本中使用 elastisearch python 库和较新版本中的弹性搜索服务器时,错误会附加。 我将 lib (https://elasticsearch-py.readthedocs.io/en/master/Changelog.html) 升级到了最新版本,现在它运行良好。

如果打扰到你,我很抱歉,希望这能帮助像我这样的人。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2020-12-01
    • 1970-01-01
    • 2020-03-29
    • 2022-12-08
    • 2021-10-11
    • 2019-07-29
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多