Skip to content
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

Unify construction of target endpoint URL, add support for configuring AWS_ENDPOINT_URL #234

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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`
whummer marked this conversation as resolved.
Show resolved Hide resolved
* 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
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",
whummer marked this conversation as resolved.
Show resolved Hide resolved
"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
23 changes: 16 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,19 @@ class LocalstackPlugin {
return this.awsProvider;
}

getServiceURL() {
getServiceURL(hostname) {
if (process.env.AWS_ENDPOINT_URL) {
Copy link
Member

Choose a reason for hiding this comment

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

great to adopt this new standardized way of endpoint configuration 🚀

Copy link
Member

Choose a reason for hiding this comment

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

We might want to consider aligning the serverless config (host) and environment variable config (AWS_ENDPOINT_URL) in the future for better consistency ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Fully agree, this is more of a bandaid for now, still not super happy with the duality of the yaml config and the environment variables. Definitely think there is still room for improvement in a follow-up change 👌

return process.env.AWS_ENDPOINT_URL;
}
hostname = hostname || 'localhost';
const proto = TRUE_VALUES.includes(process.env.USE_SSL) ? 'https' : 'http';
return `${proto}://localhost:${this.getEdgePort()}`;
const port = this.getEdgePort();
if (proto === 'https' && `${port}` === '443') {
whummer marked this conversation as resolved.
Show resolved Hide resolved
// 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
return `${proto}://${hostname}`;
}
return `${proto}://${hostname}:${port}`;
}

log(msg) {
Expand All @@ -786,7 +795,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