0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.