Skip to content

How to read current region and partition #3078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
reegnz opened this issue Mar 3, 2022 · 2 comments
Closed

How to read current region and partition #3078

reegnz opened this issue Mar 3, 2022 · 2 comments
Labels
closed-for-staleness documentation This is a problem with documentation.

Comments

@reegnz
Copy link

reegnz commented Mar 3, 2022

Describe the issue

In AWS SDK for Java V1 there was this simple way of getting access to region metadata, which was:

Region region = Regions.getCurrentRegion()

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html

I can't find any equivalent way for AWS SDK for Java V2.

Links

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html

@reegnz reegnz added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Mar 3, 2022
@reegnz reegnz changed the title How to read current region and partition (short issue description) How to read current region and partition Mar 3, 2022
@debora-ito
Copy link
Member

@reegnz Regions.getCurrentRegion() does not have an equivalent in v2.

There's a feature request to create a EC2 Metadata client, we are tracking here: #61. Make sure you add a 👍 in the original description if you want to support it.

@debora-ito debora-ito added closing-soon This issue will close in 4 days unless further comments are made. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 3, 2022
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will close in 4 days unless further comments are made. labels Mar 5, 2022
@github-actions github-actions bot closed this as completed Mar 5, 2022
@reegnz
Copy link
Author

reegnz commented Mar 7, 2022

In the end I've done it with DefaultAwsRegionProviderChain, it seems like the region also uniquely identifies the partition.
I've also done some other client metadata discovery to find the partition in the example code below (getting the partition is also possible from the caller identity ARN):

import software.amazon.awssdk.arns.Arn;
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
import software.amazon.awssdk.services.sts.StsClient;

public class SdkV2 {

    public static void print() {
        final var regionProvider = DefaultAwsRegionProviderChain.builder().build();
        final var region = regionProvider.getRegion();
        System.out.println("[SDKv2] Region: " + region.id());
        System.out.println("[SDKv2] Partition (from Region): " + region.metadata().partition().id());
        final var cli = StsClient.create();
        final var identity = cli.getCallerIdentity();
        System.out.println("[SDKv2] UserId: " + identity.userId());
        System.out.println("[SDKv2] Account: " + identity.account());
        final var arn = Arn.fromString(identity.arn());
        System.out.println("[SDKv2] Partition (from Identity): " + arn.partition());
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness documentation This is a problem with documentation.
Projects
None yet
Development

No branches or pull requests

2 participants