The following code snippet is a sample for retrieving AWS EC2 instance's tags in a Java program.
import java.util.Collection; import java.util.LinkedList; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2Client; import com.amazonaws.services.ec2.model.DescribeTagsRequest; import com.amazonaws.services.ec2.model.DescribeTagsResult; import com.amazonaws.services.ec2.model.Filter; import com.amazonaws.services.ec2.model.TagDescription; import com.amazonaws.util.EC2MetadataUtils; public class GetTag{ public static void main(String[] args){ DescribeTagsRequest req = new DescribeTagsRequest(); Collectionfilters = new LinkedList<>(); LinkedList instanceList = new LinkedList<>(); //Get instanceId of the current instance String instanceId = EC2MetadataUtils.getInstanceId(); instanceList.add(instanceId); Filter filter = new Filter("resource-id", instanceList); filters.add(filter); req.setFilters(filters); AmazonEC2Client client = new AmazonEC2Client(); client.setRegion(Region.getRegion(Regions.US_WEST_2)); DescribeTagsResult tagResult = client.describeTags(req); if(tagResult != null){ for(TagDescription tag:tagResult.getTags()){ System.out.println(tag.getKey()); System.out.println(tag.getValue()); } } } }