I have used below logic in CDK script for setting up Cloudwatch alarm
and send SNS notification
when there is failure in my glue job but when i tried to do npx synth
then it throws error : 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('trst@gmail.com'));
You are passing scope
(that was passed when the Stack was instantiated) instead of this
(the Stack itself) as the scope when creating your constructs. This is incorrect - when creating constructs in a stack, you should pass the stack itself (passing this
is the correct option in 99% of cases).
I'm guessing scope
is actually a CDK App.
Change scope
to this
and it should work.