Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I refer to the generated domain name of `elasticsearch.CfnDomain` in AWS CDK?

I created a CfnDomain in AWS CDK and I was trying to get the generated domain name to create an alarm.

const es = new elasticsearch.CfnDomain(this, id, esProps);

new cloudwatch.CfnAlarm(this, "test", {
  ...
  dimensions: [
    {
      name: "DomainName",
      value: es.domainName,
    },
  ],
});

But it seems that the domainName attribute is actually the argument that I pass in (I passed none so it will be autogenerated), so it's actually undefined and can't be used.

Is there any way that I can specify it such that it will wait for the elasticsearch cluster to be created so that I can obtain the generated domain name, or is there any other way to created an alarm for the metrics of the cluster?

like image 503
Jeff Happily Avatar asked Dec 05 '25 10:12

Jeff Happily


1 Answers

You use CfnDomain.ref as the domain value for your dimension. Sample alarm creation for red cluster status:

const domain: CfnDomain = ...;
const elasticDimension = {
    "DomainName": domain.ref,
};

const metricRed = new Metric({
    namespace: "AWS/ES",
    metricName: "ClusterStatus.red",
    statistic: "maximum",
    period: Duration.minutes(1),
    dimensions: elasticDimension
});

const redAlarm = metricRed.createAlarm(construct, "esRedAlarm", {
    alarmName: "esRedAlarm",
    evaluationPeriods: 1,
    threshold: 1
});
like image 185
Milan Gatyas Avatar answered Dec 07 '25 17:12

Milan Gatyas