Skip to content

Commit 4fab44d

Browse files
author
Eugene Cheung
authored
feat: upgrade secrets metric publisher Lambda to Node.js 18 (#417)
Closes #393 Upgrades the second of the two Lambda handlers we have. Manually verified in a test account by manually invoking the Lambda with a valid event (e.g. `{"secretId": "test-secret"}`) and verifying that the metrics were emitted to CloudWatch. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 0f5ff75 commit 4fab44d

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed
Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
const aws = require("aws-sdk");
1+
const { CloudWatchClient, PutMetricDataCommand } = require('@aws-sdk/client-cloudwatch');
2+
const { SecretsManagerClient, DescribeSecretCommand } = require('@aws-sdk/client-secrets-manager');
23

34
const region = process.env.AWS_REGION;
45
const millisPerDay = 1000 * 60 * 60 * 24;
56

67
const clientOptions = {
7-
region,
8-
maxRetries: 5,
9-
httpOptions: {
10-
connectTimeout: 3 * 1000,
11-
timeout: 3 * 1000,
12-
},
8+
signingRegion: region,
9+
retryMode: 'standard',
1310
};
1411

15-
const cloudwatch = new aws.CloudWatch(clientOptions);
16-
const sm = new aws.SecretsManager(clientOptions);
12+
const cloudwatchClient = new CloudWatchClient(clientOptions);
13+
const secretsManagerClient = new SecretsManagerClient(clientOptions);
1714

1815
function daysSince(date, now = Date.now()) {
1916
const millis = now - date.getTime();
@@ -22,19 +19,18 @@ function daysSince(date, now = Date.now()) {
2219
}
2320

2421
exports.handler = async (event, context) => {
25-
console.debug("event:", JSON.stringify(event));
26-
console.debug("context:", JSON.stringify(context));
22+
console.info(`Retrieving secret for event ${JSON.stringify(event)}`);
23+
console.debug(`context: ${JSON.stringify(context)}`);
2724

28-
console.info(`retrieving secret: ${event.secretId}`);
29-
const secret = await sm
30-
.describeSecret({
25+
const secret = await secretsManagerClient.send(
26+
new DescribeSecretCommand({
3127
SecretId: event.secretId,
3228
})
33-
.promise();
29+
);
3430

35-
console.debug("found secret: ", JSON.stringify(secret));
31+
console.debug(`Found secret: ${JSON.stringify(secret)}`);
3632
if (!secret.Name || !secret.CreatedDate) {
37-
throw new Error("invalid secret response");
33+
throw new Error("Invalid secret response");
3834
}
3935

4036
// use retrieved secret name in case secretId was an arn
@@ -43,40 +39,37 @@ exports.handler = async (event, context) => {
4339
const lastRotatedDate = secret.LastRotatedDate ?? secret.CreatedDate;
4440
const now = Date.now();
4541

46-
const metricData = [
47-
{
48-
MetricName: "DaysSinceLastChange",
49-
Dimensions: [
50-
{
51-
Name: "SecretName",
52-
Value: secretName,
53-
},
54-
],
55-
Unit: "Count",
56-
Value: daysSince(lastChangedDate, now),
57-
},
58-
{
59-
MetricName: "DaysSinceLastRotation",
60-
Dimensions: [
61-
{
62-
Name: "SecretName",
63-
Value: secretName,
64-
},
65-
],
66-
Unit: "Count",
67-
Value: daysSince(lastRotatedDate, now),
68-
},
69-
];
70-
7142
const params = {
7243
Namespace: "SecretsManager",
73-
MetricData: metricData,
44+
MetricData: [
45+
{
46+
MetricName: "DaysSinceLastChange",
47+
Dimensions: [
48+
{
49+
Name: "SecretName",
50+
Value: secretName,
51+
},
52+
],
53+
Unit: "Count",
54+
Value: daysSince(lastChangedDate, now),
55+
},
56+
{
57+
MetricName: "DaysSinceLastRotation",
58+
Dimensions: [
59+
{
60+
Name: "SecretName",
61+
Value: secretName,
62+
},
63+
],
64+
Unit: "Count",
65+
Value: daysSince(lastRotatedDate, now),
66+
},
67+
],
7468
};
75-
76-
console.debug("putMetricData params: ", JSON.stringify(params));
77-
78-
console.info(`publishing metrics for secret: ${event.secretId}`);
79-
await cloudwatch.putMetricData(params).promise();
69+
console.debug(`putMetricData params: ${JSON.stringify(params)}`);
70+
console.info(`Publishing metrics for secret "${event.secretId}"`);
71+
const command = new PutMetricDataCommand(params);
72+
await cloudwatchClient.send(command);
8073

8174
return Promise.resolve();
8275
};

lib/monitoring/aws-secretsmanager/SecretsManagerMetricsPublisher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SecretsManagerMetricsPublisher extends Construct {
3434
"Custom metrics publisher for SecretsManager Secrets (MonitoringCDKConstructs)",
3535
handler: "index.handler",
3636
memorySize: 128,
37-
runtime: Runtime.NODEJS_14_X,
37+
runtime: Runtime.NODEJS_18_X,
3838
timeout: Duration.seconds(60),
3939
logRetention: RetentionDays.ONE_DAY,
4040
});

test/facade/__snapshots__/MonitoringAspect.test.ts.snap

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

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

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

0 commit comments

Comments
 (0)