Using official nodejs elasticsearch npm package on AWS Lambda Docker container sets the Content-Type header to [text/plain]

I am trying to publish a document to my self-hosted elasticsearch running on AWS. I wrote my code in nodejs, using v18 LTS, using @elastic/elasticsearch npm module version 8.10.0. When I run my code locally, outside of Docker, it works and my document is published. When I run it as a Lambda function from AWS, it fails with the following error:

ERROR	Promise was not fulfilled {
      status: 'rejected',
      reason: ResponseError: {"error":"Content-Type header [text/plain] is not supported","status":406}
          at SniffingTransport.request (/var/task/node_modules/@elastic/transport/lib/Transport.js:479:27)
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async Client.CreateApi [as create] (/var/task/node_modules/@elastic/elasticsearch/lib/api/api/create.js:43:12)
          at async Promise.allSettled (index 1)
          at async Runtime.processEvent [as handler] (/var/task/index.js:105:27) {
        meta: {
          body: [Object],
          statusCode: 406,
          headers: [Object],
          meta: [Object],
          warnings: [Getter]
        }
      }
    }

Once I received this error I modified my client creation to the following:

elasticsearch: {
        //node: process.env.ELASTICSEARCH_HOST,
        node: {
            url: new URL(process.env.ELASTICSEARCH_HOST),
            headers: {
                'content-type': 'application/vnd.elasticsearch+json; compatible-with=8',
            },
        },
        auth: {
            username: process.env.ELASTICSEARCH_USERNAME,
            password: process.env.ELASTICSEARCH_PASSWORD,
        },
        tls: {
            ca: fs.readFileSync(process.env.CA_FILE),
            rejectUnauthorized: true
        }
    }

and I call const esClient = new Client(elasticsearch); to create the client. I still get the same error when I try this. My Dockerfile is simple

FROM amazon/aws-lambda-nodejs:18
WORKDIR $LAMBDA_TASK_ROOT
# Copy only the files for running in Lambda
COPY package*.json .
RUN npm ci --omit=dev
COPY . .

CMD [ "index.processEvent" ]

I cannot figure out why the Content-Type header is being set to [text/plain]

I ran the Lambda locally on a Mac and it was successful. So, running locally with the same Dockerfile works and running on AWS Lambda does not.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.