Scooby
Scooby

Reputation: 655

AWS Cloudwatch list-metrics in CDK

Is there is a way to automatically get all the dimensions of a metrics from CW in AWS CDK code? There is a cli command (aws cloudwatch list-metrics) and SDK API (list-metrics) too.. I am unable to find equivalent call in CDK.

My use case is, I have a metric under a namespace which records metrics for multiple tables and tablename is passed as a dimension to the metric. I am looking to iterate over all the dimensions so that I can create alarms for all the table names in the dimension. Also, when new tables are added we do not have to update anything in the code. Currently, I am maintaining the list of dimensions in the hard-coded constants file.

Upvotes: 0

Views: 508

Answers (1)

fedonev
fedonev

Reputation: 25639

It's possible to add SDK calls like list-metrics directly in your CDK code. The SDK command would be called on each synth your app*. The CDK does not recommend this, however. It advises against comingling SDK calls with your CDK application code:

If you need some value (from AWS or elsewhere) for which there is no native CDK context provider, we recommend writing a separate script. The script should retrieve the value and write it to a file, then read that file in your CDK app. Run the script only when you want to refresh the stored value, not as part of your regular build process.

The benefit of this added complexisy is that you can put the SDK response under source control and get deterministic deploys:

There are no out-of-band changes to your application, and any given commit always yields the exact same AWS CloudFormation template and accompanying assets. This makes unit testing much more reliable.


* The CDK has several built-in context methods like Vpc.fromLookup that make an initial SDK call and subsequently read cached results from cdk.context.json.

Upvotes: 0

Related Questions