Skip to content

Commit

Permalink
feat: support Lambda actions (#597)
Browse files Browse the repository at this point in the history
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
echeung-amzn authored Nov 13, 2024
1 parent 5b94644 commit 1f8ca08
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 1 deletion.
51 changes: 50 additions & 1 deletion API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions lib/common/alarm/action/LambdaAlarmActionStrategy.ts
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));
}
}
1 change: 1 addition & 0 deletions lib/common/alarm/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./IAlarmActionStrategy";
export * from "./LambdaAlarmActionStrategy";
export * from "./MultipleAlarmActionStrategy";
export * from "./NoopAlarmActionStrategy";
export * from "./OpsItemAlarmActionStrategy";
Expand Down
65 changes: 65 additions & 0 deletions test/common/alarm/action/LambdaAlarmActionStrategy.test.ts
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();
});
Loading

0 comments on commit 1f8ca08

Please sign in to comment.