Skip to content

Commit

Permalink
unify construction of target endpoint URL, add support for configurin…
Browse files Browse the repository at this point in the history
…g `AWS_ENDPOINT_URL` (#234)
  • Loading branch information
whummer authored Sep 21, 2023
1 parent 29a3558 commit 2a3384d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ custom:
### Configuration via environment variables

The following environment variables can be configured (taking precedence over the values in `serverless.yml`):
* `EDGE_PORT`: LocalStack edge port to connect to (default: `4566`)
* `LOCALSTACK_HOSTNAME`: LocalStack host name to connect to (default: `localhost`)
* `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.
* `EDGE_PORT`: LocalStack edge port to connect to (deprecated; default: `4566`)
* `LOCALSTACK_HOSTNAME`: LocalStack host name to connect to (deprecated; default: `localhost`)
* `USE_SSL`: Whether to use SSL/HTTPS when connecting to the LocalStack endpoint (deprecated)

### Activating the plugin for certain stages

Expand Down Expand Up @@ -206,6 +208,7 @@ custom:
## Change Log
* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL`
* v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying
* v1.1.0: Fix SSM environment variables resolving issues with serverless v3, change default for `BUCKET_MARKER_LOCAL` to `hot-reload`
* 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/).
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.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-localstack",
"version": "1.1.1",
"version": "1.1.2",
"description": "Connect Serverless to LocalStack!",
"main": "src/index.js",
"scripts": {
Expand All @@ -19,13 +19,13 @@
"author": "LocalStack Team <[email protected]>",
"contributors": [
"LocalStack Team <[email protected]>",
"temyers",
"Waldemar Hummer (whummer)",
"Ben Simon Hartung (bentsku)",
"Joel Scheuner (joe4dev)",
"temyers",
"Justin McCormick <[email protected]>",
"djKooks",
"yohei1126",
"bentsku",
"Joel Scheuner (joe4dev)"
"yohei1126"
],
"license": "MIT",
"bugs": {
Expand Down
38 changes: 31 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ class LocalstackPlugin {
const slsSecretsAWS = this.findPlugin('ServerlessSecrets');
if (slsSecretsAWS) {
slsSecretsAWS.config.options.providerOptions = slsSecretsAWS.config.options.providerOptions || {};
slsSecretsAWS.config.options.providerOptions.endpoint = this.getServiceURL('ssm');
slsSecretsAWS.config.options.providerOptions.endpoint = this.getServiceURL();
slsSecretsAWS.config.options.providerOptions.accessKeyId = 'test';
slsSecretsAWS.config.options.providerOptions.secretAccessKey = 'test';
}
Expand All @@ -570,8 +570,7 @@ class LocalstackPlugin {
}
this.log('Using serverless-localstack');
const hostname = await this.getConnectHostname();
const host = `http://${hostname}`;
const edgePort = this.getEdgePort();

const configChanges = {};

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

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

Expand Down Expand Up @@ -758,9 +757,34 @@ class LocalstackPlugin {
return this.awsProvider;
}

getServiceURL() {
getServiceURL(hostname) {
if (process.env.AWS_ENDPOINT_URL) {
return this.injectHostnameIntoLocalhostURL(process.env.AWS_ENDPOINT_URL, hostname);
}
hostname = hostname || 'localhost';
const proto = TRUE_VALUES.includes(process.env.USE_SSL) ? 'https' : 'http';
return `${proto}://localhost:${this.getEdgePort()}`;
const port = this.getEdgePort();
// little hack here - required to remove the default HTTPS port 443, as otherwise
// routing for some platforms and ephemeral instances (e.g., on namespace.so) fails
const isDefaultPort =
(proto === 'http' && `${port}` === '80') ||
(proto === 'https' && `${port}` === '443');
if (isDefaultPort) {
return `${proto}://${hostname}`;
}
return `${proto}://${hostname}:${port}`;
}

/**
* If the given `endpointURL` points to `localhost`, then inject the given `hostname` into the URL
* and return it. This helps fix IPv6 issues with node v18+ (see also getConnectHostname() above)
*/
injectHostnameIntoLocalhostURL(endpointURL, hostname) {
const url = new URL(endpointURL);
if (hostname && url.hostname === 'localhost') {
url.hostname = hostname;
}
return url.href;
}

log(msg) {
Expand All @@ -786,7 +810,7 @@ class LocalstackPlugin {
stepFunctionsReplaceDisplay() {
const plugin = this.findPlugin('ServerlessStepFunctions');
if (plugin) {
const endpoint = this.getServiceURL()
const endpoint = this.getServiceURL();
plugin.originalDisplay = plugin.display;
plugin.localstackEndpoint = endpoint;

Expand Down

0 comments on commit 2a3384d

Please sign in to comment.