AWS警报未从度量中获得价值

发布于 2025-01-21 18:45:16 字数 1879 浏览 1 评论 0原文

我需要使用cpuutilization设置CloudWatch警报用于集群和服务维度的度量标准类型。为了设置这些资源,我正在使用CDK库,在部署CDKSTACK之后,情况看起来不错。 ECS集群中都可以使用警报和度量。

以下是我使用的代码。 现在,当我使用压力实用程序增加CPU负载时,此CDK代码不起作用,但是如果我使用AWS GUI手动创建警报,则情况正常。

有人可以帮我吗?

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

    const vpc = new ec2.Vpc(this, "AlarmVpc3", {
       maxAzs: 3
     });

    const cluster = new ecs.Cluster(this, "AlarmClusterDemo3", {
       vpc: vpc,
       containerInsights: true,
       clusterName: "DemoCluster"
    });

    cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
      instanceType: new ec2.InstanceType("t2.micro"),
    });

    const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDefDemo3', {
      // memoryLimitMiB: 512,
      // cpu: 256 
    });

    taskDefinition.addContainer('MyContainer', {
      image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
      memoryLimitMiB: 512,
    });

    // Instantiate an Amazon ECS Service
    const ecsService = new ecs.FargateService(this, 'ServiceDemo3', {
      serviceName: "DemoService",
      cluster,
      taskDefinition,
      enableExecuteCommand: true,
      desiredCount: 1
    });

    const cpuMetric = new cloudwatch.Metric({
      namespace: 'AWS/ECS ',
      metricName: 'CPUUtilization', //ECS/ContainerInsights
      period: Duration.minutes(1),
      dimensionsMap: {
        ServiceName: "AlarmCdk3-ServiceDemo3Service80A0F131-LofBYzFzUatv",
        ClusterName: "AlarmCdk3-AlarmClusterDemo3EF161BDA-jm7guu27s8bV"
      }
    });
    
    const cpuAlarm = new cloudwatch.Alarm(taskDefinition, 'CPUAlarmDemo', {
      alarmName: "DemoAlarm",
      metric: cpuMetric,
      threshold: 5,
      evaluationPeriods: 1,
      datapointsToAlarm: 1,
    });
    
  } 
}

I havea requirement to setup a CloudWatch alarm with CPUUtilizationmetric type for cluster and service dimensions. To setup these resources I am using CDK library and after deploy CDKstack things looks fine. Both alarm and metric are available in ECS cluster.

Following is piece of code that I am using for same.
Now when I increase CPU load using stress utility then this CDK code doesn't work but if I create alarm manually using AWS GUI then things work fine.

Can someone help me on this ?

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

    const vpc = new ec2.Vpc(this, "AlarmVpc3", {
       maxAzs: 3
     });

    const cluster = new ecs.Cluster(this, "AlarmClusterDemo3", {
       vpc: vpc,
       containerInsights: true,
       clusterName: "DemoCluster"
    });

    cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
      instanceType: new ec2.InstanceType("t2.micro"),
    });

    const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDefDemo3', {
      // memoryLimitMiB: 512,
      // cpu: 256 
    });

    taskDefinition.addContainer('MyContainer', {
      image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
      memoryLimitMiB: 512,
    });

    // Instantiate an Amazon ECS Service
    const ecsService = new ecs.FargateService(this, 'ServiceDemo3', {
      serviceName: "DemoService",
      cluster,
      taskDefinition,
      enableExecuteCommand: true,
      desiredCount: 1
    });

    const cpuMetric = new cloudwatch.Metric({
      namespace: 'AWS/ECS ',
      metricName: 'CPUUtilization', //ECS/ContainerInsights
      period: Duration.minutes(1),
      dimensionsMap: {
        ServiceName: "AlarmCdk3-ServiceDemo3Service80A0F131-LofBYzFzUatv",
        ClusterName: "AlarmCdk3-AlarmClusterDemo3EF161BDA-jm7guu27s8bV"
      }
    });
    
    const cpuAlarm = new cloudwatch.Alarm(taskDefinition, 'CPUAlarmDemo', {
      alarmName: "DemoAlarm",
      metric: cpuMetric,
      threshold: 5,
      evaluationPeriods: 1,
      datapointsToAlarm: 1,
    });
    
  } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2025-01-28 18:45:16

使用服务露出

const cpumetric = ecsservice.metriccpuutilization({strie:lisation.minutes(1)});>

Use the Metric objects that the service exposes:

const cpuMetric = ecsService.metricCpuUtilization({period: Duration.minutes(1)});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文