Skip to content

Commit

Permalink
feat: upgrade bitmap widget rendering Lambda to Node.js 18 (#416)
Browse files Browse the repository at this point in the history
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_
  • Loading branch information
echeung-amzn authored Aug 29, 2023
1 parent d4116ea commit 0f5ff75
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
32 changes: 10 additions & 22 deletions assets/BitmapWidgetRenderingSupport/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const aws = require('aws-sdk');
const { CloudWatchClient, GetMetricWidgetImageCommand } = require('@aws-sdk/client-cloudwatch');

const DOCS = `
## Display a CloudWatch bitmap graph
Expand All @@ -21,27 +21,15 @@ const DOCS = `

exports.handler = async (event) => {
async function renderUsingCloudWatch(graph, width, height) {
const params = {MetricWidget: JSON.stringify(graph)};
const region = graph.region;
const customBackoff = (retryCount) => {
// Keep retrying with a random delay, long enough to overcome throttling from CW
const delay = 300 + Math.floor(Math.random() * 500);
console.log(`retry number ${retryCount} with a delay of ${delay} ms`);
return delay;
}
const clientOptions = {
region,
// Keep retrying until the Lambda times out
maxRetries: 99,
retryDelayOptions: {customBackoff},
httpOptions: {
connectTimeout: 3 * 1000,
timeout: 3 * 1000,
},
};
const cloudwatch = new aws.CloudWatch(clientOptions);
const image = await cloudwatch.getMetricWidgetImage(params).promise();
const base64Image = Buffer.from(image.MetricWidgetImage).toString('base64');
const client = new CloudWatchClient({
signingRegion: graph.region,
retryMode: 'standard',
});
const command = new GetMetricWidgetImageCommand({
MetricWidget: JSON.stringify(graph),
});
const response = await client.send(command);
const base64Image = Buffer.from(response.MetricWidgetImage).toString('base64');
return `<img width="${width}" height="${height}" loading="lazy" src="data:image/png;base64,${base64Image}"/>`;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/dashboard/widget/BitmapWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class BitmapWidgetRenderingSupport extends Construct {
"Custom Widget Render for Bitmap Widgets (MonitoringCDKConstructs)",
handler: "index.handler",
memorySize: 128,
runtime: Runtime.NODEJS_14_X,
runtime: Runtime.NODEJS_18_X,
timeout: Duration.seconds(60),
logRetention: RetentionDays.ONE_DAY,
});
Expand Down

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

0 comments on commit 0f5ff75

Please sign in to comment.