Home > other >  CDK v2 - access metric from CloudFormation Queue (CfnQueue)
CDK v2 - access metric from CloudFormation Queue (CfnQueue)

Time:07-22

I am using the cdk v2.33.0 for Typescript and I am trying to get the NumberOfMessageSent metric from a Cloud Formation Queue I created using the CfnQueue class. The end goal being to use this metric for an alarm I want to create.

While I am able to do myStandardQueue.metricNumberOfMessagesSent() with a Queue object, it seems CfnQueue doesn't have this method.

So my question is, is there another way to do this with CfnQueue objects?

To illustrate:

import { CfnQueue, Queue } from 'aws-cdk-lib/aws-sqs'


// THIS PART IS OK
const myStandardQueue = new Queue(..some parameters...);

const myStandardQueueAlarm = new Alarm(myScope, 'someIdForMyAlarm', {
      alarmName: `myStandardQueueAlarm`,
      comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
      evaluationPeriods: 1,
      threshold: 1,
      // this works because myStandardQueue has the metricNumberOfMessagesSent method -->
      metric: myStandardQueue.metricNumberOfMessagesSent({
        period: Duration.seconds(60),
        statistic: 'Sum',
      })
    });


// THIS PART IS NOT OK
const myCfnQueue = new CfnQueue(...some parameters...);

const myCfnQueueAlarm = new Alarm(myScope, 'someOtherIdForMyOtherAlarm', {
      alarmName: `myCfnQueueAlarm`,
      comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
      evaluationPeriods: 1,
      threshold: 1,
      // this does not works because myCfnQueue does not have 
      // a metricNumberOfMessagesSent method -->
      metric: myCfnQueue.metricNumberOfMessagesSent({
        period: Duration.seconds(60),
        statistic: 'Sum',
      })
    });

Thanks :)

CodePudding user response:

You can construct it manually. For metricNumberOfMessagesSent, the equivalent Metric would be:

const myMetric = new Metric({
    dimensions: { QueueName: myCfnQueue.attrQueueName },
    namespace: 'AWS/SQS',
    metricName: 'NumberOfMessagesSent',
    period: Duration.minutes(5),
    statistic: 'Sum',
  });

You can do this for any metrics exposed by SQS.

The metric methods available in L2 are generated from spec files - here's the one for SQS: https://github.com/aws/aws-cdk/blob/main/packages/@aws-cdk/cfnspec/lib/augmentations/AWS_SQS_Queue.json

  • Related