Example usage for com.amazonaws.util EC2MetadataUtils getInstanceInfo

List of usage examples for com.amazonaws.util EC2MetadataUtils getInstanceInfo

Introduction

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

Prototype

public static InstanceInfo getInstanceInfo() 

Source Link

Document

The instance info is only guaranteed to be a JSON document per http://docs .aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html <p> This method is only a best attempt to capture the instance info as a typed object.

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  w  w .  j  a va2  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");
    }
}

From source file:com.hazelcast.samples.amazon.elasticbeanstalk.HazelcastInstanceFactory.java

License:Open Source License

protected Properties getAwsProperties() {
    EC2MetadataUtils.InstanceInfo instanceInfo = EC2MetadataUtils.getInstanceInfo();
    String instanceId = instanceInfo.getInstanceId();

    // EB sets the environment ID and name as the elasticbeanstalk:environment-id and
    // elasticbeanstalk:environment-name EC2 tags on all of the parts of an EB app environment: load balancer,
    // EC2 instances, security groups, etc. Surprisingly, EC2 tags aren't available to instances through the
    // instance metadata interface, but they are available through the normal AWS APIs DescribeTags call.
    Collection<Filter> filters = new ArrayList<Filter>();
    filters.add(new Filter("resource-type").withValues("instance"));
    filters.add(new Filter("resource-id").withValues(instanceId));
    filters.add(new Filter("key").withValues(ELASTICBEANSTALK_ENVIRONMENT_NAME));

    DescribeTagsRequest describeTagsRequest = new DescribeTagsRequest();
    describeTagsRequest.setFilters(filters);
    DescribeTagsResult describeTagsResult = amazonEC2.describeTags(describeTagsRequest);

    if (describeTagsResult == null || describeTagsResult.getTags().isEmpty()) {
        throw new IllegalStateException(
                "No tag " + ELASTICBEANSTALK_ENVIRONMENT_NAME + " found for instance " + instanceId + ".");
    }/*from   ww  w  .j a  v a 2s.c  o m*/

    String environmentName = describeTagsResult.getTags().get(0).getValue();
    String environmentPassword = MD5Util.toMD5String(environmentName);

    Properties properties = new Properties();
    properties.setProperty(HAZELCAST_ENVIRONMENT_NAME, environmentName);
    properties.setProperty(HAZELCAST_ENVIRONMENT_PASSWORD, environmentPassword);
    properties.setProperty(HAZELCAST_AWS_IAM_ROLE, ELASTICBEANSTALK_EC2_ROLE_NAME);
    properties.setProperty(HAZELCAST_AWS_REGION, instanceInfo.getRegion());

    return properties;
}

From source file:org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.java

License:Apache License

protected Region getCurrentRegion() {
    try {/*from w ww  .ja v  a 2 s. c o  m*/
        InstanceInfo instanceInfo = EC2MetadataUtils.getInstanceInfo();
        return instanceInfo != null && instanceInfo.getRegion() != null
                ? RegionUtils.getRegion(instanceInfo.getRegion())
                : null;
    } catch (AmazonClientException e) {
        return null;
    }

}