Java Code Examples for com.amazonaws.regions.Regions#getCurrentRegion()

The following examples show how to use com.amazonaws.regions.Regions#getCurrentRegion() . These examples are extracted from open source projects. 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 Project: titus-control-plane   File: Main.java    License: Apache License 2.0 6 votes vote down vote up
private static AwsIamConnector createConnector() {
    AWSCredentialsProvider baseCredentials = new ProfileCredentialsProvider("default");
    AWSSecurityTokenServiceAsync stsClient = new AmazonStsAsyncProvider(CONFIGURATION, baseCredentials).get();
    AWSCredentialsProvider credentialsProvider = new DataPlaneAgentCredentialsProvider(CONFIGURATION, stsClient, baseCredentials).get();

    Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion == null) {
        currentRegion = Region.getRegion(Regions.US_EAST_1);
    }
    return new AwsIamConnector(
            CONFIGURATION,
            AmazonIdentityManagementAsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build(),
            AWSSecurityTokenServiceAsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build(),
            new DefaultRegistry()
    );
}
 
Example 2
Source Project: titus-control-plane   File: Main.java    License: Apache License 2.0 6 votes vote down vote up
private static AwsInstanceCloudConnector createConnector() {
    AWSCredentialsProvider baseCredentials = new ProfileCredentialsProvider("default");
    AWSSecurityTokenServiceAsync stsClient = new AmazonStsAsyncProvider(CONFIGURATION, baseCredentials).get();
    AWSCredentialsProvider credentialsProvider = new DataPlaneControllerCredentialsProvider(CONFIGURATION, stsClient, baseCredentials).get();

    Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion == null) {
        currentRegion = Region.getRegion(Regions.US_EAST_1);
    }
    return new AwsInstanceCloudConnector(
            CONFIGURATION,
            AmazonEC2AsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build(),
            AmazonAutoScalingAsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build()
    );
}
 
Example 3
private static Region getRegion(EnvVars vars) {
	if (vars.get(AWS_DEFAULT_REGION) != null) {
		return Region.getRegion(Regions.fromName(vars.get(AWS_DEFAULT_REGION)));
	}
	if (vars.get(AWS_REGION) != null) {
		return Region.getRegion(Regions.fromName(vars.get(AWS_REGION)));
	}
	if (System.getenv(AWS_DEFAULT_REGION) != null) {
		return Region.getRegion(Regions.fromName(System.getenv(AWS_DEFAULT_REGION)));
	}
	if (System.getenv(AWS_REGION) != null) {
		return Region.getRegion(Regions.fromName(System.getenv(AWS_REGION)));
	}
	Region currentRegion = Regions.getCurrentRegion();
	if (currentRegion != null) {
		return currentRegion;
	}
	return Region.getRegion(Regions.DEFAULT_REGION);
}
 
Example 4
Source Project: crate   File: AwsEc2ServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
private AmazonEC2 buildClient(Ec2ClientSettings clientSettings) {
    final AWSCredentialsProvider credentials = buildCredentials(LOGGER, clientSettings);
    final ClientConfiguration configuration = buildConfiguration(LOGGER, clientSettings);
    final AmazonEC2 client = buildClient(credentials, configuration);
    if (Strings.hasText(clientSettings.endpoint)) {
        LOGGER.debug("using explicit ec2 endpoint [{}]", clientSettings.endpoint);
        client.setEndpoint(clientSettings.endpoint);
    } else {
        Region currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null) {
            LOGGER.debug("using ec2 region [{}]", currentRegion);
            client.setRegion(currentRegion);
        }
    }
    return client;
}
 
Example 5
@Override
public AWSGlue newClient() throws MetaException {
  try {
    AWSGlueClientBuilder glueClientBuilder = AWSGlueClientBuilder.standard()
        .withCredentials(getAWSCredentialsProvider(conf));

    String regionStr = getProperty(AWS_REGION, conf);
    String glueEndpoint = getProperty(AWS_GLUE_ENDPOINT, conf);

    // ClientBuilder only allows one of EndpointConfiguration or Region to be set
    if (StringUtils.isNotBlank(glueEndpoint)) {
      logger.info("Setting glue service endpoint to " + glueEndpoint);
      glueClientBuilder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(glueEndpoint, null));
    } else if (StringUtils.isNotBlank(regionStr)) {
      logger.info("Setting region to : " + regionStr);
      glueClientBuilder.setRegion(regionStr);
    } else {
      Region currentRegion = Regions.getCurrentRegion();
      if (currentRegion != null) {
        logger.info("Using region from ec2 metadata : " + currentRegion.getName());
        glueClientBuilder.setRegion(currentRegion.getName());
      } else {
        logger.info("No region info found, using SDK default region: us-east-1");
      }
    }

    glueClientBuilder.setClientConfiguration(buildClientConfiguration(conf));
    return glueClientBuilder.build();
  } catch (Exception e) {
    String message = "Unable to build AWSGlueClient: " + e;
    logger.error(message);
    throw new MetaException(message);
  }
}
 
Example 6
Source Project: presto   File: AwsCurrentRegionHolder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @throws IllegalStateException when no region is resolved to avoid memoizing a transient failure
 */
private static Region loadCurrentRegionOrThrowOnNull()
        throws IllegalStateException
{
    Region result = Regions.getCurrentRegion();
    if (result == null) {
        throw new IllegalStateException("Failed to resolve current AWS region from EC2 metadata");
    }
    return result;
}
 
Example 7
private com.schibsted.security.strongbox.sdk.types.Region getRegion() {
    com.amazonaws.regions.Region region = Regions.getCurrentRegion();
    if (region == null) {
        LOG.debug("Cannot get current AWS region, using default region eu-west-1");
        region = com.amazonaws.regions.Region.getRegion(Regions.EU_WEST_1);
    }
    return com.schibsted.security.strongbox.sdk.types.Region.fromName(region.getName());
}
 
Example 8
private static Region getStartingRegion(final String keyArn) {
    final String region = parseRegionfromKeyArn(keyArn);
    if (region != null) {
        return RegionUtils.getRegion(region);
    }
    final Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion != null) {
        return currentRegion;
    }

    return Region.getRegion(Regions.DEFAULT_REGION);
}
 
Example 9
Source Project: athenz   File: AWSEmailProvider.java    License: Apache License 2.0 5 votes vote down vote up
private static AmazonSimpleEmailService initSES() {
    ///CLOVER:OFF
    Region region = Regions.getCurrentRegion();
    if (region == null) {
        region = Region.getRegion(Regions.US_EAST_1);
    }
    return AmazonSimpleEmailServiceClientBuilder.standard().withRegion(region.getName()).build();
    ///CLOVER:ON
}
 
Example 10
Source Project: tutorials   File: EC2MetadataLiveTest.java    License: MIT License 4 votes vote down vote up
@Before
public void setUp() {
    serverEc2 = Regions.getCurrentRegion() != null;
}
 
Example 11
Source Project: genie   File: S3ClientFactory.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param awsCredentialsProvider The base AWS credentials provider to use for the generated S3 clients
 * @param regionProvider         How this factory should determine the default {@link Regions}
 * @param environment            The Spring application {@link Environment}
 */
public S3ClientFactory(
    final AWSCredentialsProvider awsCredentialsProvider,
    final AwsRegionProvider regionProvider,
    final Environment environment
) {
    this.awsCredentialsProvider = awsCredentialsProvider;

    /*
     * Use the Spring property binder to dynamically map properties under a common root into a map of key to object.
     *
     * In this case we're trying to get bucketName -> BucketProperties
     *
     * So if there were properties like:
     * genie.aws.s3.buckets.someBucket1.roleARN = blah
     * genie.aws.s3.buckets.someBucket2.region = us-east-1
     * genie.aws.s3.buckets.someBucket2.roleARN = blah
     *
     * The result of this should be two entries in the map "bucket1" and "bucket2" mapping to property binding
     * object instances of BucketProperties with the correct property set or null if option wasn't specified.
     */
    this.bucketProperties = Binder
        .get(environment)
        .bind(
            BUCKET_PROPERTIES_ROOT_KEY,
            Bindable.mapOf(String.class, BucketProperties.class)
        )
        .orElse(Collections.emptyMap());

    // Set the initial size to the number of special cases defined in properties + 1 for the default client
    // NOTE: Should we proactively create all necessary clients or be lazy about it? For now, lazy.
    final int initialCapacity = this.bucketProperties.size() + 1;
    this.clientCache = new ConcurrentHashMap<>(initialCapacity);
    this.transferManagerCache = new ConcurrentHashMap<>(initialCapacity);

    String tmpRegion;
    try {
        tmpRegion = regionProvider.getRegion();
    } catch (final SdkClientException e) {
        tmpRegion = Regions.getCurrentRegion() != null
            ? Regions.getCurrentRegion().getName()
            : Regions.US_EAST_1.getName();
        log.warn(
            "Couldn't determine the AWS region from the provider ({}) supplied. Defaulting to {}",
            regionProvider.toString(),
            tmpRegion
        );
    }
    this.defaultRegion = Regions.fromName(tmpRegion);

    // Create a token service client to use if we ever need to assume a role
    // TODO: Perhaps this should be just set to null if the bucket properties are empty as we'll never need it?
    this.stsClient = AWSSecurityTokenServiceClientBuilder
        .standard()
        .withRegion(this.defaultRegion)
        .withCredentials(this.awsCredentialsProvider)
        .build();

    this.bucketToClientKey = new ConcurrentHashMap<>();
}