I’m using the localstack testcontainer to mock the KMS calls from my application. The existing code in my app uses only the region, and it works fine when communicating with AWS.
AWSKMSClientBuilder.standard()
.withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(basicCredentials)).build();
But when I use the localstack params to the builder object, it still calls AWS - https://kms.us-east-1.amazonaws.com/ which is failing with an invalid client exception.
AWSKMSClientBuilder.standard()
.withRegion(Regions.fromName(localstack.getRegion())
.withCredentials(new AWSStaticCredentialsProvider(basicCredentials)).build();
However when i use the endpoint config builder method instead of region , it makes the call to localstack and works fine.
AWSKMSClientBuilder.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpoint().toString(),
localstack.getRegion()
))
Do you know why when region and credentials alone are given, it makes the call to AWS? The AWSKMSClientBuilder doesn’t allow to use of both region and endpointconfiguration in the same builder object. Is there any workaround for this, other than using the withEndpointConfiguration instead of region?
Also, can you please share what is the type of the localstack object in your sample, and what the runtime values of localstack.getEndpoint().toString() and localstack.getRegion()? Thanks
@whummer Im using the v1 SDK. The type of localstack im trying to create is AWS KMS.
public static LocalStackContainer localstack = new LocalStackContainer().withServices(LocalStackContainer.Service.KMS);
The runtime value of localstack.getEndpoint().toString() changes for every run. It uses the port of the test container that gets created. The value of localstack.getRegion() is defaulted to us-east-1.
The only difference is im using region instead of withEndpointConfiguration. Looks like the aws sdk uses the default url unless it is overridden.
Do you know why when region and credentials alone are given, it makes the call to AWS? … Is there any workaround for this, other than using the withEndpointConfiguration instead of region?
Unfortunately not - currently it is required to set the endpoint configuration directly for each client. This is the expected behavior, as the AWS Java SDK does not allow global endpoint configurations (yet).
Please note that AWS has introduced custom endpoints recently, configurable via AWS_ENDPOINT_URL: Service-specific endpoints - AWS SDKs and Tools Unfortunately, they are not yet supported in the Java SDK (see page bottom), but in the future it will become easier to make this configuration