【发布时间】: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)
根据以下博客:
- https://aws.amazon.com/blogs/database/indexing-metadata-in-amazon-elasticsearch-service-using-aws-lambda-and-python/
- https://docs.aws.amazon.com/opensearch-service/latest/developerguide/search-example.html
但它不起作用(它不想进行身份验证,"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