代码之家  ›  专栏  ›  技术社区  ›  Andrew

CloudWatch错误:规则JobFailureRule应在堆栈范围内创建,但找不到堆栈

  •  0
  • Andrew  · 技术社区  · 9 月前

    我在CDK脚本中使用了以下逻辑进行设置 Cloudwatch alarm 并发送 SNS notification 当我的胶水工作失败了,但当我试着做的时候 npx synth 则抛出错误: Error: Rule at '{DVRD replication} JobFailureRule' should be created in the scope of a Stack, but no Stack found .

    export interface InfraStackProps extends VpcStackProps {
      stageName?: string;
    }
    
    export class InfraStack extends VpcStack {
      constructor(scope: Construct, id: string, props: InfraStackProps ) {
        super(scope, id, props);
        // jobName. This is our AWS Glue script to monitor
        const jobFailedRule = new Rule(scope, '{DVRD replication} JobFailureRule', {
          eventPattern: {
            source: ['aws.glue'],
            detailType: ['Glue Job State Change'],
            detail: {
              state: ['FAILED', 'TIMEOUT', 'ERROR'],
              jobName: ['DVRD replication'],
            },
          },
        })
        
        //cloudwatch metric
        const numFailedJobsMetric = new cloudwatch.Metric({
          namespace: 'AWS/Events',
          metricName: 'TriggeredRules',
          statistic: 'Sum',
          dimensionsMap: {
            RuleName: jobFailedRule.ruleName,
          },
        })
        
        //cloudwatch alarm
        const numFailedJobsAlarm = new cloudwatch.Alarm(scope, '{DVRD replication} numFailedJobsAlarm', {
          metric: numFailedJobsMetric,
          threshold: 1,
          evaluationPeriods: 1,
        });
        
        
        // Adding an SNS action and an email registration 
        const notificationsTopic = new Topic(this, 'Topic'); 
        numFailedJobsAlarm.addAlarmAction(new SnsAction(notificationsTopic));
        notificationsTopic.addSubscription(new EmailSubscription('[email protected]'));
    
    1 回复  |  直到 9 月前
        1
  •  2
  •   gshpychka    9 月前

    你正在通过 scope (在实例化Stack时传递)而不是 this (堆栈本身)作为创建构造时的作用域。这是不正确的——在堆栈中创建构造时,应该传递堆栈本身(传递 在99%的情况下是正确的选择)。

    我猜 范围 实际上是一个CDK应用程序。

    更改 范围 它应该奏效。