aws-toolkit-jetbrains icon indicating copy to clipboard operation
aws-toolkit-jetbrains copied to clipboard

Update Function Code with TypeScript

Open iheffernan opened this issue 4 years ago • 10 comments
trafficstars

Describe the bug I have a Lambda function written in TypeScript that I can run and debug locally. However, if I try to use AWS Explorer to Update Function Code of a deployed Lambda and then test that deployed Lambda in the AWS Console I always get the following error (my lambda function file is app.ts...

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'app'", "Require stack:", "- /var/runtime/UserFunction.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:100:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)", " at Object.<anonymous> (/var/runtime/index.js:43:30)", " at Module._compile (internal/modules/cjs/loader.js:1072:14)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)", " at Module.load (internal/modules/cjs/loader.js:937:32)", " at Function.Module._load (internal/modules/cjs/loader.js:778:12)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)", " at internal/main/run_main_module.js:17:47" ] }

To reproduce

  1. Create a SAM project in WebStorm
  2. Create and test a Local configuration pointing to src/app.lambdaHandler
  3. Use AWS Explorer to Update Function Code (or create New Lambda) referencing the same handler path of src/app.lambdaHandler

Expected behavior I would expect the deployed Lambda to behave identically to when I run it locally.

Screenshots Screenshots of my local project layout, including the main handler and my local Lambda config are below...

Screen Shot 2021-09-24 at 10 18 27 Screen Shot 2021-09-24 at 10 18 46

Your Environment

  • OS: MacOS 11.6
  • JetBrains product: WebStorm
  • JetBrains product version: 2021.2.2
  • AWS Toolkit version: 1.32-212
  • SAM CLI version: 1.31.0
  • JVM/Python version: n/a

Additional context

iheffernan avatar Sep 24 '21 14:09 iheffernan

So an update here. I can workaround this by Updating the function code with AWS Toolkit, then going into the AWS Console and manually changing the Handler to point to dist/src/app.lambdaHandler. Would be nice, though, if we could cut out that additional step.

iheffernan avatar Sep 28 '21 11:09 iheffernan

I am not sure our custom TS step gets executed in that flow.

We will have to take a look.

FYI, you can update the handler in the Update Function Configuration dialog if you wish to avoid going to the console.

abrooksv avatar Sep 28 '21 16:09 abrooksv

That's the problem - the console allows me to set the handler to dist/src/app.lambdaHandler, but if I try to use that path in the Update Function dialogs it gives me an error that says Must be able to locate the handler in the project in order to deploy to Lambda. Almost like there's some overzealous validation of the Handler path in the Toolkit dialog.

iheffernan avatar Sep 28 '21 18:09 iheffernan

FWIW - once I do set the Handler path in the AWS Console it is retained, regardless of what I put in the AWS Explorer dialog when I do Update Function.

iheffernan avatar Sep 28 '21 18:09 iheffernan

It is over zealous because we currently backtrack from the handler to find where to build the function from.

The fact that the handler doesn't update is probably actually a bug 😄

I was referring to the Update Function Configuration dialog, which does NOT update any code Screen Shot 2021-09-28 at 11 36 28 AM :

abrooksv avatar Sep 28 '21 18:09 abrooksv

I take that last comment back - it does get overwritten on each deployment from AWS Explorer

iheffernan avatar Sep 28 '21 18:09 iheffernan

Ok, that makes sense. Yes, I can update the configuration, but then when I try to actually update the function I get the Must be able to locate... error.

Like I said, I have a workaround for now, but it would be nice because I'll eventually forget to make the update in the console!

iheffernan avatar Sep 28 '21 18:09 iheffernan

I wanted to circle back on this to see if this was something that you would be able to allow in future versions, e.g. specifying the path to the transpiled code as the Handler in the the Update Function Code dialog.

It is still possible to do this in 2 steps by first updating the code and pointing to the source directory that contains the TypeScript handler and then using the Update Function Configuration dialog to change the Handler to the transpiled code. It would just be nice if you could specify the transpiled Handler when updating the code.

Thanks again...

iheffernan avatar Oct 18 '21 12:10 iheffernan

Still the same issue, any progress?

Dec- avatar Sep 01 '22 09:09 Dec-

Still seeing this error when using the AWS Tookit, but no error when running SAM CLI in Terminal

abhishek-parative avatar Jan 04 '24 15:01 abhishek-parative