Example usage for com.amazonaws.util EC2MetadataUtils.InstanceInfo getInstanceType

List of usage examples for com.amazonaws.util EC2MetadataUtils.InstanceInfo getInstanceType

Introduction

In this page you can find the example usage for com.amazonaws.util EC2MetadataUtils.InstanceInfo getInstanceType.

Prototype

public static String getInstanceType() 

Source Link

Document

Get the type of the instance.

Usage

From source file:com.amazon.kinesis.streaming.agent.processing.processors.AddEC2MetadataConverter.java

License:Open Source License

private void refreshEC2Metadata() {
    LOGGER.info("Refreshing EC2 metadata");

    metadataTimestamp = System.currentTimeMillis();

    try {//  w ww . j  ava 2 s .  c  o  m
        EC2MetadataUtils.InstanceInfo info = EC2MetadataUtils.getInstanceInfo();

        metadata = new LinkedHashMap<String, Object>();
        metadata.put("privateIp", info.getPrivateIp());
        metadata.put("availabilityZone", info.getAvailabilityZone());
        metadata.put("instanceId", info.getInstanceId());
        metadata.put("instanceType", info.getInstanceType());
        metadata.put("accountId", info.getAccountId());
        metadata.put("amiId", info.getImageId());
        metadata.put("region", info.getRegion());
        metadata.put("metadataTimestamp",
                new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date(metadataTimestamp)));

        final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
        DescribeTagsResult result = ec2.describeTags(new DescribeTagsRequest()
                .withFilters(new Filter().withName("resource-id").withValues(info.getInstanceId())));
        List<TagDescription> tags = result.getTags();

        Map<String, Object> metadataTags = new LinkedHashMap<String, Object>();
        for (TagDescription tag : tags) {
            metadataTags.put(tag.getKey().toLowerCase(), tag.getValue());
        }
        metadata.put("tags", metadataTags);
    } catch (Exception ex) {
        LOGGER.warn("Error while updating EC2 metadata - " + ex.getMessage() + ", ignoring");
    }
}