AWS警报未从度量中获得价值
我需要使用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 CPUUtilization
metric 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用服务露出:
const cpumetric = ecsservice.metriccpuutilization({strie:lisation.minutes(1)});
>Use the Metric objects that the service exposes:
const cpuMetric = ecsService.metricCpuUtilization({period: Duration.minutes(1)});