I am trying to print out just the averaged CPU utilization of an AWS instance. This code will print out the 'response' but the for loop at the end isn't printing the averaged utilization. Could someone assist? Thank you in advance!
import boto3
import sys
from datetime import datetime, timedelta
client = boto3.client('cloudwatch')
response = client.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[
{
'Name': 'InstanceId',
'Value': 'i-1234abcd'
},
],
StartTime=datetime(2018, 4, 23) - timedelta(seconds=600),
EndTime=datetime(2018, 4, 24),
Period=86400,
Statistics=[
'Average',
],
Unit='Percent'
)
for cpu in response:
if cpu['Key'] == 'Average':
k = cpu['Value']
print(k)
This is the error message I am getting:
Traceback (most recent call last):
File "C:\bin\TestCW-CPU.py", line 25, in <module>
if cpu['Key'] == 'Average':
TypeError: string indices must be integers
This will output the average CPU:
for k, v in response.items():
if k == 'Datapoints':
for y in v:
print(y['Average'])