Fetching CloudWatch metrics using the AWS Java SDK?

Question:

I’m trying to fetch CPU stats from an EC2 instance using the CloudWatch API:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudwatch/AmazonCloudWatchClient.html

I have the following code but its returning an empty result, even though the instance Id, and AWS access and secret keys are correct.

I can see the CPU util for the instance on the CloudWatch UI, but can’t seem to get it below?

I’m using version 1.9.0 of the AWS SDK.

Any help much appreciated.

Answer:

I was missing the endpoint setting on the client. Working now.

Changed this:

to this:

Leave a Reply