我在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]'));