i-dot-ai / redbox

Bringing Generative AI to the way the Civil Service works
https://i-dot-ai.github.io/redbox/
MIT License
83 stars 27 forks source link

[AWS SUPPORT] Add Amazon OpenSearch support #758

Open dawidcx3 opened 1 month ago

dawidcx3 commented 1 month ago

Currently application uses Elasticsearch service with cloud_id and api_key requirement as here: https://elasticsearch-py.readthedocs.io/en/v8.14.0/api/elasticsearch.html. Current connection can be found here: https://github.com/i-dot-ai/redbox/blob/main/redbox-core/redbox/models/settings.py#L211

AWS does not support Elasticsearch anymore and it uses Amazon OpenSearch instead. We prepared and tested a code that creates a connection with Amazon OpenSearch on AWS platform:

from opensearchpy import OpenSearch, AWSV4SignerAuth, RequestsHttpConnection
import boto3

credentials = boto3.Session().get_credentials()
auth = AWSV4SignerAuth(credentials, "eu-west-2")

host = 'vpc-redbox-dev-tggskxn42wlh7czdzdqvuuolfy.eu-west-2.es.amazonaws.com'
port = 443

client = OpenSearch(
  hosts = [{'host': host, 'port': port}],
  http_compress = True,
  use_ssl = True,
  http_auth = auth,
  verify_certs = True,
  connection_class = RequestsHttpConnection
)

info = client.info()
print(f"Welcome to {info['version']['distribution']} {info['version']['number']}!")

More details can be found in OpenSearch documentation: https://opensearch.org/docs/latest/clients/python-low-level/

Our recommended way of authentication is to use IAM Role prepared from infrastructure side. To use it is required to add the following lines:

credentials = boto3.Session().get_credentials()
auth = AWSV4SignerAuth(credentials, "eu-west-2")

Environment variables to set hostname and port are also required as well.