Ask Your Question
2

What is the process to generate an elasticsearch index via a CDK custom resource?

asked 2023-07-12 11:20:43 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-12 11:29:02 +0000

ladyg gravatar image

To generate an Elasticsearch index via a CDK custom resource, you can use the following process:

  1. Create a custom resource in your CDK stack using AWS CloudFormation.
  2. In the custom resource's lambda function, include the necessary dependencies to communicate with Elasticsearch, such as the AWS SDK for JavaScript and the Elasticsearch node.js client.
  3. In the lambda function, retrieve any required properties or parameters from the custom resource's input properties.
  4. Use the Elasticsearch node.js client to create or update the desired Elasticsearch index with the specified settings and mappings.
  5. Return a CloudFormation response indicating the success or failure of the custom resource creation.

Here is a sample code for creating an Elasticsearch index:

import * as elasticsearch from 'elasticsearch';
import { CustomResourceProvider, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Stack } from 'aws-cdk-lib/core';
import { CfnCustomResource } from 'aws-cdk-lib/aws-cloudformation';

const ES_ENDPOINT = 'your-es-endpoint';
const INDEX_NAME = 'your-index-name';

export class ElasticsearchIndex extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const provider = new CustomResourceProvider(this, 'ESIndexProvider', {
      onEventHandler: this.createIndexHandler,
      isCompleteHandler: this.isIndexCreated,
    });

    const indexResource = new CfnCustomResource(this, 'ESIndex', {
      serviceToken: provider.serviceToken,
      properties: {
        IndexName: INDEX_NAME,
        Settings: {
          NumberOfShards: 2,
          NumberOfReplicas: 1,
        },
        Mappings: {
          properties: {
            Name: {
              type: 'text',
              fields: {
                keyword: {
                  type: 'keyword',
                },
              },
            },
          },
        },
      },
    });
  }

  // called when the custom resource is created or updated
  private async createIndexHandler(event: any) {
    const esClient = new elasticsearch.Client({
      apiVersion: '7.4',
      hosts: [`${ES_ENDPOINT}`],
    });

    const indexName = event.ResourceProperties.IndexName;
    const settings = event.ResourceProperties.Settings;
    const mappings = event.ResourceProperties.Mappings;

    const indexExists = await esClient.indices.exists({ index: indexName });

    if (!indexExists) {
      // create index with settings and mappings
      await esClient.indices.create({
        index: indexName,
        body: {
          settings,
          mappings,
        },
      });

      return { Data: 'success' };
    }

    return { Data: 'already-exists' };
  }

  // called to determine if the custom resource has been fully created
  private async isIndexCreated(event: any) {
    const esClient = new elasticsearch.Client({
      apiVersion: '7.4',
      hosts: [`${ES_ENDPOINT}`],
    });

    const indexName = event.ResourceProperties.IndexName;

    const indexExists = await esClient.indices.exists({ index: indexName });

    if (indexExists) {
      return { IsComplete: true };
    }

    return { IsComplete: false };
  }
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-12 11:20:43 +0000

Seen: 15 times

Last updated: Jul 12 '23