Skip to content

Commit 2a3384d

Browse files
authored
unify construction of target endpoint URL, add support for configuring AWS_ENDPOINT_URL (#234)
1 parent 29a3558 commit 2a3384d

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ custom:
6363
### Configuration via environment variables
6464

6565
The following environment variables can be configured (taking precedence over the values in `serverless.yml`):
66-
* `EDGE_PORT`: LocalStack edge port to connect to (default: `4566`)
67-
* `LOCALSTACK_HOSTNAME`: LocalStack host name to connect to (default: `localhost`)
66+
* `AWS_ENDPOINT_URL`: LocalStack endpoint URL to connect to (default: `http://localhost:4566`). This is the recommended configuration, and replaces the deprecated config options (`EDGE_PORT`/`LOCALSTACK_HOSTNAME`/`USE_SSL`) below.
67+
* `EDGE_PORT`: LocalStack edge port to connect to (deprecated; default: `4566`)
68+
* `LOCALSTACK_HOSTNAME`: LocalStack host name to connect to (deprecated; default: `localhost`)
69+
* `USE_SSL`: Whether to use SSL/HTTPS when connecting to the LocalStack endpoint (deprecated)
6870

6971
### Activating the plugin for certain stages
7072

@@ -206,6 +208,7 @@ custom:
206208
207209
## Change Log
208210
211+
* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL`
209212
* v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying
210213
* v1.1.0: Fix SSM environment variables resolving issues with serverless v3, change default for `BUCKET_MARKER_LOCAL` to `hot-reload`
211214
* v1.0.6: Add `BUCKET_MARKER_LOCAL` configuration for customizing S3 bucket for lambda mount and [Hot Reloading](https://docs.localstack.cloud/user-guide/tools/lambda-tools/hot-reloading/).

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-localstack",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Connect Serverless to LocalStack!",
55
"main": "src/index.js",
66
"scripts": {
@@ -19,13 +19,13 @@
1919
"author": "LocalStack Team <info@localstack.cloud>",
2020
"contributors": [
2121
"LocalStack Team <info@localstack.cloud>",
22-
"temyers",
2322
"Waldemar Hummer (whummer)",
23+
"Ben Simon Hartung (bentsku)",
24+
"Joel Scheuner (joe4dev)",
25+
"temyers",
2426
"Justin McCormick <me@justinmccormick.com>",
2527
"djKooks",
26-
"yohei1126",
27-
"bentsku",
28-
"Joel Scheuner (joe4dev)"
28+
"yohei1126"
2929
],
3030
"license": "MIT",
3131
"bugs": {

src/index.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ class LocalstackPlugin {
553553
const slsSecretsAWS = this.findPlugin('ServerlessSecrets');
554554
if (slsSecretsAWS) {
555555
slsSecretsAWS.config.options.providerOptions = slsSecretsAWS.config.options.providerOptions || {};
556-
slsSecretsAWS.config.options.providerOptions.endpoint = this.getServiceURL('ssm');
556+
slsSecretsAWS.config.options.providerOptions.endpoint = this.getServiceURL();
557557
slsSecretsAWS.config.options.providerOptions.accessKeyId = 'test';
558558
slsSecretsAWS.config.options.providerOptions.secretAccessKey = 'test';
559559
}
@@ -570,8 +570,7 @@ class LocalstackPlugin {
570570
}
571571
this.log('Using serverless-localstack');
572572
const hostname = await this.getConnectHostname();
573-
const host = `http://${hostname}`;
574-
const edgePort = this.getEdgePort();
573+
575574
const configChanges = {};
576575

577576
// Configure dummy AWS credentials in the environment, to ensure the AWS client libs don't bail.
@@ -591,7 +590,7 @@ class LocalstackPlugin {
591590
}
592591

593592
// If a host has been configured, override each service
594-
const localEndpoint = `${host}:${edgePort}`;
593+
const localEndpoint = this.getServiceURL(hostname);
595594
for (const service of this.awsServices) {
596595
const serviceLower = service.toLowerCase();
597596

@@ -758,9 +757,34 @@ class LocalstackPlugin {
758757
return this.awsProvider;
759758
}
760759

761-
getServiceURL() {
760+
getServiceURL(hostname) {
761+
if (process.env.AWS_ENDPOINT_URL) {
762+
return this.injectHostnameIntoLocalhostURL(process.env.AWS_ENDPOINT_URL, hostname);
763+
}
764+
hostname = hostname || 'localhost';
762765
const proto = TRUE_VALUES.includes(process.env.USE_SSL) ? 'https' : 'http';
763-
return `${proto}://localhost:${this.getEdgePort()}`;
766+
const port = this.getEdgePort();
767+
// little hack here - required to remove the default HTTPS port 443, as otherwise
768+
// routing for some platforms and ephemeral instances (e.g., on namespace.so) fails
769+
const isDefaultPort =
770+
(proto === 'http' && `${port}` === '80') ||
771+
(proto === 'https' && `${port}` === '443');
772+
if (isDefaultPort) {
773+
return `${proto}://${hostname}`;
774+
}
775+
return `${proto}://${hostname}:${port}`;
776+
}
777+
778+
/**
779+
* If the given `endpointURL` points to `localhost`, then inject the given `hostname` into the URL
780+
* and return it. This helps fix IPv6 issues with node v18+ (see also getConnectHostname() above)
781+
*/
782+
injectHostnameIntoLocalhostURL(endpointURL, hostname) {
783+
const url = new URL(endpointURL);
784+
if (hostname && url.hostname === 'localhost') {
785+
url.hostname = hostname;
786+
}
787+
return url.href;
764788
}
765789

766790
log(msg) {
@@ -786,7 +810,7 @@ class LocalstackPlugin {
786810
stepFunctionsReplaceDisplay() {
787811
const plugin = this.findPlugin('ServerlessStepFunctions');
788812
if (plugin) {
789-
const endpoint = this.getServiceURL()
813+
const endpoint = this.getServiceURL();
790814
plugin.originalDisplay = plugin.display;
791815
plugin.localstackEndpoint = endpoint;
792816

0 commit comments

Comments
 (0)