Java Code Examples for com.amazonaws.util.EC2MetadataUtils#getAvailabilityZone()

The following examples show how to use com.amazonaws.util.EC2MetadataUtils#getAvailabilityZone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EC2LocalityInfoProvider.java    From singer with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Map<String, String> conf) throws IllegalArgumentException, IOException {
  // check if EC2 AZ info is working
  try {
    EC2MetadataUtils.getAvailabilityZone();
    // cache locality info to avoid service based lookups
    cachedLocality = EC2MetadataUtils.getAvailabilityZone();
  } catch (Exception e) {
    LOG.error("Failed to get AZ info", e);
    Stats.addMetric(SingerMetrics.LOCALITY_MISSING, 1);
    cachedLocality = LOCALITY_NOT_AVAILABLE;
  }
}
 
Example 2
Source File: CloudWatchReporterFactory.java    From dropwizard-metrics-cloudwatch with Apache License 2.0 5 votes vote down vote up
protected Region region() {
    String az = null;
    if (isEC2MetadataAvailable()) {
        az = EC2MetadataUtils.getAvailabilityZone();
    }
    String regionName = awsRegion;
    if (!Strings.isNullOrEmpty(az)) {
        regionName = az.substring(0, az.length() - 1); // strip the AZ letter
    }
    return RegionUtils.getRegion(regionName);
}
 
Example 3
Source File: ServerInstanceContext.java    From chassis with Apache License 2.0 5 votes vote down vote up
private ServerInstanceContext(){
    amazonElasticLoadBalancing = new AmazonElasticLoadBalancingClient();
    amazonEC2 = new AmazonEC2Client();

    ec2MetadataClient = new Ec2MetadataClient() {
        @Override
        public String getAvailabilityZone() {
            return EC2MetadataUtils.getAvailabilityZone();
        }

        @Override
        public String getInstanceId() {
            return EC2MetadataUtils.getInstanceId();
        }

        @Override
        public String getUserData() {
            return EC2MetadataUtils.getUserData();
        }

        @Override
        public String getPrivateIpAddress() {
            return EC2MetadataUtils.getPrivateIpAddress();
        }

        @Override
        public String getPublicIpAddress() {
            for (EC2MetadataUtils.NetworkInterface net : EC2MetadataUtils.getNetworkInterfaces()) {
                List<String> ips = net.getPublicIPv4s();
                if (ips != null && ips.size() > 0) {
                    return ips.get(0);
                }
            }
            return null;
        }
    };

    init();
}