如何使用 AWS Lambda 函数访问支持 VPC 的 Elasticsearch

How to Access VPC enabled Elasticsearch Using AWS Lambda function

我正在尝试使用 lambda 函数访问弹性搜索集群。我面临的问题是它给出了一个错误。响应是:

{
  "errorMessage": "Failed to parse: https://[https://vpc-myendpoint-us-east-1.es.amazonaws.com/]:443/_cluster/health",
  "errorType": "InvalidURL",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n    print(es.cluster.health())\n",
    "  File \"/var/task/elasticsearch/client/utils.py\", line 153, in _wrapped\n    return func(*args, params=params, headers=headers, **kwargs)\n",
    "  File \"/var/task/elasticsearch/client/cluster.py\", line 66, in health\n    return self.transport.perform_request(\n",
    "  File \"/var/task/elasticsearch/transport.py\", line 381, in perform_request\n    status, headers_response, data = connection.perform_request(\n",
    "  File \"/var/task/elasticsearch/connection/http_requests.py\", line 159, in perform_request\n    prepared_request = self.session.prepare_request(request)\n",
    "  File \"/var/task/requests/sessions.py\", line 456, in prepare_request\n    p.prepare(\n",
    "  File \"/var/task/requests/models.py\", line 316, in prepare\n    self.prepare_url(url, params)\n",
    "  File \"/var/task/requests/models.py\", line 384, in prepare_url\n    raise InvalidURL(*e.args)\n"
  ]
}

我的代码在 python 中,如下所示:

from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import boto3


def lambda_handler(event, context):
    region = 'us-east-1'
    host = 'https://vpc-myendpoint.us-east-1.es.amazonaws.com/'
    service = 'es'
    
    credentials = boto3.Session().get_credentials()
    awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service,
                       session_token=credentials.token)

    es = Elasticsearch(
        hosts=[{'host': host, 'port': 443}],
        http_auth= awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )

    print(es.cluster.health())


我该如何解决这个错误。我的猜测是启用 VPC 的端点使用不同的技术进行访问。如果是,如何访问它们?

提前致谢

查看报错信息:

"Failed to parse: https://[https://vpc-myendpoint-us-east-1.es.amazonaws.com/]:443/_cluster/health",

https://[https://vpc-myendpoint-us-east-1.es.amazonaws.com 不是有效的 URL.

您在需要主机名的变量中指定 URL。

改变这个:

host = 'https://vpc-myendpoint.us-east-1.es.amazonaws.com/'

为此:host = 'vpc-myendpoint.us-east-1.es.amazonaws.com'