Get current InstanceId in AWS Java SDK

572 Views Asked by At

I am using the AWS CloudWatch SDK to record metrics from a program running inside EC2 instance. I would like to report back the InstanceId as a dimension for these metrics. How do I find out the EC2 InstanceId from a Java program running inside the instance?

1

There are 1 best solutions below

0
On BEST ANSWER

Use EC2MetadataUtils.

import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils;

String instanceId = EC2MetadataUtils.getInstanceId();