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

The following examples show how to use com.amazonaws.util.EC2MetadataUtils#getPrivateIpAddress() . 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: HostnameUtil.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Get the local hostname string.
 * This implementation actually return an IP address string.
 *
 * @return a hostname string
 * @throws UnknownHostException if hostname resolution fails
 */
public static String getHostname() throws UnknownHostException {
    final String hostname;
    if (AwsCloudEnvironmentCheckUtils.isRunningOnCloudEnvironment()) {
        hostname = EC2MetadataUtils.getPrivateIpAddress();
    } else {
        // Fallback if not on AWS
        hostname = InetAddress.getLocalHost().getCanonicalHostName();
    }

    if (StringUtils.isBlank(hostname)) {
        throw new IllegalStateException("Unable to create a Genie Host Info instance as hostname is blank");
    }

    return hostname;
}
 
Example 2
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();
}