Home > database >  unable to connect to elasticsearch service using AWS lambda
unable to connect to elasticsearch service using AWS lambda

Time:12-23

I am running my lambdas on port:4566 using localstack using below image

version: "2.1"
services:
  localstack:
    image: "localstack/localstack"
    container_name: "localstack"
    ports:
      - "4566-4620:4566-4620"
      - "127.0.0.1:8055:8080"
    environment:
      - SERVICES=s3,es,dynamodb,apigateway,lambda,sns,sqs,sloudformation
      - DEBUG=1
      - EDGE_PORT=4566
      - DATA_DIR=/var/lib/localstack/data
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOST_TMP_FOLDER=${TMPDIR}
      - LAMBDA_EXECUTOR=docker
      - DYNAMODB_SHARE_DB=1
      - DISABLE_CORS_CHECKS=1
      - AWS_DDB_ENDPOINT=http://localhost:4566

    volumes:
      - "${TMPDIR:-/var/lib/localstack}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - "local"

  elasticsearch:
    container_name: tqd-elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:7.0.1
    # volumes:
    #   - esdata:/usr/share/elasticsearch/data
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    depends_on:
      - "localstack"
    logging:
      driver: none
    ports:
      - 9300:9300
      - 9200:9200
    networks:
      - "local"

networks:
  local:
    driver: "bridge"

Problem: Not getting any response from elasticsearch while calling it from lambda
This is my lambda code

module.exports.createIndex = async () => {
    
   
    const elasticClient = new Client({
        node: "http://localhost:9200"
    });
    console.log("before the client call")
     console.log(getIndices().then(res => { console.log(res) }).catch(err => {
        console.log(err)
    }))
    console.log("after the client call")

   
       
    const getIndices = async () =>
    {
        return await elasticClient.indices.create({
            index:"index-from-lambda"
        })
    }
   
    return {
        statusCode: 201,
        body: JSON.stringify({
            msg:"index created successfully"
        })
    }
}
logs on my docker image,
before the client call
Promise { <pending> }
console.log("after the client call")

After this even when i go to bash and validate whether this index has been created or not , it returns empty set of indexes i.e. no index has been created
But, the same code works fine i.e. creates index on elasticsearch at port 9200 while called from httpserver @port 3000 and from standalone javascript file standalone server code

const express = require('express')

const app = express();
const { Client } = require('@elastic/elasticsearch');

const elasticClient = new Client({
  node: "http://localhost:9200"
});

app.listen(3000, () => {
    console.log('listening to the port 3000')
})

const getIndices = async () =>
{
  return await elasticClient.cat.indices()
}

console.log(getIndices().then(res => { console.log(res) }).catch(err => {
  console.log(err)
}))

this is standalone js script

const { Client } = require('@elastic/elasticsearch');

const elasticClient = new Client({
  node: "http://localhost:9200"
});

const getIndices = async () =>
{
  return await elasticClient.cat.indices()
}

console.log(getIndices().then(res => { console.log(res) }).catch(err => {
  console.log(err)
}))

Is this any kind of networking error or docker image error?

CodePudding user response:

This issue has been listed out here, The problem is with the endpoint. So localhost actually addresses your docker container, not your host machine. If you run your express server on the host, please use host.docker.internal as hostname, which will address the host from your docker container. Same is the thing with Elasticsearch image. Now code becomes,

elasticClient = new Client({
    node:"http://host.docker.internal:9200"
})

Rest remains the same.

  • Related