Skip to content

Commit 4a0f0d7

Browse files
author
Eugene Cheung
authored
feat(secretsmanager): provide method to interact with metrics publisher Lambda function (#219)
Based on a customer request since they want to monitor the function too. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 5051193 commit 4a0f0d7

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

API.md

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/monitoring/aws-secretsmanager/SecretsManagerMetricsPublisher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { SecretsManagerSecretMetricFactory } from "./SecretsManagerSecretMetricF
2020

2121
export class SecretsManagerMetricsPublisher extends Construct {
2222
private static instances: Record<string, SecretsManagerMetricsPublisher> = {};
23-
private readonly lambda: IFunction;
23+
public readonly lambda: IFunction;
2424

2525
private constructor(scope: MonitoringScope) {
2626
super(scope, "SecretsManagerMetricsPublisher");

lib/monitoring/aws-secretsmanager/SecretsManagerSecretMonitoring.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
HorizontalAnnotation,
44
IWidget,
55
} from "aws-cdk-lib/aws-cloudwatch";
6+
import { IFunction } from "aws-cdk-lib/aws-lambda";
67

78
import {
89
AgeAlarmFactory,
@@ -26,6 +27,10 @@ import {
2627
SecretsManagerSecretMetricFactoryProps,
2728
} from "./SecretsManagerSecretMetricFactory";
2829

30+
export interface IPublisherConsumer {
31+
consume(lambdaFunction: IFunction): void;
32+
}
33+
2934
export interface SecretsManagerSecretMonitoringOptions
3035
extends BaseMonitoringProps {
3136
readonly addDaysSinceLastChangeAlarm?: Record<
@@ -41,6 +46,12 @@ export interface SecretsManagerSecretMonitoringOptions
4146
* @default - true, if `addDaysSinceLastRotationAlarm` is set, otherwise `false`.
4247
*/
4348
readonly showLastRotationWidget?: boolean;
49+
50+
/**
51+
* Provides access to the underlying metrics publisher Lambda function.
52+
* This may be useful if you want to monitor the function itself.
53+
*/
54+
readonly usePublisher?: IPublisherConsumer;
4455
}
4556

4657
/**
@@ -116,6 +127,7 @@ export class SecretsManagerSecretMonitoring extends Monitoring {
116127
}
117128

118129
props.useCreatedAlarms?.consume(this.createdAlarms());
130+
props.usePublisher?.consume(publisher.lambda);
119131
}
120132

121133
private getDaysSinceLastChangeWidget() {

test/monitoring/aws-secretsmanager/SecretsManagerSecretMonitoring.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { App, Duration, Stack } from "aws-cdk-lib";
22
import { Template } from "aws-cdk-lib/assertions";
3+
import { IFunction } from "aws-cdk-lib/aws-lambda";
34
import { Secret } from "aws-cdk-lib/aws-secretsmanager";
45

56
import {
@@ -29,6 +30,7 @@ test("snapshot test", () => {
2930

3031
let numAlarmsCreated = 0;
3132

33+
let lambdaFunction1: IFunction;
3234
new SecretsManagerSecretMonitoring(scope, {
3335
secret: secret1,
3436
addDaysSinceLastChangeAlarm: {
@@ -46,8 +48,14 @@ test("snapshot test", () => {
4648
numAlarmsCreated += alarms.length;
4749
},
4850
},
51+
usePublisher: {
52+
consume(lambdaFunction: IFunction) {
53+
lambdaFunction1 = lambdaFunction;
54+
},
55+
},
4956
});
5057

58+
let lambdaFunction2: IFunction;
5159
new SecretsManagerSecretMonitoring(scope, {
5260
secret: secret2,
5361
addDaysSinceLastRotationAlarm: {
@@ -61,8 +69,14 @@ test("snapshot test", () => {
6169
numAlarmsCreated += alarms.length;
6270
},
6371
},
72+
usePublisher: {
73+
consume(lambdaFunction: IFunction) {
74+
lambdaFunction2 = lambdaFunction;
75+
},
76+
},
6477
});
6578

79+
let lambdaFunction3: IFunction;
6680
new SecretsManagerSecretMonitoring(scope, {
6781
secret: secret3,
6882
addDaysSinceLastChangeAlarm: {
@@ -77,10 +91,19 @@ test("snapshot test", () => {
7791
numAlarmsCreated += alarms.length;
7892
},
7993
},
94+
usePublisher: {
95+
consume(lambdaFunction: IFunction) {
96+
lambdaFunction3 = lambdaFunction;
97+
},
98+
},
8099
});
81100

82101
expect(numAlarmsCreated).toStrictEqual(4);
83102

103+
// All the same instance since multiple publishers shouldn't be created
104+
expect(lambdaFunction1!).toBe(lambdaFunction2!);
105+
expect(lambdaFunction2!).toBe(lambdaFunction3!);
106+
84107
forceStableAssetKeys(stack);
85108

86109
expect(Template.fromStack(stack)).toMatchSnapshot();

0 commit comments

Comments
 (0)