【发布时间】: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