Skip to content

fix: patch SdkProvider._makeSdk for CDK >= 2.177.0 to support path-style S3 URLs#123

Merged
skyrpex merged 3 commits intomainfrom
fix/force-path-style-conditional
Apr 1, 2026
Merged

fix: patch SdkProvider._makeSdk for CDK >= 2.177.0 to support path-style S3 URLs#123
skyrpex merged 3 commits intomainfrom
fix/force-path-style-conditional

Conversation

@whummer
Copy link
Copy Markdown
Member

@whummer whummer commented Mar 28, 2026

Problem

When using cdklocal deploy with CDK >= 2.177.0 against a remote LocalStack endpoint (e.g. *.sandbox.localstack.cloud), asset publishing fails with TLS errors:

QuizAppStack: fail: write EPROTO C0988EF501000000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:ssl/record/rec_layer_s3.c:918:SSL alert number 80

Failed to publish asset GetQuizFunctionLambdaFunction/Code (current_account-current_region-3be8a451)

The root cause is that CDK 2.177.0 reorganised its package structure — aws-cdk/lib now resolves to legacy-exports.js, a thin static wrapper that no longer exposes the real SdkProvider instance methods (e.g. _makeSdk). As a result, the existing patching mechanism in cdklocal no longer takes effect, and S3 clients are created without forcePathStyle: true.

Without path-style URLs, the AWS SDK constructs S3 requests using virtual-hosted-style URLs (e.g. https://<bucket>.abc.sandbox.localstack.cloud/...). The wildcard TLS certificate for sandbox endpoints only covers one subdomain level, so these requests fail the TLS handshake with an internal alert.

Solution

Load the real SdkProvider by resolving the CDK package root and requiring index.js directly (bypassing the exports field), then patch _makeSdk to inject forcePathStyle: true into every SDK instance's config.

The patch is applied conditionally — only when one of the following is true:

  • AWS_S3_FORCE_PATH_STYLE is set — explicit opt-in via env var
  • AWS_ENDPOINT_URL hostname contains .sandbox. — automatic detection for remote LocalStack sandbox endpoints

…S_S3_FORCE_PATH_STYLE is set

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@whummer whummer changed the title fix: apply forcePathStyle patch only for sandbox endpoints or when AWS_S3_FORCE_PATH_STYLE is set fix: patch SdkProvider._makeSdk for CDK >= 2.177.0 to support path-style S3 URLs Mar 28, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@whummer whummer requested review from simonrw and skyrpex March 28, 2026 18:24
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@whummer whummer marked this pull request as ready for review March 28, 2026 18:36
Comment on lines +118 to +126
// require("aws-cdk/lib") resolves to legacy-exports.js which re-exports SdkProvider as a thin
// wrapper with only static methods. The real SdkProvider (with _makeSdk and other instance
// methods) lives in index.js. Load it directly by resolving the package root and bypassing
// the exports field.
const loadRealSdkProvider = () => {
const cdkRoot = path.dirname(require.resolve("aws-cdk/package.json"));
const indexLib = require(path.join(cdkRoot, "lib", "index.js"));
return indexLib.SdkProvider;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't convinced this was going to work with aws-cdk@^2.1114.0 because they removed all exports from the package except for a few, making it illegal to access most of the files and forbidding monkey-patching, BUT turns out that importing a module using an absolute path avoids this restriction. This behavior might be a bug in the node resolution algorithm but if it works temporarily, there's no reason not to accept it.

Just be mindful that this patch could stop working anytime. Any future errors are already swallowed so that's good.

@skyrpex skyrpex merged commit 004cb3d into main Apr 1, 2026
23 checks passed
@whummer whummer deleted the fix/force-path-style-conditional branch April 1, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants