【问题标题】:Setting the logging to Elasticsearch using python-elasticsearch-extension in AWS Lambda在 AWS Lambda 中使用 python-elasticsearch-extension 将日志记录设置为 Elasticsearch
【发布时间】:2021-02-02 09:22:30
【问题描述】:

我需要将 AWS lambda 日志记录到 AWS Elasticsearch(ES) 域。默认情况下,日志会推送到 Cloudwatch。不必将日志发送到 CloudWatch,而必须使用 python-example-elasticsearch-extension 发送到 ES

为实现这一点,已将 elasticsearch 扩展添加到 lambda 层,并在 lambda 函数中添加层。在 lambda 函数中导入扩展时,它正在工作,但无法在 Elasticsearch 中看到日志。在文档中,添加扩展和 ES_ENDPOINT 和 ES_INDEX 应该将 lambda 日志记录到 ES 但不起作用。

如何使用 Elasticsearch 扩展在 lambda 中设置记录器,以使用 LogsExtensionAPI 将日志流式传输到 Elasticsearch 而不是 Cloudwatch。

Lambda 代码:

import json
import extensions.logs_api_elasticsearch_extension as logs_api_elasticsearch_extension
elogger = logs_api_elasticsearch_extension()

def lambda_handler(event, context):
    try:
        print("[info] test esext")
    except Exception as e:
        print("[error] test esext")

执行结果:

   Function Logs
    START RequestId: 47d30bec-8b9b-4fb6-ae45-41952f1a5d66 Version: $LATEST
    Starting Extensions {'events': ['INVOKE', 'SHUTDOWN']} {'destination': {'protocol': 'HTTP', 'URI': 'http://sandbox:4243'}, 'types': ['platform', 'function'], 'buffering': {'timeoutMs': 1000, 'maxBytes': 262144, 'maxItems': 10000}}
    Initializing LogsAPIExternalExtension logs_api_elasticsearch_extension.py
    Registering to ExtensionsAPIClient on http://127.0.0.1:9001/2020-01-01/extension
    Initializing HTTP Server on 0.0.0.0:4243
    Subscribing to Logs API on http://127.0.0.1:9001/2020-08-15
    Successfully subscribed to Logs API: b'"AlreadySubscribed"'
    Serving LogsAPIHTTPExternalExtension logs_api_elasticsearch_extension.py
    Serving HTTP Server on 0.0.0.0:4243
    166.255.279.10 - - [02/Feb/2021 09:12:52] "POST / HTTP/1.1" 200 -
    166.255.279.10 - - [02/Feb/2021 09:12:52] "POST / HTTP/1.1" 200 -
    [ERROR] TypeError: 'module' object is not callable
    Traceback (most recent call last):
      File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
        return load_source(name, filename, file)
      File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
        module = _load(spec)
      File "<frozen importlib._bootstrap>", line 702, in _load
      File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 783, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/var/task/lambda_function.py", line 7, in <module>
        elogger = logs_api_elasticsearch_extension()EXTENSION   Name: logs_api_elasticsearch_extension.py   State: Ready    Events: [INVOKE,SHUTDOWN]
    Received response from ExtensionsAPIClient: b'{"eventType":"SHUTDOWN","deadlineMs":1612257175241,"shutdownReason":"failure"}'
    166.255.279.10 - - [02/Feb/2021 09:12:53] "POST / HTTP/1.1" 200 -
    END RequestId: 47d30bec-8b9d-4fb6-ae45-41952f1a5d66

【问题讨论】:

    标签: python elasticsearch aws-lambda


    【解决方案1】:

    AWS Lambda 函数和 AWS Elastic 搜索集群应该在同一个 VPC 中或通过 vpc 对等连接具有适当的连接性。可从 lambda 函数访问的弹性搜索集群。

    以下是弹性搜索集群的屏幕截图,您可以在其中提及它是否是可公开访问的堆栈。

    以下是 AWS Lambda for VPC 设置的屏幕截图。

    检查 AWS IAM 策略以获取最少搜索访问权限。

    https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/ https://aws.amazon.com/premiumsupport/knowledge-center/es-troubleshoot-cloudwatch-logs/ https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html

    【讨论】:

    • 角色被分配给 lambda 函数以访问 ES 域。你是说这个错误是由于权限问题造成的。
    • 出于测试目的,请您尝试将一些日志从 cloudwatch 日志发送到 ES。并检查弹性搜索访问策略:docs.aws.amazon.com/elasticsearch-service/latest/developerguide/…
    • 有道理,会将 CloudWatch 日志发送到 ES。
    猜你喜欢
    • 2022-08-12
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2021-08-26
    • 2014-02-26
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多