question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cloudwatch: Creating cloudwatch alarms on existing metrics

See original GitHub issue

Hey guys,

I don’t know if this is a feature request or a gap in the current documentation but I was wondering if it would be possible to create cloudwatch alarms on existing metrics. For example, when creating an ELB with CDK specific metrics come out of the box like: Response times. It would be great to be able to reference an already created cloudwatch metric like:

    const myExistingMetric = fn.metricErrors({
      namespace: "SomeNameSpace",
      metricName: "someMetricName"
    });

    new Alarm(this, 'CustomeAlarm', {
      metric: myExistingMetric,
      EvaluationPeriods: 2,
      threshold: 1,
      Statistic: 'Average',
      Period: 300, // five minutes
    });

I don’t exactly know great error handling mechanisms when those metrics don’t exist, but perhaps it would even expose what metrics are available out of the box.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

13reactions
zakwalterscommented, Nov 27, 2019

Is there any way to import existing metrics so that they can be used when creating alarms? I was thinking of something similar to Vpc.fromLookup.

As I understand it, the approach outlined in Doug’s link only works if you know the correct values needed to construct the metric (the namespace, metric name, and dimensions). We have a use case where a dimension of the metric is the ID of the container posting it, and so we cannot predict the dimensions from the CDK code.

It would be ideal if we could do something like:

import {Alarm, Metric, TreatMissingData} from "@aws-cdk/aws-cloudwatch";
import {App, Stack, StackProps} from "@aws-cdk/core";

export class AlarmStack extends Stack {
    constructor(scope: App, id: string, props?: StackProps) {
        super(scope, id, props);

        const containerErrorMetrics = Metric.importMetrics({ namespace: "MyContainers", metricName: "errors" });
        // containerErrorMetrics:
        // [
        //   Metric { namespace: "MyContainers", metricName: "errors", dimensions: { id: "1" } },
        //   Metric { namespace: "MyContainers", metricName: "errors", dimensions: { id: "2" } },
        // ]
        containerErrorMetrics.forEach(metric => {
            const props = {
                metric,
                threshold: 1,
                evaluationPeriods: 1,
                treatMissingData: TreatMissingData.NOT_BREACHING,
            };
            new Alarm(this, `ContainerErrors${metric.dimensions.id}Alarm`, props);
        });
    }
}

In the absence of such a method to import existing metrics, the best approach I can think of is to use ListMetrics from the CloudWatch SDK to get the metrics from the account. However, the SDK is async, so the call to ListMetrics (and the subsequent creation of alarms on these metrics) can’t be done in a constructor, as a constructor can’t be async. Instead, it has to be done in some sort of initalizer method. This feels like it breaks the normal CDK pattern of creating all child resources in the constructor.

Overall, my questions are:

  1. Is there ever going to be a way of importing existing metrics without knowing all the dimensions?
  2. What approach would you recommend in the meantime? Is it an anti-pattern to add an initialize method to our CDK classes?
4reactions
zakwalterscommented, Nov 29, 2019

We ended up deciding to create alarms with the CloudWatch SDK when the container is created rather than trying to keep these alarms in our CDK code. Answers to the questions above would still be interesting and potentially valuable others who end up on this thread though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set a CloudWatch alarm - AWS Documentation
Once you have a metric, either an existing one or one you defined, you can create an alarm. In this example, the alarm...
Read more >
typescript - Creating a Cloudwatch alarm on an existing ...
I need to set alarms for these already existing metrics in CDK. I cannot find how you can import pre-existing CloudWatch metrics in...
Read more >
Tutorial: Setting up AWS CloudWatch Alarms - Coralogix
You can create metric filters using data logged by AWS services such as Lambda. Once Lambda logs are in CloudWatch, you can create...
Read more >
Creating alarms in Amazon CloudWatch — Boto3 Docs 1.26 ...
An alarm watches a single metric over a time period you specify, and performs one or more actions based on the value of...
Read more >
CloudWatch Metric Math: Tutorial With Examples - OpsRamp
CloudWatch is a great tool to visualize the metrics as-is, but if you want to go to the next level of metrics aggregation,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found