Example usage for com.amazonaws.util EC2MetadataUtils getEC2InstanceRegion

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

Introduction

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

Prototype

public static String getEC2InstanceRegion() 

Source Link

Document

Returns the current region of this running EC2 instance; or null if it is unable to do so.

Usage

From source file:com.yahoo.athenz.zms.store.impl.AWSObjectStoreFactory.java

License:Apache License

String getAuthToken(String hostname, int port, String rdsUser, String rdsIamRole) {

    InstanceProfileCredentialsProvider awsCredProvider = new InstanceProfileCredentialsProvider(true);

    if (LOG.isDebugEnabled()) {
        LOG.debug("getAuthToken: Access key id: {}", awsCredProvider.getCredentials().getAWSAccessKeyId());
    }//  w  w w  . j  a  va2s .  com

    RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder().credentials(awsCredProvider)
            .region(EC2MetadataUtils.getEC2InstanceRegion()).build();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Instance {} Port {} User {} Region: {} Role: {}", hostname, port, rdsUser,
                EC2MetadataUtils.getEC2InstanceRegion(), rdsIamRole);
    }

    return generator.getAuthToken(
            GetIamAuthTokenRequest.builder().hostname(hostname).port(port).userName(rdsUser).build());
}

From source file:org.apache.hadoop.dynamodb.DynamoDBUtil.java

License:Open Source License

/**
 * Calculates DynamoDB end-point./*www. j  a  v a  2s.  co m*/
 *
 * Algorithm details:
 * <ol>
 * <li> Use endpoint in job configuration "dynamodb.endpoint" value if available
 * <li> Use endpoint from region in job configuration "dynamodb.region" value if available
 * <li> Use endpoint from region in job configuration "dynamodb.regionid" value if available
 * <li> Use endpoint from EC2 Metadata of instance if available
 * <li> If all previous attempts at retrieving endpoint fail, default to us-east-1 endpoint
 * </ol>
 *
 * @param conf   Job Configuration
 * @param region optional preferred region
 * @return end-point for DynamoDb service
 */
public static String getDynamoDBEndpoint(Configuration conf, String region) {
    String endpoint = getValueFromConf(conf, DynamoDBConstants.ENDPOINT);
    if (Strings.isNullOrEmpty(endpoint)) {
        if (Strings.isNullOrEmpty(region)) {
            region = getValueFromConf(conf, DynamoDBConstants.REGION);
        }
        if (Strings.isNullOrEmpty(region)) {
            region = getValueFromConf(conf, DynamoDBConstants.REGION_ID);
        }
        if (Strings.isNullOrEmpty(region)) {
            try {
                region = EC2MetadataUtils.getEC2InstanceRegion();
            } catch (Exception e) {
                log.warn(String.format("Exception when attempting to get AWS region information. Will "
                        + "ignore and default " + "to %s", DynamoDBConstants.DEFAULT_AWS_REGION), e);
            }
        }
        if (Strings.isNullOrEmpty(region)) {
            region = DynamoDBConstants.DEFAULT_AWS_REGION;
        }
        endpoint = RegionUtils.getRegion(region).getServiceEndpoint(ServiceAbbreviations.Dynamodb);
    }
    log.info("Using endpoint for DynamoDB: " + endpoint);
    return endpoint;
}