Skip to content

fixed localstack AWS variables resolution#212

Closed
AndrewChubatiuk wants to merge 4 commits into
localstack:masterfrom
AndrewChubatiuk:fix-async-net-call
Closed

fixed localstack AWS variables resolution#212
AndrewChubatiuk wants to merge 4 commits into
localstack:masterfrom
AndrewChubatiuk:fix-async-net-call

Conversation

@AndrewChubatiuk
Copy link
Copy Markdown

Hello guys!
This a fix for a bug that was introduced by #210
All AWS variables such as ${aws:accountId}, ${aws;region} and ${ssm:<path>} cannot be properly resolved because of async function call in a constructor, which cannot be completed before these variables resolution and AWS sdk credentials are not patched before these variables resolution. I'm a not sure that my approach is the best how to call async function synchronously, but at least it works.
Also I've upgraded some dependencies and added some plugin features from Serverless v3

Copy link
Copy Markdown
Member

@whummer whummer left a comment

Choose a reason for hiding this comment

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

Thanks for this PR @AndrewChubatiuk ! Interesting approach with the waitForAsync - generally looks good to me, but would like to request help from @joe4dev to help with some more testing.. 👍

Kudos also for cleaning up the logging logic a big 👌

Comment thread src/index.js Outdated
@whummer whummer requested a review from joe4dev March 12, 2023 22:58
Copy link
Copy Markdown
Member

@joe4dev joe4dev left a comment

Choose a reason for hiding this comment

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

Thank you very much @AndrewChubatiuk for pointing out this issue 🙏 and contributing a PR 👏👏 .

  1. I can confirm that the waitForAsync busy-wait fixes the variable resolver issue. Unfortunately, the IPv6 issue appears to be back again. @AndrewChubatiuk Could you reproduce the IPv6 issue and does the IPv4 fallback work for you (see detailed comment in waitForAsync)?

  2. My second main comment relates to breaking changes from bumping Serverless v2 to v3, especially in relation to the upcoming LocalStack v2 release. See discussion in package.json @whummer

Comment thread spec/unit/index.spec.js
const Serverless = require('serverless')
let AwsProvider;
try {
AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@whummer any idea why this was necessary?

Comment thread src/index.js
const path = require('path');
const net = require('net');
const {promisify} = require('es6-promisify');
const { promisify } = require('util');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice modernization 👍

"Added in: v8.0.0" and should be save as drop-in replacement (if functionally equivalent): https://nodejs.org/api/util.html#utilpromisifyoriginal

Comment thread src/index.js

class LocalstackPlugin {
constructor(serverless, options) {
constructor(serverless, options, { log, progress }) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice logging improvements 👍

Comment thread src/index.js
});
const cwd = process.cwd();
const env = this.clone(process.env);
env.DEBUG = '1';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

⚠️ That is a behavioral change but I guess it makes sense to be consistent with LocalStack.
It also enables disabling DEBUG mode 👍

Comment thread package.json
"sinon": "^6.2.0",
"tempy": "^0.2.1",
"rimraf": "^4.1.2",
"serverless": "^3.27.0",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

⚠️ The major serverless upgrade from v2 to v3 introduces a lot of breaking changes (upgrading-v3) and a long list of new deprecation warnings (deprecations) also related to variable resolving.

I agree that we should upgrade to v3. To indicate breaking changes, we should probably bump serverless-localstack to a new major version. I wonder whether we should batch this with further breaking changes related to the upcoming LocalStack v2 release 🤔

On on hand, we should resolve this variable resolver issue quickly.
On the other hand, we should think about how to savely bump the Serverless dependency to v3 and align with the LocalStack v2 release.

@whummer What do you think?

Comment thread src/index.js Outdated
loop(iterator.next());
}

waitForAsync(function* () {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tested using the localstack-pro-sample lambda-xray (also works in community without X-Ray feature) with some test variables for the hello function:

    environment:
      TABLE_NAME: tableName
      SLS_AWS_ACCOUNT_ID: ${aws:accountId}
      SLS_AWS_REGION: ${aws:region}

✅ I can confirm that this waitForAsync busy-wait fixes the variable resolver issue when using the IPv6 workaround host: http://127.0.0.1
❎ Unfortunately, it re-introduces the IPv6 issue again but now during variable resolve time when trying to connect to localhost. The debug logs reveal that IPv6 is the issue address: '::1':

sls deploy --stage local
Running "serverless" from node_modules
Using serverless-localstack
(node:86411) NOTE: We are formalizing our plans to enter AWS SDK for JavaScript (v2) into maintenance mode in 2023.

Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check the migration guide at https://a.co/7PzMCcy
(Use `node --trace-warnings ...` to show where the warning was created)
Recoverable error occurred (Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.), sleeping for ~4 seconds. Try 1 of 4
Recoverable error occurred (Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.), sleeping for ~5 seconds. Try 2 of 4
Recoverable error occurred (Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.), sleeping for ~5 seconds. Try 3 of 4
Recoverable error occurred (Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.), sleeping for ~5 seconds. Try 4 of 4
Environment: darwin, node 19.1.0, framework 3.28.1 (local) 3.27.0v (global), plugin 6.2.3, SDK 4.3.2
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Cannot resolve serverless.yml: Variables resolution errored with:
  - Cannot resolve variable at "functions.hello.environment.SLS_AWS_ACCOUNT_ID": Inaccessible host: `localhost' at port `4566'. This service may not be available in the `us-west-2' region.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added static localhost to 127.0.0.1 translation to avoid async call

Comment thread src/index.js Outdated
@@ -708,24 +716,44 @@ class LocalstackPlugin {
* a rejected promise on any connection error.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The documentation does not match the updated busy-wait approach. Maybe adding a "synchronous" busy-wait wrapper would be clearer.

Andrii Chubatiuk and others added 3 commits March 28, 2023 10:48
@AndrewChubatiuk
Copy link
Copy Markdown
Author

@joe4dev is there anything needed from me to merge this PR?

@joe4dev
Copy link
Copy Markdown
Member

joe4dev commented Apr 28, 2023

Sorry for the late reply @AndrewChubatiuk
(lots of things with the v2 release and I didn't plan to catch the flu)

We agree that the variable resolver issue is essential and would like to release this fix with Serverless v2 compatibility. Therefore, we plan to extract a PR that fixes the variable resolution issue without breaking the IPv4/IPv6 fallback. This PR can then serve as a basis for a major localstack-serverless release targeting Serverless v3 compatibility.

Comment thread src/index.js
this.log.debug(`Reconfiguring hostname to use ${fallbackHostname} (IPv4) because connection to ${hostname} failed`);
hostname = fallbackHostname;
}
hostname = "127.0.0.1";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this solution works but can be considered a breaking change because users have no way of forcing localhost anymore and it could potentially break applications depending on this 🤔

@steffyP
Copy link
Copy Markdown
Member

steffyP commented May 16, 2023

Hi @AndrewChubatiuk 🙂

Thanks so much for your effort! In the meantime we found another solution (#219) for the issue with resolving environment variables, that keeps the fix we provided in #210 🥳
We also updated the CI pipeline so that the tests now run with serverless v2 and v3 (#220).

In your PR you also updated some other outdated libs - if you still want to work on this PR, please make sure to rebase with master! 🚀

@steffyP
Copy link
Copy Markdown
Member

steffyP commented Jan 29, 2024

Hi @AndrewChubatiuk, unfortunately we haven't back from you in several months.
I am closing this PR now because it's outdated.

Please feel free to create a new PR if you have new changes to contribute to localstack-serverless 🙏 🚀

@steffyP steffyP closed this Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants