List of usage examples for com.amazonaws.util EC2MetadataUtils.InstanceInfo getInstanceId
public static String getInstanceId()
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 {/*from ww w . ja v a 2s. 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 w ww . j a v a 2 s .com 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; }