Skip to content

Fix: support aws-cdk>=2.177.0 #107

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

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
id: simple-matrix
if: ${{ inputs.run-all-latest-cdk-versions == false }}
run: |
export VERSIONS_ARRAY='["2.30.0", "2.50.0", "2.75.0", "2.120.0", "2.166.0", "2.167.0", ""]'
export VERSIONS_ARRAY='["2.30.0", "2.50.0", "2.75.0", "2.120.0", "2.166.0", "2.167.0", "2.177.0", ""]'
echo "VERSIONS_ARRAY=$VERSIONS_ARRAY" >> $GITHUB_ENV

- name: Generate matrix
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ $ export NODE_PATH=$NODE_PATH:/opt/homebrew/Cellar/aws-cdk/<CDK_VERSION>/libexec
The following environment variables can be configured:

* `AWS_ENDPOINT_URL`: The endpoint URL to connect to (combination of `USE_SSL`/`LOCALSTACK_HOSTNAME`/`EDGE_PORT` below)
* `AWS_ENDPOINT_URL_S3`: The endpoint URL to connect to (combination of `USE_SSL`/`LOCALSTACK_HOSTNAME`/`EDGE_PORT` below) for S3 requests
* `EDGE_PORT` (deprecated): Port under which LocalStack edge service is accessible (default: `4566`)
* `LOCALSTACK_HOSTNAME` (deprecated): Target host under which LocalStack edge service is accessible (default: `localhost`)
* `USE_SSL` (deprecated): Whether to use SSL to connect to the LocalStack endpoint, i.e., connect via HTTPS.
Expand Down Expand Up @@ -77,6 +78,7 @@ $ awslocal sns list-topics

## Change Log

* 2.19.2: Fix SDK compatibility with aws-cdk versions >= 2.177.0
* 2.19.1: Fix SDK compatibility with older CDK versions; Fix patched bucket location in TemplateURL
* 2.19.0: Add support for aws-cdk versions >= `2.167.0`
* 2.18.1: Throw better exception if `aws-cdk` not found
Expand Down
31 changes: 29 additions & 2 deletions bin/cdklocal
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const patchToolkitInfo = (ToolkitInfo) => {
await prefetchBucketUrl(toolkitInfoObject);
return toolkitInfoObject;
};

const fromStackFn = ToolkitInfo.fromStack;
ToolkitInfo.fromStack = (...args) => {
const toolkitInfoObject = fromStackFn(...args);
Expand Down Expand Up @@ -451,6 +451,13 @@ const patchPre_2_14 = () => {
applyPatches(provider, CdkToolkit, SDK, ToolkitInfo);
};

const configureEnvironment = () => {
// This _must_ use localhost.localstack.cloud as we require valid subdomains of these paths to
// resolve. Unfortunately though `curl` seems to support subdomains of localhost, the CDK does not.
Comment on lines +455 to +456
Copy link
Member

Choose a reason for hiding this comment

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

So s3.localhost didn't work for you with cdklocal but s3.localhost.localstack.cloud did? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup unfortunately. I think it depends on the dns resolver and tool making the dns query, so sometimes it works and sometimes it doesn't depending on the tool.

process.env.AWS_ENDPOINT_URL_S3 = process.env.AWS_ENDPOINT_URL_S3 || `${PROTOCOL}://s3.localhost.localstack.cloud:${EDGE_PORT}`;
Copy link

Choose a reason for hiding this comment

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

I forgot to share, but by default, we are also using s3.localhost.localstack.cloud for tflocal

process.env.AWS_ENDPOINT_URL = process.env.AWS_ENDPOINT_URL || `${PROTOCOL}://localhost.localstack.cloud:${EDGE_PORT}`;
};

const patchPost_2_14 = () => {
var lib = null;
try {
Expand All @@ -463,7 +470,27 @@ const patchPost_2_14 = () => {
}
}

applyPatches(lib, lib, lib.SDK, lib.ToolkitInfo, false);
// detect if we are using version 2.177.0 or later. This version has reorganised the package
// structure so that we cannot import and patch the aws-cdk package as we did for versions
// <2.177.0. We use the specific error raised by the node require system to determine if we are
// using pre or post 2.177.0.
try {
require("aws-cdk/lib/legacy-exports");
} catch (e) {
switch (e.code) {
case "MODULE_NOT_FOUND":
// pre 2.177
applyPatches(lib, lib, lib.SDK, lib.ToolkitInfo, false);
break;
case "ERR_PACKAGE_PATH_NOT_EXPORTED":
// post 2.177
configureEnvironment();
break;
default:
// a different error
throw e
}
}
};

if (isEsbuildBundle()) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "aws-cdk-local",
"description": "CDK Toolkit for use with LocalStack",
"version": "2.19.1",
"version": "2.19.2",
"bin": {
"cdklocal": "bin/cdklocal"
},
Expand Down