Read AWS CloudWatch metrics from boto3

Giuseppe Borgese
2 min readAug 10, 2020

For several reasons, I need to read from Python boto3 the number of Requests on my Target Group.

I need to have the 186 number in my boto3 Lambda to run others checks and actions.

When I started this I thought it was an easy one but it wasn’t like that. So I’m writing this very short guide to save you and to my future self :)

GetMetricData or GetMetricStatistics

If you go to the CloudWatch page for boto3 you found 2 options

I tested both and to be sure to do the right choice I ask to AWS Premium support what is the right choice. They point me to this documentation page where there is a good explanation and also some example with AWS CLI, this really helps me to chose the metric_data but it wasn’t enough because I need to have it in boto3.

Solution

This is my final solution a function that returns the final number 186 in my initial example.

I used a filter based on days because it was enough for my purpose but if you are a good python developer (I’m not) you can customize using also hours.

I’m taking back all the results from the end of the current day (because when you add 1 day it starts from midnight) and a number of days in the past, in the example it was 2.

import boto3
import os
from datetime import date, timedelta, datetime
cloudwatch_client = boto3.client('cloudwatch')
def handler(event, context):
mytargetgrouparn='arn:aws:elasticloadbalancing:eu-west-1:012345678:targetgroup/mytgname/d3cf755348f4b85d'
myapplicationlbarn='arn:aws:elasticloadbalancing:eu-west-1:012345678:loadbalancer/app/myappliclbname/11229988hfhunls'
print(access_last_days_data_metric(mytargetgrouparn,myapplicationlbarn,2))
def access_last_days_data_metric(targetGroupARN,loadBalancerARN,days):
tgarray=targetGroupARN.split(':')
tgstring=tgarray[-1]

lbarray=loadBalancerARN.split(':')
lbstring=lbarray[-1]
lbarray2=lbstring.split('/')
lbstring2=lbarray2[1]+'/'+lbarray2[2]+'/'+lbarray2[3]
yesterday=date.today() - timedelta(days=days)
tomorrow=date.today() + timedelta(days=1)


response = cloudwatch_client.get_metric_data(
MetricDataQueries=[
{
'Id': 'myrequest',
'MetricStat': {
'Metric': {
'Namespace': 'AWS/ApplicationELB',
'MetricName': 'RequestCount',
'Dimensions': [
{
'Name': 'TargetGroup',
'Value': tgstring
},
{
'Name': 'LoadBalancer',
'Value': lbstring2
},
]
},
'Period': 300,
'Stat': 'Sum',
'Unit': 'Count'
}
},
],
StartTime=datetime(yesterday.year, yesterday.month, yesterday.day),
EndTime=datetime(tomorrow.year, tomorrow.month, tomorrow.day),
)
return sum(response['MetricDataResults'][0]['Values'])

If you want to know more about Python date and time objects here a good article

Feedback

If you like this article, and you want to motivate me to continue to write, please:

--

--

Giuseppe Borgese

AWS DevOps Professional Certified — Book Author — Terraform Modules Contributor — AWS Tech Youtuber