abrorkhamidov
abrorkhamidov

Reputation: 143

Getting error while creating index for elastic search

I am trying to create an elastic search index if it doesn't exist using OnModuleInit (NestJs). It works fine with my local machine but when I am trying to deploy to AWS ES Cluster, this part is giving such an error.

ElasticsearchModule.registerAsync({
          useFactory: async () => ({
            node: process.env.ES_ENDPOINT,
            maxRetries: 3,
            requestTimeout: 60000,
            pingTimeout: 60000,
          }),
        })

    export class ClientElasticSearchModule implements OnModuleInit {
      constructor(private clientSearchService: ClientElasticSearchService) {}
      async onModuleInit() {
        await this.clientSearchService.createIndex();
      }
    }

It is where I am creating an index:

await this.elasticsearchService.indices.create({
          index: clientsIndex,
          body: {
            settings: {
              analysis: {...},
            },
            mappings: {
              properties: {
                name: {
                  type: 'text',
                  fields: {
                    complete: {
                      type: 'text',
                      analyzer: 'autocomplete_analyzer',
                      search_analyzer: 'autocomplete_search_analyzer',
                    },
                  },
                },
              },
            },
          },
        }); 

Error message:

ResponseError: Response Error
    at onBody (/code/node_modules/@elastic/elasticsearch/lib/Transport.js:337:23)
    at IncomingMessage.onEnd (/code/node_modules/@elastic/elasticsearch/lib/Transport.js:264:11)
    at IncomingMessage.emit (node:events:402:35)
    at IncomingMessage.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
      meta: {
        body: '',
        statusCode: 403,
        headers: {
          date: 'Fri, 14 Jan 2022 16:18:46 GMT',
          'content-type': 'application/json',
          'content-length': '73',
          connection: 'keep-alive',
          'x-amzn-requestid': '8b3cded2-f210-4c79-ab48-ff517725a1e2',
          'access-control-allow-origin': '*'
        },
        meta: {
          context: null,
          request: [Object],
          name: 'elasticsearch-js',
          connection: [Object],
          attempts: 0,
          aborted: false
        }
      }
    }

Upvotes: 1

Views: 1170

Answers (1)

Brandon Max
Brandon Max

Reputation: 56

This looks like your AWS Opensearch (same tech under the hood of Elasticsearch) instance is returning a 403. This might mean you need to set up IAM Roles and the correct access for your instance. Please See the AWS Docs

Upvotes: 1

Related Questions