-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #560 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
- Loading branch information
1 parent
5b94644
commit 1f8ca08
Showing
5 changed files
with
566 additions
and
1 deletion.
There are no files selected for viewing
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,28 @@ | ||
import { LambdaAction } from "aws-cdk-lib/aws-cloudwatch-actions"; | ||
import { IAlias, IFunction, IVersion } from "aws-cdk-lib/aws-lambda"; | ||
|
||
import { | ||
AlarmActionStrategyProps, | ||
IAlarmActionStrategy, | ||
} from "./IAlarmActionStrategy"; | ||
|
||
export function triggerLambda( | ||
lambdaFunction: IAlias | IVersion | IFunction, | ||
): IAlarmActionStrategy { | ||
return new LambdaAlarmActionStrategy(lambdaFunction); | ||
} | ||
|
||
/** | ||
* Alarm action strategy that triggers a Lambda function. | ||
*/ | ||
export class LambdaAlarmActionStrategy implements IAlarmActionStrategy { | ||
protected readonly lambdaFunction: IAlias | IVersion | IFunction; | ||
|
||
constructor(lambdaFunction: IAlias | IVersion | IFunction) { | ||
this.lambdaFunction = lambdaFunction; | ||
} | ||
|
||
addAlarmActions(props: AlarmActionStrategyProps): void { | ||
props.alarm.addAlarmAction(new LambdaAction(this.lambdaFunction)); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
test/common/alarm/action/LambdaAlarmActionStrategy.test.ts
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,65 @@ | ||
import { Stack } from "aws-cdk-lib"; | ||
import { Template } from "aws-cdk-lib/assertions"; | ||
import { Alarm, Metric } from "aws-cdk-lib/aws-cloudwatch"; | ||
import { Function, InlineCode, Runtime } from "aws-cdk-lib/aws-lambda"; | ||
|
||
import { LambdaAlarmActionStrategy } from "../../../../lib"; | ||
|
||
test("snapshot test: Lambda function", () => { | ||
const stack = new Stack(); | ||
const onAlarmFunction = new Function(stack, "alarmLambda", { | ||
functionName: "DummyLambda", | ||
runtime: Runtime.NODEJS_18_X, | ||
code: InlineCode.fromInline("{}"), | ||
handler: "Dummy::handler", | ||
}); | ||
const alarm = new Alarm(stack, "DummyAlarm", { | ||
evaluationPeriods: 1, | ||
threshold: 0, | ||
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }), | ||
}); | ||
const action = new LambdaAlarmActionStrategy(onAlarmFunction); | ||
action.addAlarmActions({ alarm, action }); | ||
|
||
expect(Template.fromStack(stack)).toMatchSnapshot(); | ||
}); | ||
|
||
test("snapshot test: Lambda alias", () => { | ||
const stack = new Stack(); | ||
const onAlarmFunction = new Function(stack, "alarmLambda", { | ||
functionName: "DummyLambda", | ||
runtime: Runtime.NODEJS_18_X, | ||
code: InlineCode.fromInline("{}"), | ||
handler: "Dummy::handler", | ||
}); | ||
const alias = onAlarmFunction.addAlias("aliasName"); | ||
const alarm = new Alarm(stack, "DummyAlarm", { | ||
evaluationPeriods: 1, | ||
threshold: 0, | ||
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }), | ||
}); | ||
const action = new LambdaAlarmActionStrategy(alias); | ||
action.addAlarmActions({ alarm, action }); | ||
|
||
expect(Template.fromStack(stack)).toMatchSnapshot(); | ||
}); | ||
|
||
test("snapshot test: Lambda version", () => { | ||
const stack = new Stack(); | ||
const onAlarmFunction = new Function(stack, "alarmLambda", { | ||
functionName: "DummyLambda", | ||
runtime: Runtime.NODEJS_18_X, | ||
code: InlineCode.fromInline("{}"), | ||
handler: "Dummy::handler", | ||
}); | ||
const version = onAlarmFunction.currentVersion; | ||
const alarm = new Alarm(stack, "DummyAlarm", { | ||
evaluationPeriods: 1, | ||
threshold: 0, | ||
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }), | ||
}); | ||
const action = new LambdaAlarmActionStrategy(version); | ||
action.addAlarmActions({ alarm, action }); | ||
|
||
expect(Template.fromStack(stack)).toMatchSnapshot(); | ||
}); |
Oops, something went wrong.