Closed rtejwani1309 closed 7 months ago
Looks like this error is from:
I don't have the immediate fix off the top of my head. Making it a p1 bug.
Maybe I'm missing some step but I cannot reproduce this issue. I ran cdk synth
using the following example as you provided
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as cw from 'aws-cdk-lib/aws-cloudwatch'
import { Ec2Action, Ec2InstanceAction } from 'aws-cdk-lib/aws-cloudwatch-actions';
export class UnlinkedCdkAppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const metric = new cw.Metric({
namespace: 'CWAgent',
metricName: 'disk_used_percent',
dimensionsMap: {
InstanceId: "instance-id",
ImageId: "ami-id",
InstanceType: "t2.micro",
path: "/",
device: "xvda1",
fstype: "xfs"
},
period: cdk.Duration.minutes(5),
statistic: "Average"
})
const sev3Alarm = new cw.Alarm(this, `DISK_USED_PERCENT_SEV3`, {
alarmName: `DISK_USED_PERCENT_SEV3`,
actionsEnabled: true,
metric: metric,
evaluationPeriods: 1,
datapointsToAlarm: 1,
threshold: 75,
comparisonOperator: cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: cw.TreatMissingData.BREACHING,
});
sev3Alarm.addAlarmAction(new Ec2Action(Ec2InstanceAction.REBOOT));
}
}
cdk synth
correct synth the code into the following template
Resources:
DISKUSEDPERCENTSEV36CE6B660:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: true
AlarmActions:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":automate:"
- Ref: AWS::Region
- :ec2:reboot
AlarmName: DISK_USED_PERCENT_SEV3
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 1
Dimensions:
- Name: ImageId
Value: ami-id
- Name: InstanceId
Value: instance-id
- Name: InstanceType
Value: t2.micro
- Name: device
Value: xvda1
- Name: fstype
Value: xfs
- Name: path
Value: /
EvaluationPeriods: 1
MetricName: disk_used_percent
Namespace: CWAgent
Period: 300
Statistic: Average
Threshold: 75
TreatMissingData: breaching
Metadata:
aws:cdk:path: UnlinkedCdkAppStack/DISK_USED_PERCENT_SEV3/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/yXIPQ6DMAxA4bOwJ4ZkYEfcAA5QpU6qGoIt5acMiLu30Ol9ehaM7aFr3J41+lVHesIxF4er+q0HRql+dwXfcAzRpU2NL75xXppClpowXB6FPRUSPhWLD7Dk9mN6sB2YZslEOlUutAWY/v0C8ssPDnoAAAA=
Metadata:
aws:cdk:path: UnlinkedCdkAppStack/CDKMetadata/Default
Condition: CDKMetadataAvailable
Conditions:
CDKMetadataAvailable:
Fn::Or:
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- af-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-east-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-2
- Fn::Equals:
- Ref: AWS::Region
- ap-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-2
- Fn::Equals:
- Ref: AWS::Region
- ca-central-1
- Fn::Equals:
- Ref: AWS::Region
- cn-north-1
- Fn::Equals:
- Ref: AWS::Region
- cn-northwest-1
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- eu-central-1
- Fn::Equals:
- Ref: AWS::Region
- eu-north-1
- Fn::Equals:
- Ref: AWS::Region
- eu-south-1
- Fn::Equals:
- Ref: AWS::Region
- eu-west-1
- Fn::Equals:
- Ref: AWS::Region
- eu-west-2
- Fn::Equals:
- Ref: AWS::Region
- eu-west-3
- Fn::Equals:
- Ref: AWS::Region
- il-central-1
- Fn::Equals:
- Ref: AWS::Region
- me-central-1
- Fn::Equals:
- Ref: AWS::Region
- me-south-1
- Fn::Equals:
- Ref: AWS::Region
- sa-east-1
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- us-east-1
- Fn::Equals:
- Ref: AWS::Region
- us-east-2
- Fn::Equals:
- Ref: AWS::Region
- us-west-1
- Fn::Equals:
- Ref: AWS::Region
- us-west-2
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
CheckBootstrapVersion:
Assertions:
- Assert:
Fn::Not:
- Fn::Contains:
- - "1"
- "2"
- "3"
- "4"
- "5"
- Ref: BootstrapVersion
AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.
Hi @GavinZZ
I can reproduce it. Check out my full cdk app.
#!/usr/bin/env node
import 'source-map-support/register';
import { App, StackProps, Stack, CfnOutput, Duration,
aws_cloudwatch as cw,
aws_cloudwatch_actions as cwactions,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class UnlinkedCdkAppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const metric = new cw.Metric({
namespace: 'CWAgent',
metricName: 'disk_used_percent',
dimensionsMap: {
InstanceId: "instance-id",
ImageId: "ami-id",
InstanceType: "t2.micro",
path: "/",
device: "xvda1",
fstype: "xfs"
},
period: Duration.minutes(5),
statistic: "Average"
})
const sev3Alarm = new cw.Alarm(this, `DISK_USED_PERCENT_SEV3`, {
alarmName: `DISK_USED_PERCENT_SEV3`,
actionsEnabled: true,
metric: metric,
evaluationPeriods: 1,
datapointsToAlarm: 1,
threshold: 75,
comparisonOperator: cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: cw.TreatMissingData.BREACHING,
});
sev3Alarm.addAlarmAction(new cwactions.Ec2Action(cwactions.Ec2InstanceAction.REBOOT));
}
}
const app = new App();
const env = { region: process.env.CDK_DEFAULT_REGION, account: process.env.CDK_DEFAULT_ACCOUNT };
new UnlinkedCdkAppStack(app, 'UnlinkedCdkAppStack', { env });
cdk synth
returns this error:
Error: EC2 alarm actions requires an EC2 Per-Instance Metric. ({"metricStat":{"dimensions":[{"name":"ImageId","value":"ami-id"},{"name":"InstanceId","value":"instance-id"},{"name":"InstanceType","value":"t2.micro"},{"name":"device","value":"xvda1"},{"name":"fstype","value":"xfs"},{"name":"path","value":"/"}],"namespace":"CWAgent","metricName":"disk_used_percent","period":{"amount":5,"unit":{"label":"minutes","isoLabel":"M","inMillis":60000}},"statistic":"Average"},"renderingProperties":{}} does not have an 'InstanceId' dimension)
at Alarm.validateActionArn (/Users/hunhsieh/repos/xxx/node_modules/aws-cdk-lib/aws-cloudwatch/lib/alarm.js:1:5300)
at /Users/hunhsieh/repos/xxx/node_modules/aws-cdk-lib/aws-cloudwatch/lib/alarm.js:1:4982
at Array.map (<anonymous>)
at Alarm.addAlarmAction (/Users/hunhsieh/repos/xxx/node_modules/aws-cdk-lib/aws-cloudwatch/lib/alarm.js:1:4970)
at new UnlinkedCdkAppStack (/Users/hunhsieh/repos/xxx/bin/xxx.ts:41:15)
at Object.<anonymous> (/Users/hunhsieh/repos/xxx/bin/xxx.ts:47:1)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module.m._compile (/Users/hunhsieh/repos/xxx/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Object.require.extensions.<computed> [as .ts] (/Users/hunhsieh/repos/xxx/node_modules/ts-node/src/index.ts:1621:12)
Subprocess exited with error 1
% npx cdk --version 2.131.0 (build 92b912d)
Thanks Pahud, I can confirm that this is reproducible. Will investigate this issue.
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
Describe the bug
While trying to create a Custom Metric with multiple dimension, and adding EC2 action, the CDK synth fails with the error below.
The code for generating the error is as below
To resolve the issue, there are 2 ways of doing it, the first one is we comment out the other dimensions apart from instance id in the dimensions map as below.
Or we use escape hatch as below.
Expected Behavior
The expected behaviour is that it should allow to add EC2 action for the custom metrics too, as one can do using CloudWatch Console, and CloudFormation.
Current Behavior
The current behaviour is that it results into the error stated below.
Reproduction Steps
Use the below code, and run cdk synth
https://github.com/aws/aws-cdk/blob/9487b39d6da4ab7efd884c3a8379b03e317786b9/packages/%40aws-cdk/aws-cloudwatch/lib/alarm.ts#L249