Skip to content

Commit 1f8ca08

Browse files
author
Eugene Cheung
authored
feat: support Lambda actions (#597)
Closes #560 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 5b94644 commit 1f8ca08

File tree

5 files changed

+566
-1
lines changed

5 files changed

+566
-1
lines changed

API.md

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { LambdaAction } from "aws-cdk-lib/aws-cloudwatch-actions";
2+
import { IAlias, IFunction, IVersion } from "aws-cdk-lib/aws-lambda";
3+
4+
import {
5+
AlarmActionStrategyProps,
6+
IAlarmActionStrategy,
7+
} from "./IAlarmActionStrategy";
8+
9+
export function triggerLambda(
10+
lambdaFunction: IAlias | IVersion | IFunction,
11+
): IAlarmActionStrategy {
12+
return new LambdaAlarmActionStrategy(lambdaFunction);
13+
}
14+
15+
/**
16+
* Alarm action strategy that triggers a Lambda function.
17+
*/
18+
export class LambdaAlarmActionStrategy implements IAlarmActionStrategy {
19+
protected readonly lambdaFunction: IAlias | IVersion | IFunction;
20+
21+
constructor(lambdaFunction: IAlias | IVersion | IFunction) {
22+
this.lambdaFunction = lambdaFunction;
23+
}
24+
25+
addAlarmActions(props: AlarmActionStrategyProps): void {
26+
props.alarm.addAlarmAction(new LambdaAction(this.lambdaFunction));
27+
}
28+
}

lib/common/alarm/action/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./IAlarmActionStrategy";
2+
export * from "./LambdaAlarmActionStrategy";
23
export * from "./MultipleAlarmActionStrategy";
34
export * from "./NoopAlarmActionStrategy";
45
export * from "./OpsItemAlarmActionStrategy";
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Stack } from "aws-cdk-lib";
2+
import { Template } from "aws-cdk-lib/assertions";
3+
import { Alarm, Metric } from "aws-cdk-lib/aws-cloudwatch";
4+
import { Function, InlineCode, Runtime } from "aws-cdk-lib/aws-lambda";
5+
6+
import { LambdaAlarmActionStrategy } from "../../../../lib";
7+
8+
test("snapshot test: Lambda function", () => {
9+
const stack = new Stack();
10+
const onAlarmFunction = new Function(stack, "alarmLambda", {
11+
functionName: "DummyLambda",
12+
runtime: Runtime.NODEJS_18_X,
13+
code: InlineCode.fromInline("{}"),
14+
handler: "Dummy::handler",
15+
});
16+
const alarm = new Alarm(stack, "DummyAlarm", {
17+
evaluationPeriods: 1,
18+
threshold: 0,
19+
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }),
20+
});
21+
const action = new LambdaAlarmActionStrategy(onAlarmFunction);
22+
action.addAlarmActions({ alarm, action });
23+
24+
expect(Template.fromStack(stack)).toMatchSnapshot();
25+
});
26+
27+
test("snapshot test: Lambda alias", () => {
28+
const stack = new Stack();
29+
const onAlarmFunction = new Function(stack, "alarmLambda", {
30+
functionName: "DummyLambda",
31+
runtime: Runtime.NODEJS_18_X,
32+
code: InlineCode.fromInline("{}"),
33+
handler: "Dummy::handler",
34+
});
35+
const alias = onAlarmFunction.addAlias("aliasName");
36+
const alarm = new Alarm(stack, "DummyAlarm", {
37+
evaluationPeriods: 1,
38+
threshold: 0,
39+
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }),
40+
});
41+
const action = new LambdaAlarmActionStrategy(alias);
42+
action.addAlarmActions({ alarm, action });
43+
44+
expect(Template.fromStack(stack)).toMatchSnapshot();
45+
});
46+
47+
test("snapshot test: Lambda version", () => {
48+
const stack = new Stack();
49+
const onAlarmFunction = new Function(stack, "alarmLambda", {
50+
functionName: "DummyLambda",
51+
runtime: Runtime.NODEJS_18_X,
52+
code: InlineCode.fromInline("{}"),
53+
handler: "Dummy::handler",
54+
});
55+
const version = onAlarmFunction.currentVersion;
56+
const alarm = new Alarm(stack, "DummyAlarm", {
57+
evaluationPeriods: 1,
58+
threshold: 0,
59+
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }),
60+
});
61+
const action = new LambdaAlarmActionStrategy(version);
62+
action.addAlarmActions({ alarm, action });
63+
64+
expect(Template.fromStack(stack)).toMatchSnapshot();
65+
});

0 commit comments

Comments
 (0)