Skip to content

Commit 0f5ff75

Browse files
author
Eugene Cheung
authored
feat: upgrade bitmap widget rendering Lambda to Node.js 18 (#416)
Addresses the first of two Lambda functions for #393. Tested successfully in a dev account configured with bitmap dashboards (i.e. with `renderingPreference: DashboardRenderingPreference.INTERACTIVE_AND_BITMAP)`. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent d4116ea commit 0f5ff75

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

assets/BitmapWidgetRenderingSupport/index.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const aws = require('aws-sdk');
1+
const { CloudWatchClient, GetMetricWidgetImageCommand } = require('@aws-sdk/client-cloudwatch');
22

33
const DOCS = `
44
## Display a CloudWatch bitmap graph
@@ -21,27 +21,15 @@ const DOCS = `
2121

2222
exports.handler = async (event) => {
2323
async function renderUsingCloudWatch(graph, width, height) {
24-
const params = {MetricWidget: JSON.stringify(graph)};
25-
const region = graph.region;
26-
const customBackoff = (retryCount) => {
27-
// Keep retrying with a random delay, long enough to overcome throttling from CW
28-
const delay = 300 + Math.floor(Math.random() * 500);
29-
console.log(`retry number ${retryCount} with a delay of ${delay} ms`);
30-
return delay;
31-
}
32-
const clientOptions = {
33-
region,
34-
// Keep retrying until the Lambda times out
35-
maxRetries: 99,
36-
retryDelayOptions: {customBackoff},
37-
httpOptions: {
38-
connectTimeout: 3 * 1000,
39-
timeout: 3 * 1000,
40-
},
41-
};
42-
const cloudwatch = new aws.CloudWatch(clientOptions);
43-
const image = await cloudwatch.getMetricWidgetImage(params).promise();
44-
const base64Image = Buffer.from(image.MetricWidgetImage).toString('base64');
24+
const client = new CloudWatchClient({
25+
signingRegion: graph.region,
26+
retryMode: 'standard',
27+
});
28+
const command = new GetMetricWidgetImageCommand({
29+
MetricWidget: JSON.stringify(graph),
30+
});
31+
const response = await client.send(command);
32+
const base64Image = Buffer.from(response.MetricWidgetImage).toString('base64');
4533
return `<img width="${width}" height="${height}" loading="lazy" src="data:image/png;base64,${base64Image}"/>`;
4634
}
4735

lib/dashboard/widget/BitmapWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class BitmapWidgetRenderingSupport extends Construct {
3434
"Custom Widget Render for Bitmap Widgets (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/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)