0
votes

I have submitted a few training jobs from AWS SageMaker. I want to use boto3 cloudwatch api to fetch the cloudwatch data to be displayed within jupyter notebook instead of using CloudWatch UI.

1

1 Answers

1
votes

You can use CloudWatch GetMetricWidgetImage API to get the graph into the notebook.

Here's an example:

import boto3
from PIL import Image
from io import BytesIO

widget = '''{
    "metrics": [
        [ "namespace", "metricname", "dim1", "value1", "dim2", "value2", "dim3", "value3", { "stat": "Average", "id": "m0r0" } ]
    ],
    "title": "Title of the graph",
    "view": "timeSeries",
    "stacked": false,
    "width": 600,
    "height": 400,
    "start": "-PT3H",
    "end": "P0D"
}'''

client = boto3.client('cloudwatch')

response = client.get_metric_widget_image(
    MetricWidget=widget
)

Image.open(BytesIO(response['MetricWidgetImage']))

You can get the widget definition by opening the metric you need in CloudWatch Console, clicking on the Source tab and selecting Image API view on the bottom. Tweak the width and height per your liking.

You will also need to adjust the policy on your SageMaker execution role to allow calls to GetMetricWidgetImage.