【问题标题】:opensearch authentication with opensearch-py on aws lambda在 aws lambda 上使用 opensearch-py 进行 opensearch 身份验证
【发布时间】:2021-11-30 06:20:04
【问题描述】:

我正在尝试使用 opensearch python 客户端从 AWS Lambda 连接到 AWS OpenSearch 域(开发目的,非生产)。

我正在尝试以下方法:

from opensearchpy import OpenSearch
import boto3
from requests_aws4auth import AWS4Auth
import os
import config
my_region = os.environ['AWS_REGION']
service = 'es' # still es???
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, my_region, service, session_token=credentials.token)
openSearch_endpoint = config.openSearch_endpoint

# sth wrong here:
openSearch_client = OpenSearch(hosts = [openSearch_endpoint], auth = awsauth)

根据以下博客:

但它不起作用(它不想进行身份验证,"errorMessage":"AuthorizationException(403, '')"。但是,如果我不使用 python 客户端而只是通过 requests 代替:

import requests
host = config.openSearch_endpoint
url = host + '/' +'_cat/indices?v'
# this one works:
r = requests.get(url, auth=awsauth)

,我的 lambda 函数确实与 OpenSearch 域进行通信。

我查阅了OpenSearch() 文档,但我不清楚它的参数如何映射到boto3 会话凭据和/或AWS4Auth。那么这一行应该是什么

openSearch_client = OpenSearch(hosts = [openSearch_endpoint], auth = awsauth)

是吗?

【问题讨论】:

    标签: amazon-web-services aws-lambda boto3 amazon-iam opensearch


    【解决方案1】:

    OpenSearch 服务需要端口 443 来接收传入请求,因此您需要在附加到您的 OpenSearch 服务域的安全组下添加新的入站规则。

    试试规则:

    • 类型:HTTPS
    • 协议:TCP
    • 端口范围:443
    • 来源:0.0.0.0/0 (Anywhere-IPv4)

    此外,您应该为您的 Lambda 函数制定基于资源的策略,以执行对您的 OpenSearch 服务域的请求。

    【讨论】:

      【解决方案2】:

      实际上在几个小时后找到了解决方案:

      from opensearchpy import OpenSearch, RequestsHttpConnection
      my_region = os.environ['AWS_REGION']
      service = 'es' # still es?
      credentials = boto3.Session().get_credentials()
      awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, my_region, service, session_token=credentials.token)
      host = config.openSearch_endpoint
      
      openSearch_client = OpenSearch(
              hosts=[openSearch_endpoint],
              http_auth = awsauth,
              use_ssl = True,
              verify_certs = True,
              ssl_assert_hostname = False,
              ssl_show_warn = False,
              connection_class=RequestsHttpConnection
              )
      
      

      【讨论】:

        猜你喜欢
        • 2021-12-21
        • 2022-10-05
        • 2021-10-05
        • 2022-01-07
        • 2019-06-19
        • 2022-11-05
        • 2021-12-22
        • 2023-01-27
        • 2016-09-27
        相关资源
        最近更新 更多