cloudwatch API to retrieve TransitGatewayAttachment metrics via python not working

316 views Asked by At

I can retrieve the transitgateway metrics fine using code below, but it doesnt work when I do it for the tgw attachment. But I know there is stats because the network manager dashboard shows metrics for the tgw and all the attachment and graphs the values.

The code below works and i get a response array back for the tgw, the region is setup properly etc. The tgw attachment is also in the same region so I use the same cloudwatch object.

The time range asked is identical for both tgw and tgw attachment and they were both created at the same time so i'm pretty sure its not a time range issue.

response = cloudwatch.get_metric_data(
MetricDataQueries=[
    {
        'Id': 'm1',
        'MetricStat': {
            'Metric': {
                'Namespace': 'AWS/TransitGateway',
                'MetricName': 'BytesIn',
                'Dimensions': [
                    {
                        'Name': 'TransitGateway',
                        'Value': 'tgw-0456c4c6fe596f58b',
                    },
                ]
            },
            'Period': 360,
            'Stat': 'Sum',  
            'Unit': 'None'
        },
        'ReturnData': True,
    },
],
StartTime=datetime(2020, 10, 1),
EndTime=datetime.now(),
)

Code below brings back an empty array, the return code is 200 and doesnt complain about syntax. the tgw attach id is correct and is actually attached to the above tgw. I know support for granular tgw attach cloud metrics happened recently so maybe its not yet supported via API ?

response_attach = cloudwatch.get_metric_data(
MetricDataQueries=[
    {
        'Id': 'm1',
        'MetricStat': {
            'Metric': {
                'Namespace': 'AWS/TransitGatewayAttachment',
                'MetricName': 'BytesIn',
                'Dimensions': [
                    {
                        'Name': 'TransitGatewayAttachment',
                        'Value': 'tgw-attach-08ed0e1d3e5f488d8'
                    },
                ]
            },
            'Period': 360,
            'Stat': 'Sum',
            'Unit': 'None'
        },
        'ReturnData': True,
    },
],
StartTime=datetime(2020, 10, 1),
EndTime=datetime.now(),
)

Results look like this, no Values results, where as the first set of code to the tgw returns an array in Values. I tried different Stat, Period, MetricName to no avail.

{'MetricDataResults': [{'Id': 'm1', 'Label': 'BytesIn', 'Timestamps': [], 'Values': [], 'StatusCode': 'Complete'}], 'Messages': [], 'ResponseMetadata': {'RequestId': '495f97d5-5862-49f2-b8d0-91346606c88a', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '495f97d5-5862-49f2-b8d0-91346606c88a', 'content-type': 'text/xml', 'content-length': '490', 'date': 'Sat, 03 Oct 2020 10:57:38 GMT'}, 'RetryAttempts': 0}}

Anything obvious I'm doing wrong ? Thanks in advance.

2

There are 2 answers

1
Marcin On BEST ANSWER

Your namespace is incorrect:

 'Namespace': 'AWS/TransitGatewayAttachment',

it should be:

 'Namespace': 'AWS/TransitGateway',

The reason is that TransitGatewayAttachment is a dimension within AWS/TransitGateway namespace as explained in:

0
js9999 On

This finally worked and I believe is giving me the attachment metrics. So seems you have to give both the tgw and attachment in the dimension but it only returns 1 result array and looking at it and comparing when I only ask for tgw, it looks likes the correct value just for the attachment.

I got the clue from looking at the cloudwatch mgmt console and looking at the graph, the source tab showed this snippet below and notice it specified the tgw and attachment although it was only graphing the attachment.

{
    "view": "timeSeries",
    "stacked": false,
    "metrics": [
        [ "AWS/TransitGateway", "BytesIn", "TransitGatewayAttachment", "tgw-attach-08ed0e1d3e5f488d8", "TransitGateway", "tgw-0456c4c6fe596f58b" ]
    ],
    "region": "us-east-1"
}

Code that works

response = cloudwatch.get_metric_data(
MetricDataQueries=[
    {
        'Id': 'm1',
        'MetricStat': {
            'Metric': {
                'Namespace': 'AWS/TransitGateway',
                'MetricName': 'BytesIn',
                'Dimensions': [
                    {
                        'Name': 'TransitGateway',
                        'Value': 'tgw-0456c4c6fe596f58b',
                    },
                    {
                        'Name': 'TransitGatewayAttachment',
                        'Value': 'tgw-attach-08ed0e1d3e5f488d8'
                    },
                ]
            },
            'Period': 360, # 3600,
            'Stat': 'Sum',   # 'SampleCount',
            'Unit': 'None'
        },
        'ReturnData': True,
    },
],
StartTime=datetime(2020, 10, 1),
EndTime=datetime.now(),
)