How to reference a CloudWatch metric created by container insights for ECS/Fargate

1.8k views Asked by At

I've created an ECS cluster like so:

    this.cluster = new ecs.Cluster(this, 'Cluster', {
        containerInsights: true,
        vpc: ec2.Vpc.fromLookup(this, props.stage + 'Vpc', {isDefault: false})
    });

I want to create a CW alarm based off my cluser like so:

    const CPUHigh = new cw.Alarm(this, "CPUHigh", {
        metric: this.cluster.metric("CPUUtilized"),
        threshold: 50,
        evaluationPeriods: 3,
        period: cdk.Duration.seconds(60),
        comparisonOperator: cw.ComparisonOperator.GREATER_THAN_THRESHOLD
    })

But even though the metric matches the one created by Container Insights, it seems like it can't be referenced this way.

Does anyone know the way its supposed to be referenced?

1

There are 1 answers

2
Jonny Rimek On BEST ANSWER

CDK only has support for certain metrics baseline and container insight is not covered, but it's not a problem you can create your own metric object pretty easy. For container insights it looks like this:

new cloudwatch.Metric({
  metricName: 'NetworkTxBytes',
  namespace: 'ECS/ContainerInsights',
  dimensionsMap: {
    ServiceName: props.ecsService.serviceName,
    ClusterName: props.ecsCluster.clusterName,
  },
  statistic: 'avg',
  period: cdk.Duration.minutes(5),
}),

Important here is the namespace, dimensionsMap, and the metricName.

You can get the information about the namespace and the dimensions from the metrics console and the last tab "source".