diff --git a/assets/BitmapWidgetRenderingSupport/index.js b/assets/BitmapWidgetRenderingSupport/index.js index 8b8778f1..73d9f0ec 100644 --- a/assets/BitmapWidgetRenderingSupport/index.js +++ b/assets/BitmapWidgetRenderingSupport/index.js @@ -1,4 +1,4 @@ -const aws = require('aws-sdk'); +const { CloudWatchClient, GetMetricWidgetImageCommand } = require('@aws-sdk/client-cloudwatch'); const DOCS = ` ## Display a CloudWatch bitmap graph @@ -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 ``; } diff --git a/lib/dashboard/widget/BitmapWidget.ts b/lib/dashboard/widget/BitmapWidget.ts index bfc4f65d..7cfbcd26 100644 --- a/lib/dashboard/widget/BitmapWidget.ts +++ b/lib/dashboard/widget/BitmapWidget.ts @@ -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, }); diff --git a/test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap b/test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap index abba281d..5d9aac16 100644 --- a/test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap +++ b/test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap @@ -109,7 +109,7 @@ Object { "Arn", ], }, - "Runtime": "nodejs14.x", + "Runtime": "nodejs18.x", "Tags": Array [ Object { "Key": "cw-custom-widget",