如何引用容器见解为 ECS/Fargate 创建的 CloudWatch 指标

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

我已经像这样创建了一个 ECS 集群:

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

我想像这样基于我的集群创建一个 CW 警报:

    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
    })

但即使该指标与 Container Insights 创建的指标相匹配,它似乎也不能以这种方式引用。

有谁知道应该如何引用它?

CDK 仅支持某些指标基线,未涵盖容器洞察力,但这不是问题,您可以很容易地创建自己的指标对象。对于容器洞察,它看起来像这样:

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

此处重要的是命名空间、dimensionsMap 和 metricName。

您可以从指标控制台和最后一个选项卡“源”中获取有关命名空间和维度的信息。