S3Client throws SdkClientException (Unable to load region) even when an SdkPlugin sets a region
- Dominant language
- Java
- Stars
- 2.6k
- Forks
- 1k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 51
Description
### Describe the bug
When building an S3Client, the client throws an `SdkClientException` because it was unable to load a region. However, I added an `SdkPlugin` to the builder which sets a region. I think this is due to the plugin being evaluated after the builder checks for a region.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Expected Behavior
The plugin should be able to set the region, and the S3Client should build without throwing an exception.
### Current Behavior
When an S3Client builds, it throws an SdkClientException due to not finding a region, even though the Plugin sets a region.
Error log:
```
Exception in thread "main" software.amazon.awssdk.core.exception.SdkClientException: Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@60f00693: [software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@65c7a252: Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region)., software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@1e0b4072: No region provided in profile: default, software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@50378a4: Unable to contact EC2 metadata service.]
at software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:130)
at software.amazon.awssdk.regions.providers.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:70)
at software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder.resolveRegion(AwsDefaultClientBuilder.java:375)
at software.amazon.awssdk.utils.AttributeMap$DerivedValue.primeCache(AttributeMap.java:604)
at software.amazon.awssdk.utils.AttributeMap$DerivedValue.get(AttributeMap.java:593)
at software.amazon.awssdk.utils.AttributeMap$Builder.resolveValue(AttributeMap.java:400)
at software.amazon.awssdk.utils.AttributeMap$Builder.internalGet(AttributeMap.java:389)
at software.amazon.awssdk.utils.AttributeMap$Builder.access$1300(AttributeMap.java:201)
at software.amazon.awssdk.utils.AttributeMap$Builder$1.get(AttributeMap.java:403)
at software.amazon.awssdk.services.s3.DefaultS3BaseClientBuilder.lambda$finalizeServiceConfiguration$2(DefaultS3BaseClientBuilder.java:222)
at software.amazon.awssdk.utils.AttributeMap$DerivedValue.primeCache(AttributeMap.java:604)
at software.amazon.awssdk.utils.AttributeMap$DerivedValue.get(AttributeMap.java:593)
at software.amazon.awssdk.utils.AttributeMap$Builder.resolveValue(AttributeMap.java:400)
at software.amazon.awssdk.utils.AttributeMap$Builder.internalComputeIfAbsent(AttributeMap.java:331)
at software.amazon.awssdk.utils.AttributeMap$Builder.putLazyIfAbsent(AttributeMap.java:275)
at software.amazon.awssdk.core.client.config.SdkClientConfiguration$Builder.lazyOptionIfAbsent(SdkClientConfiguration.java:182)
at software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder.finalizeAwsConfiguration(AwsDefaultClientBuilder.java:191)
at software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder.finalizeChildConfiguration(AwsDefaultClientBuilder.java:173)
at software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder.syncClientConfiguration(SdkDefaultClientBuilder.java:206)
at software.amazon.awssdk.services.s3.DefaultS3ClientBuilder.buildClient(DefaultS3ClientBuilder.java:37)
at software.amazon.awssdk.services.s3.DefaultS3ClientBuilder.buildClient(DefaultS3ClientBuilder.java:26)
at software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder.build(SdkDefaultClientBuilder.java:173)
at org.example.App.main(App.java:16)
```
### Reproduction Steps
```java
package org.example;
import java.net.URI;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ServiceClientConfiguration;
public class App {
public static void main(String[] args) {
S3Client client = S3Client.builder()
.addPlugin(config -> {
((S3ServiceClientConfiguration.Builder) config)
.region(Region.US_EAST_1);
})
.build();
}
}
```
### Possible Solution
I think that the builder is reading the region before running plugins, which causes the exception to be thrown.
### Additional Information/Context
_No response_
### AWS Java SDK version used
software.amazon.awssdk:s3:2.47.4
### JDK version used
openjdk 26.0.1 2026-04-21 OpenJDK Runtime Environment Temurin-26.0.1+8 (build 26.0.1+8) OpenJDK 64-Bit Server VM Temurin-26.0.1+8 (build 26.0.1+8, mixed mode, sharing)
### Operating System and version
Microsoft Windows 11 Enterprise 10.0.26100 Build 26100
Contributor guide
Assessment
This issue has not been assessed yet.