-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdkv2.ts
62 lines (53 loc) · 2.33 KB
/
cdkv2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as elasticsearch from 'aws-cdk-lib/aws-elasticsearch';
import * as cdk from 'aws-cdk-lib';
import * as customResource from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import * as path from 'path';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ElasticsearchClusterSettings = Record<string, any>;
export interface ElasticsearchSettingsProps {
readonly esDomain: elasticsearch.CfnDomain;
readonly clusterSettings: ElasticsearchClusterSettings;
}
class ElasticsearchSettingsProvider extends Construct {
public readonly provider: customResource.Provider;
public static getOrCreate(scope: Construct): customResource.Provider {
const stack = cdk.Stack.of(scope);
const id = 'com.isotoma.cdk.custom-resources.es-settings';
const x = (stack.node.tryFindChild(id) as ElasticsearchSettingsProvider) || new ElasticsearchSettingsProvider(stack, id);
return x.provider;
}
constructor(scope: Construct, id: string) {
super(scope, id);
this.provider = new customResource.Provider(this, 'es-settings-provider', {
onEventHandler: new lambda.Function(this, 'es-settings-event', {
code: lambda.Code.fromAsset(path.join(__dirname, 'provider')),
runtime: lambda.Runtime.NODEJS_16_X,
handler: 'index.onEvent',
timeout: cdk.Duration.minutes(5),
initialPolicy: [
new iam.PolicyStatement({
resources: ['*'],
actions: ['es:ESHttp*'],
}),
],
}),
});
}
}
export class ElasticsearchSettings extends Construct {
constructor(scope: Construct, id: string, props: ElasticsearchSettingsProps) {
super(scope, id);
const provider = ElasticsearchSettingsProvider.getOrCreate(this);
new cdk.CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: 'Custom::ElasticsearchSettings',
properties: {
EsDomainEndpoint: props.esDomain.attrDomainEndpoint,
ClusterSettingsJson: JSON.stringify(props.clusterSettings),
},
});
}
}