Giter Site home page Giter Site logo

Comments (4)

pahud avatar pahud commented on June 12, 2024

Looks like this error is from:

private validateActionArn(actionArn: string): string {
const ec2ActionsRegexp: RegExp = /arn:aws[a-z0-9-]*:automate:[a-z|\d|-]+:ec2:[a-z]+/;
if (ec2ActionsRegexp.test(actionArn)) {
// Check per-instance metric
const metricConfig = this.metric.toMetricConfig();
if (metricConfig.metricStat?.dimensions?.length != 1 || metricConfig.metricStat?.dimensions![0].name != 'InstanceId') {
throw new Error(`EC2 alarm actions requires an EC2 Per-Instance Metric. (${JSON.stringify(metricConfig)} does not have an 'InstanceId' dimension)`);
}
}
return actionArn;
}

I don't have the immediate fix off the top of my head. Making it a p1 bug.

from aws-cdk.

GavinZZ avatar GavinZZ commented on June 12, 2024

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.

from aws-cdk.

pahud avatar pahud commented on June 12, 2024

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)

from aws-cdk.

GavinZZ avatar GavinZZ commented on June 12, 2024

Thanks Pahud, I can confirm that this is reproducible. Will investigate this issue.

from aws-cdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.