-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added canary url and stepfunction monitoring with sns email and slack…
… integration Signed-off-by: Brandon Shien <bshien@amazon.com>
- Loading branch information
Showing
21 changed files
with
1,072 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
infrastructure/canary/nodejs/node_modules/urlMonitor.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {SnsMonitors} from "./snsMonitor"; | ||
import {SnsMonitorsProps} from "./snsMonitor"; | ||
import {Construct} from "constructs"; | ||
import {Alarm} from "aws-cdk-lib/aws-cloudwatch"; | ||
import { Canary } from 'aws-cdk-lib/aws-synthetics'; | ||
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch"; | ||
|
||
interface canarySnsProps extends SnsMonitorsProps { | ||
readonly canaryAlarms: Array<{ alertName: string, canary: Canary }>; | ||
} | ||
|
||
export class canarySns extends SnsMonitors { | ||
private readonly canaryAlarms: Array<{ alertName: string, canary: Canary }>; | ||
constructor(scope: Construct, id: string, props: canarySnsProps) { | ||
super(scope, id, props); | ||
this.canaryAlarms = props.canaryAlarms; | ||
this.canaryAlarms.forEach(({ alertName, canary }) => | ||
{ | ||
const alarm = this.canaryFailed(alertName, canary); | ||
this.map[alarm[1]] = alarm[0]; | ||
}); | ||
this.createTopic(); | ||
} | ||
|
||
private canaryFailed(alertName: string, canary: Canary): [Alarm, string] { | ||
const alarmObject = new cloudwatch.Alarm(this, `error_alarm_${alertName}`, { | ||
metric: canary.metricSuccessPercent(), | ||
threshold: 100, | ||
evaluationPeriods: 1, | ||
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD, | ||
datapointsToAlarm: 1, | ||
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING, | ||
alarmDescription: "Detect Canary failure", | ||
alarmName: alertName, | ||
}); | ||
return [alarmObject, alertName]; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Construct } from 'constructs'; | ||
import * as sns from "aws-cdk-lib/aws-sns"; | ||
import * as subscriptions from "aws-cdk-lib/aws-sns-subscriptions"; | ||
import * as actions from "aws-cdk-lib/aws-cloudwatch-actions"; | ||
import { Canary } from 'aws-cdk-lib/aws-synthetics'; | ||
import {OpenSearchLambda} from "./lambda"; | ||
|
||
export interface SnsMonitorsProps { | ||
readonly region: string; | ||
readonly accountId: string; | ||
readonly alarmNameSpace: string; | ||
readonly snsTopicName: string; | ||
readonly slackLambda: OpenSearchLambda; | ||
} | ||
|
||
export class SnsMonitors extends Construct { | ||
protected readonly region: string; | ||
protected readonly accountId: string; | ||
protected readonly alarmNameSpace: string; | ||
protected readonly map: { [id: string]: any }; | ||
private readonly snsTopicName: string; | ||
private readonly slackLambda: OpenSearchLambda; | ||
private readonly emailList: Array<string>; | ||
|
||
|
||
constructor(scope: Construct, id: string, props: SnsMonitorsProps) { | ||
super(scope, id); | ||
this.region = props.region; | ||
this.accountId = props.accountId; | ||
this.alarmNameSpace = props.alarmNameSpace; | ||
this.snsTopicName = props.snsTopicName; | ||
this.slackLambda = props.slackLambda; | ||
|
||
// The email list for receiving alerts | ||
this.emailList = [ | ||
'insert@mail.here' | ||
]; | ||
|
||
// Create alarms | ||
this.map = {}; | ||
|
||
} | ||
|
||
protected createTopic(){ | ||
// Create SNS topic for alarms to be sent to | ||
const snsTopic = new sns.Topic(this, `OpenSearchMetrics-Alarm-${this.snsTopicName}`, { | ||
displayName: `OpenSearchMetrics-Alarm-${this.snsTopicName}` | ||
}); | ||
|
||
// Iterate map to create SNS topic and add alarms on it | ||
Object.keys(this.map).map(key => { | ||
// Connect the alarm to the SNS | ||
this.map[key].addAlarmAction(new actions.SnsAction(snsTopic)); | ||
}) | ||
|
||
// Send email notification to the recipients | ||
for (const email of this.emailList) { | ||
snsTopic.addSubscription(new subscriptions.EmailSubscription(email)); | ||
} | ||
|
||
// Send slack notification | ||
snsTopic.addSubscription(new subscriptions.LambdaSubscription(this.slackLambda.lambda)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {SnsMonitors} from "./snsMonitor"; | ||
import {SnsMonitorsProps} from "./snsMonitor"; | ||
import {Construct} from "constructs"; | ||
import {Alarm} from "aws-cdk-lib/aws-cloudwatch"; | ||
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch"; | ||
|
||
interface stepFunctionSnsProps extends SnsMonitorsProps { | ||
readonly stepFunctionSnsAlarms: Array<{ alertName: string, stateMachineName: string }>; | ||
} | ||
|
||
export class StepFunctionSns extends SnsMonitors { | ||
private readonly stepFunctionSnsAlarms: Array<{ alertName: string, stateMachineName: string }>; | ||
constructor(scope: Construct, id: string, props: stepFunctionSnsProps) { | ||
super(scope, id, props); | ||
this.stepFunctionSnsAlarms = props.stepFunctionSnsAlarms; | ||
this.stepFunctionSnsAlarms.forEach(({ alertName, stateMachineName }) => | ||
{ | ||
const alarm = this.stepFunctionExecutionsFailed(alertName, stateMachineName); | ||
this.map[alarm[1]] = alarm[0]; | ||
}); | ||
this.createTopic(); | ||
} | ||
|
||
private stepFunctionExecutionsFailed(alertName: string, stateMachineName: string): [Alarm, string] { | ||
const alarmObject = new cloudwatch.Alarm(this, `error_alarm_${alertName}`, { | ||
metric: new cloudwatch.Metric({ | ||
namespace: this.alarmNameSpace, | ||
metricName: "ExecutionsFailed", | ||
statistic: "Sum", | ||
dimensionsMap: { | ||
StateMachineArn: `arn:aws:states:${this.region}:${this.accountId}:stateMachine:${stateMachineName}` | ||
} | ||
}), | ||
threshold: 1, | ||
evaluationPeriods: 1, | ||
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD, | ||
datapointsToAlarm: 1, | ||
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING, | ||
alarmDescription: "Detect SF execution failure", | ||
alarmName: alertName, | ||
}); | ||
return [alarmObject, alertName]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.