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

问题描述 投票:0回答:1

我在 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]'));
amazon-web-services amazon-cloudwatch aws-glue aws-cdk amazon-sns
1个回答
0
投票

看起来您位于 Stack 的构造函数内,并且正在传递

scope
(在实例化 Stack 时传递)而不是
this
(Stack 本身)作为范围。

我猜

scope
实际上是一个CDK应用程序。

scope
更改为
this
就可以了。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.