Skip to content

S3Client throws SdkClientException (Unable to load region) even when an SdkPlugin sets a region #7151

Open

Description

@Techno3d

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

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

Activity

  1. added
    bug
    needs-triage
    on Jul 15, 2026
  2. added
    potential-regression
    on Jul 15, 2026
  3. Techno3d commented on Jul 15, 2026

    @Techno3d
    Author

    I may have accidentally checked the regression checkbox. I do not this is a regression, my bad.

  4. removed
    potential-regression
    on Jul 15, 2026
  5. self-assigned this
    on Jul 15, 2026
  6. bhoradc commented on Jul 15, 2026

    @bhoradc
    Contributor

    @Techno3d - Noted, thanks for reporting this.

    Your analysis appears to be correct, the SDK resolves region during client finalization, which occurs before plugins are invoked. Since no region is available through the builder or the default provider chain at that point, the build fails before your plugin gets a chance to set it.

    Note that plugins do currently override region if it's already resolvable, so the plugin mechanism itself works, it's just that the SDK needs a value to be present before plugins execute.

    In the meantime, you can work around this by setting region outside of the SdkPlugin. I will review this with the team to determine next steps. Thanks!

  7. added
    needs-review
    p2
    and removed
    needs-triage
    on Jul 15, 2026
  8. Techno3d commented on Jul 17, 2026

    @Techno3d
    Author

    Thanks for getting back so quickly. Would it be okay for me to work on the fix for this and submit a PR?

  9. Techno3d commented on Jul 17, 2026

    @Techno3d
    Author

    I was looking through the code, and found something a bit strange in how the client configuration gets finalized. In SdkDefaultClientBuilder::syncClientConfiguration and the async version, the method passes SdkClientConfiguration to all of the methods it calls (e.g. setOverrides(), mergeChildDefaults(), etc). Each of these methods takes the SdkClientConfiguration, and converts them to a Builder before building and returning. Is there a reason that the methods use SdkClientConfiguration instead of Builders, and then syncClientConfiguration calls build() at the end?

    I think using builders would solve this issue of the region being resolved before the plugins gets to run, but preserves the plugins precedence. Though there might be something I'm missing.

  10. Techno3d commented on Jul 17, 2026

    @Techno3d
    Author

    I think this code change might fix the issue, though I'm not certain if the finalizeConfiguration methods could overwrite something that plugins could set. My previous comment might be helpful, but I think I was overthinking things a little.

        protected final SdkClientConfiguration asyncClientConfiguration() {
            clientConfiguration.option(SdkClientOption.CLIENT_CONTEXT_PARAMS, clientContextParams.build());
            SdkClientConfiguration configuration = clientConfiguration.build();
    
            // Apply overrides
            configuration = setOverrides(configuration);
    
            // Apply defaults
            configuration = mergeChildDefaults(configuration);
            configuration = mergeGlobalDefaults(configuration);
    
            // Invoke the plugins - Moved the plugin invocation here
            configuration = invokePlugins(configuration);
    
            // Create additional configuration from the default-applied configuration
            configuration = finalizeChildConfiguration(configuration);
            configuration = finalizeAsyncConfiguration(configuration);
            configuration = finalizeConfiguration(configuration);
    
            return configuration;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugneeds-reviewp2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions