使用python弹性搜索包访问aws弹性搜索角色



我正在使用deepset/haystack,并与弹性搜索进行通信。使用OpenDistroElasticsearchDocumentStore方法可以很好地使用用户名、pasword访问aws弹性搜索。在ec2中部署时,似乎无法使用基于角色的访问。请向我推荐一个使用python弹性搜索包访问aws弹性搜索的解决方案,给定一个角色访问

你的意思是这样在AWS上基于IAM的访问吗?我们最近刚刚合并了一个可能对您有所帮助的功能(#965(。请从master分支安装最新的Haystack版本,并尝试以下操作:

import boto3
from requests_aws4auth import AWS4Auth
from haystack.document_store.elasticsearch import ElasticsearchDocumentStore
from elasticsearch import RequestsHttpConnection
host = '<vpc_host>'
port = 443
region = 'eu-central-1'
service = 'es'

credentials = boto3.Session().get_credentials()
aws4auth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

document_store = OpenDistroElasticsearchDocumentStore(host=host,
port=port,
aws4auth=aws4auth,
# can't be used with default es client version used in e.g. aws sagemaker
embedding_field=None,
index="document")
from requests_aws4auth import AWS4Auth
from botocore.session import Session
credentials = Session().get_credentials()
auth = AWS4Auth(region='eu-west-1', service='es', refreshable_credentials=credentials)

此示例显示如何构造具有自动刷新凭据的AWS4Auth实例,该实例适用于使用AWS IAM角色的长时间运行的应用程序。RefreshableCredentials实例用于为每个请求生成有效的静态凭据,从而无需在临时凭据过期时重新创建AWS4Auth实例。

来源:https://github.com/tedder/requests-aws4auth#dynamic-sts凭据使用botocore可刷新凭据

该网站于2021年5月并入AWS4Auth。

最新更新