StackToolbox / aws-sam-webpack-plugin

A Webpack plugin to replace the build step for SAM CLI
MIT License
147 stars 29 forks source link

Possible Bug with compiling the webpack code #53

Closed jgomes94 closed 3 years ago

jgomes94 commented 3 years ago

Hi,

I've been having an issue for a few days and I can't figure it out . I am convinced that it might be due to webpack plugin but have no idea what can be causing it.

So I have the following code: image

Now this code when I run it simply node app it works and I get the correct result: image

However, after compiling (via using the webpack-cli + sam local start-api) I always get 404 not found and I don't get the request I am expected to get via the data client. Somehow after the code runs through the webpack plugin it "breaks" it?

I have the following configuration for webpack: image

Now I know the problem is not the database itself, because when I run it without the plugin it works.

Things that I've tried:

I've been strugling with this problem for a few days so any help is much appreciated.

Thanks

ryansonshine commented 3 years ago

Hi @jgomes94 ,

Can you create a sample repo with all of the code? There could be a number of causes for this behavior.

A few things to consider/try:

  1. Disable optimization on your webpack config, this can sometimes cause issues with npm packages written specifically for node.
    module.exports = {
    // ... your webpack config
    optimization: {
    minimize: false,
    },
    }
  2. Make sure your data-api-client is included in dependencies and not devDependencies
  3. Try running your logic on my boilerplate here: https://github.com/ryansonshine/sam-typescript-vscode-starter
jgomes94 commented 3 years ago

Hi @ryansonshine , thank you for looking at this,

I've did as you suggested, I turned the optimization off and, yes, the data api client is in the dependencies. Unfortunately without success :(.

The code i'm using is this: https://github.com/jgomes94/sam-node

for you to use it: docker-compose up -d npm i npm run local-start

this will create you a single lambda that you can find in the localhost:3000/cards if you do a GET there you'll see the error.

The strange thing is, however, if you just navigate to /lambdas/create-card and do "node app" you'll see the code works and it prints the right information. (just make sure the file calls the function or it will not print anything you can do it by adding this.lambdaHandler() at the end of the file).

Again, thanks ryan much appreciated

ryansonshine commented 3 years ago

Hey @jgomes94 ,

Thanks for providing the sample. It appears you might have a misconfiguration in your template.yaml

Change

CodeUri: lambdas/create-card/

to

CodeUri: lambdas/create-card

without the trailing slash and let me know if that resolves your issue

ryansonshine commented 3 years ago

Also, that path is defined as a post rather than a get in the template.yaml, which would likely be the result of getting a 404 for doing a GET on that path.

jgomes94 commented 3 years ago

Thanks for the feedback @ryansonshine,

I've changed the codeUri, but unfortunately no success. The code runs, what fails is the the data client with the query. I can see the logs that I print there, so I know that the lambda code is executed.

Did you download the code to your machine? It manage to query the database? or you also didn't get anything?

Yes sorry my fault i meant POST not GET.

For example when I put this simple log:

image

I can see it in the console: image

ryansonshine commented 3 years ago

I see now, the issue you're experiencing is more related to how docker networking works between containers since AWS SAM is launching your lambda in a container of its own.

To solve this, you'll want to provide the --docker-network to the sam local start-api command as well as reference the endpoint by the hostname and path it would resolve as from within the docker network itself.

I've created a PR in your repository with the fix: https://github.com/jgomes94/sam-node/pull/1

ryansonshine commented 3 years ago

@buggy This can be closed as it relates to docker/aws sam rather than the plugin specifically.

jgomes94 commented 3 years ago

I answered in the pull request @ryansonshine and it was totally it! Thank you very much!