Skip to content

Commit

Permalink
add support for AWS_ENDPOINT_URL, USE_SSL, and BUCKET_MARKER_LOCAL co…
Browse files Browse the repository at this point in the history
…nfigurations (#83)
  • Loading branch information
whummer authored Apr 4, 2023
1 parent becde9c commit 348de14
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ dist

# TernJS port file
.tern-port

cdk.context.json
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ $ cdklocal --version

The following environment variables can be configured:

* `EDGE_PORT`: Port under which LocalStack edge service is accessible (default: `4566`)
* `LOCALSTACK_HOSTNAME`: Target host under which LocalStack edge service is accessible (default: `localhost`)
* `AWS_ENDPOINT_URL`: The endpoint URL to connect to (combination of `USE_SSL`/`LOCALSTACK_HOSTNAME`/`EDGE_PORT` below)
* `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.
* `LAMBDA_MOUNT_CODE`: Whether to use local Lambda code mounting (via setting `__local__` S3 bucket name). Note: may require CDK version <2.14.0 to be fully functional.
* `BUCKET_MARKER_LOCAL`: Magic S3 bucket name for Lambda mount and [hot reloading](https://docs.localstack.cloud/user-guide/tools/lambda-tools/hot-reloading) (default: `__local__`, will default to `hot-reload` in a future release)

## Deploying a Sample App

Expand Down Expand Up @@ -67,6 +70,7 @@ $ awslocal sns list-topics

## Change Log

* 2.18.0: Add support for AWS_ENDPOINT_URL, USE_SSL, and BUCKET_MARKER_LOCAL configurations
* 2.17.0: Fix IPv4 fallback check to prevent IPv6 connection issue with `localhost` on macOS
* 2.16.0: Add check to prevent IPv6 connection issue with `localhost` on MacOS
* 2.15.0: Fix issue with undefined BUCKET_NAME_OUTPUT variable; add CI build and eslint config
Expand Down
23 changes: 13 additions & 10 deletions bin/cdklocal
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ const https = require("https");
const crypto = require("crypto");
const net = require('net');

// config constants
// constants and custom config values

const isEnvTrue = (envName) => ["1", "true"].includes(process.env[envName]);

const DEFAULT_EDGE_PORT = 4566;
const EDGE_PORT = process.env.EDGE_PORT || DEFAULT_EDGE_PORT;
const DEFAULT_HOSTNAME = "localhost";
const LAMBDA_MOUNT_CODE = ["1", "true"].includes(process.env.LAMBDA_MOUNT_CODE);
const LAMBDA_MOUNT_CODE = isEnvTrue("LAMBDA_MOUNT_CODE");
const PROTOCOL = isEnvTrue("USE_SSL") ? "https" : "http";


//----------------
// UTIL FUNCTIONS
//----------------

const getLocalEndpoint = async () => `http://${await getLocalHost()}`;

const port = process.env.EDGE_PORT || DEFAULT_EDGE_PORT;
const getLocalEndpoint = async () => process.env.AWS_ENDPOINT_URL || `${PROTOCOL}://${await getLocalHost()}`;

var resolvedHostname = undefined;

const getLocalHost = async () => {
if (resolvedHostname) {
// early exit to not resolve again
return `${resolvedHostname}:${port}`;
return `${resolvedHostname}:${EDGE_PORT}`;
}

var hostname = process.env.LOCALSTACK_HOSTNAME || DEFAULT_HOSTNAME;
Expand All @@ -40,15 +42,15 @@ const getLocalHost = async () => {
// Issue: https://github.com/localstack/aws-cdk-local/issues/78
if (hostname === "localhost") {
try {
const options = { host: hostname, port: port };
const options = { host: hostname, port: EDGE_PORT };
await checkTCPConnection(options);
} catch (e) {
hostname = "127.0.0.1";
}
}

resolvedHostname = hostname;
return `${hostname}:${port}`;
return `${hostname}:${EDGE_PORT}`;
};

/**
Expand Down Expand Up @@ -204,7 +206,7 @@ const patchToolkitInfo = (ToolkitInfo) => {
async get () {
const bucket = this.requireOutput(BUCKET_NAME_OUTPUT);
const domain = this.requireOutput(BUCKET_DOMAIN_NAME_OUTPUT) || await getLocalHost();
return `https://${domain.replace(`${bucket}.`, "")}:${port}/${bucket}`;
return `https://${domain.replace(`${bucket}.`, "")}:${EDGE_PORT}/${bucket}`;
}
});
};
Expand Down Expand Up @@ -257,7 +259,8 @@ const patchLambdaMounting = (CdkToolkit) => {
const paramKey = item.ParameterKey || key;
// TODO: create a more resilient lookup mechanism (not based on "S3Bucket" param key) below!
if (item.ParameterKey && item.ParameterKey.includes("S3Bucket")) {
item.ParameterValue = "__local__";
// TODO: change the default BUCKET_MARKER_LOCAL to 'hot-reload'
item.ParameterValue = process.env.BUCKET_MARKER_LOCAL || '__local__'; // for now, the default is still __local__
}
if (!paramKey.includes("S3VersionKey")) {
return;
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.17.0",
"version": "2.18.0",
"bin": {
"cdklocal": "bin/cdklocal"
},
Expand Down

0 comments on commit 348de14

Please sign in to comment.