To generate an Elasticsearch index via a CDK custom resource, you can use the following process:
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 };
}
}
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
Asked: 2023-07-12 11:20:43 +0000
Seen: 15 times
Last updated: Jul 12 '23
Is it feasible to utilize a Toggle Button to switch among multiple classes exceeding 2?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can I create a transition on click using CSS without the need for JavaScript?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?