List of usage examples for com.amazonaws.services.ec2.model DescribeTagsRequest setFilters
public void setFilters(java.util.Collection<Filter> filters)
The filters.
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 + "."); }/* w w w .ja v a 2s.c om*/ 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; }