RKO
RKO

Reputation: 122

Async connection to AWS elasticsearch

I am struggling to connect to AWS elasticsearch with async connection.

Regular sync connection works fine:

from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection

AWS_ACCESS_KEY_ID = "<id>"
AWS_SECRET_ACCESS_KEY = "<key>"
AWS_REGION = "us-east-1"
awsauth = AWS4Auth(AWS_ACCESS_KEY_ID,
                   AWS_SECRET_ACCESS_KEY,
                   AWS_REGION, 'es')


es = Elasticsearch(
        ['https://url-to-elastic-in-aws/'],
        http_auth=awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )

print(await es.info())

But async connection with connection_class=RequestsHttpConnection or connection_class=RequestsHttpConnection throw an exception

from requests_aws4auth import AWS4Auth
from elasticsearch import AsyncElasticsearch, AIOHttpConnection

AWS_ACCESS_KEY_ID = "<id>" 
AWS_SECRET_ACCESS_KEY = "<key>" 
AWS_REGION = "us-east-1" 
awsauth = AWS4Auth(AWS_ACCESS_KEY_ID,
                   AWS_SECRET_ACCESS_KEY,
                   AWS_REGION, 'es')


es = AsyncElasticsearch(
        ['https://url-to-elastic-in-aws/'],
        http_auth=awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=AIOHttpConnection
    )

print(await es.info())

It is either: TypeError: object tuple can't be used in 'await' expression it connection_class=RequestsHttpConnection

or

AttributeError: 'AWS4Auth' object has no attribute 'encode'

I search the Internet and could not find a solution for this. Is there any way to connect to AWS elasticsearch with AsyncElasticsearch class.

Upvotes: 0

Views: 1191

Answers (2)

Perye LI
Perye LI

Reputation: 1

This answer solves my problem https://stackoverflow.com/a/77082892. You can use AWSV4SignerAsyncAuth rather than AWSV4SignerAuth for auth and AsyncHttpConnection as the connection class in async scenarios.

Upvotes: 0

Jim
Jim

Reputation: 11

I believe this is a known limitation with RequestsHttpConnection, and has been since last year: https://github.com/elastic/elasticsearch-py/issues/1333#issuecomment-676278567

The recommendation is to not use the async elasticsearch/opensearch client until RequestsHttpsConnection is updated.

Upvotes: 1

Related Questions