I’m trying to set some custom AWS CloudWatch metrics using the Java SDK.
I can’t seem to find anything in the documentation describing how to get certain pieces of data, nor what data I need to include.
MetricDatum datum = new MetricDatum()
.withDimensions(
new Dimension()
.withName("InstanceType").withValue(/* 1 */),
new Dimension()
.withName("InstanceId").withValue(/* 2 */)
/* 3 */
.withMetricName("My metric").withTimestamp(new Date())
.withUnit("Percent").withValue(new Double(55.0));
So, questions (for each of the commented numbers in the code above):
- Where do I get the data to put here, using the Java AWS SDK?
- Where do I get the data to put here, using the Java AWS SDK?
- What other data do I need to include in order to ensure I can aggregate by auto-scaling group?
(aggregating by security group would also be fine)
For #1, I’ve seen that I can make a regular HTTP call to http://169.254.169.254/latest/meta-data/instance-id to get the instance-id, but I’m hoping to do this all via the AWS SDK, if there are methods available to do so.
I posted the question to the Amazon support team.
The EC2 documentation gives a list of URLs that can be called to grab a bunch of meta-data, including the InstanceType (question 1), the InstanceId (question 2), and the security group (question 3).
The auto-scaling group can be obtained using the AWS SDK for Java, by getting a list of all the auto-scaling groups, and then iterating through that list until you find the instance with your own instanceId (that was retrieved using the URL listed above):