Connecting to LocalStack OpenSearch from localhost Docker container

As I mentioned earlier, I use LocalStack to emulate AWS services during development phase. It works more or less well. Recently I faced a problem with LocalStack OpenSearch instance. I created one OpenSearch cluster using LocalStack. My LocalStack is installed via docker-compose file & it runs in a container. Now I have developed a Java application where I am using OpenSearch. This Java application is deployed in its own docker container. But when the application tries to connect to LocalStack OpenSearch endpoint, it fails to connect. Application gets a connection timeout for LocalStack OpenSearch endpoint.

The problem is DNS related. OpenSearch gives a domain as an endpoint. In my case it was “test-domain.ap-southeast-1.opensearch.localhost.localstack.cloud:4566”. But application docker container is not able to recognize the private domain name in its network.

After a bit of searching I found this GitHub link. The issue is similar. And one solution says that DNS resolving is available in LocalStack’s pro offering. But my personal opinion is that if I have to pay, I might very well create a small single node OpenSearch cluster in AWS & be done with it.

Another workaround was mentioned in the same link. That requires changing endpoint strategy to “path” while creating OpenSearch cluster. For that, few lines need to be changed in LocalStack docker-compose file. The details is mentioned in the above GitHub link. After doing that the endpoint becomes something similar to this “http://localhost.localstack.cloud:4566/opensearch/ap-southeast-1/test-domain”. Now at least the hostname is accessible from application docker container. But there is a new problem. I am using AwsSdk2Transport class to sign API requests to OpenSearch. And AwsSdk2Transport class only accepts hostname is its constructor. So passing the full path there doesn’t work. I could have tried Java high-level Rest Client. But that was not the preferred approach for me as I wanted to test using AwsSdk2Transport for signing the HTTP API requests.

Looks like there is no solution out there with the free version of LocalStack. At least, I couldn’t find one which can be readily used. So I went ahead & created a small one node test cluster for OpenSearch in my AWS account.

Leave a Comment