Blake
Blake

Reputation: 374

Using the AWS CDK to Create an Alarm on a Database

I am trying to create an alarm using CDK to check the free space on my DB. Without specifying the Database name I can create the alarm. However, it doesnt really do anything since there is no DB to monitor. Does anyone know how I can specify this name? Here is what I have tried using the dimensions:

 new cloudwatch.Alarm(this, 'FreeStorageSpace', {
   metric: new cloudwatch.Metric({
      "metricName": "FreeStorageSpace",
      "namespace": "AWS/RDS",
      "period": Duration.minutes(1),
      "unit": cloudwatch.Unit.COUNT,
      "statistic": cloudwatch.statistics.SUM,
      "dimensions": {
          "Name": "DBInstanceIdentifier",
          "Value": "rdsalarm-development"
       },
   }),
   threshold: 90,
   evaluationPeriods: 1,

 })

When I try to build this I get: Error:Resolution error: Supplied properties not correct for "CfnAlarmProps"

dimensions: element 0: supplied properties not correct for "DimensionsProperty"

value: {"Name":"DBInstanceIdentifier", "Value":"rdsalarm-development"} should be a string

Upvotes: 1

Views: 2846

Answers (1)

gshpychka
gshpychka

Reputation: 11579

You syntax is not correct. dimensions should use the dimension name as the key. The way you wrote it would make it impossible to specify multiple dimensions.

 new cloudwatch.Alarm(this, 'FreeStorageSpace', {
   metric: new cloudwatch.Metric({
      metricName: "FreeStorageSpace",
      namespace: "AWS/RDS",
      period: Duration.minutes(1),
      unit: cloudwatch.Unit.COUNT,
      statistic: cloudwatch.statistics.SUM,
      dimensions: {
        DBInstanceIdentifier: "rdsalarm-development"
      },
   }),
   threshold: 90,
   evaluationPeriods: 1,

 })

The CDK way would be to use the helper methods available on the rds L2 constructs:

 const myDbInstance: DatabaseInstance;
 new cloudwatch.Alarm(this, 'FreeStorageSpace', {
   metric: myDbInstance.metricFreeStorageSpace(),
   threshold: 90,
   evaluationPeriods: 1,
 });

Another option:

myDbInstance.metricFreeStorageSpace().createAlarm(this, 'FreeStorageSpace',
    {
        threshold: 90,
        evaluationPeriods: 1,
    }
);

Upvotes: 2

Related Questions