Open Rabadash8820 opened 2 years ago
Hi @Rabadash8820 thanks for reporting this issue. We've tried to re-produce it with following files, but sam local invoke
worked without any issues. Can you help us to identify what is the difference between your setup and ours (an example GH repo could be helpful to re-produce the issue too)?
@mndeveci Thanks for getting back to me. I made that example by slashing my current repo down to a minimal project, but its possible that I left something lingering that's causing our setups to differ. This is a private repo so I can't provide a link, but I could share a zip of the repo through another channel.
Hey @Rabadash8820, could you post a message on our Slack channel referencing this issue? I'll DM you to ask for the zip file. Thanks!
I'm having the same issue.
sam init
1 - AWS Quick Start Templates
1 - Hello World Example
N
11 - nodejs18.x
1 - Zip
2 - Hello World Example TypeScript
N
N
sam-app
cd sam-app
sam build
sam local invoke HelloWorldFunction --event events/event.json
START RequestId: becf4508-21f7-4b85-b134-4d62e0cf0309 Version: $LATEST
2023-08-30T04:01:25.596Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1061:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)"," at async start (file:///var/runtime/index.mjs:1256:23)"," at async file:///var/runtime/index.mjs:1262:1"]}
30 Aug 2023 04:01:25,608 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
2023-08-30T04:01:25.724Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1061:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)"," at async start (file:///var/runtime/index.mjs:1256:23)"," at async file:///var/runtime/index.mjs:1262:1"]}
END RequestId: 8e94412c-d8cf-4628-baa5-e777603f3e32
REPORT RequestId: 8e94412c-d8cf-4628-baa5-e777603f3e32 Init Duration: 0.66 ms Duration: 287.99 mBilled Duration: 288 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs", "trace": ["Runtime.ImportModuleError: Error: Cannot find module 'app'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:1061:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)", " at async start (file:///var/runtime/index.mjs:1256:23)", " at async file:///var/runtime/index.mjs:1262:1"]}
START RequestId: 64bf7e54-5509-4762-a97c-3d740498d3df Version: $LATEST
END RequestId: 64bf7e54-5509-4762-a97c-3d740498d3df
REPORT RequestId: 64bf7e54-5509-4762-a97c-3d740498d3df Init Duration: 1.09 ms Duration: 608.42 ms Billed Duration: 609 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}
sam --version
: SAM CLI, version 1.96.0I do agree with this post here. There doesn't seem to be an example of how to structure a SAM application with multiple lambdas and maybe some shared dependencies (e.g. contracts for a crud application) between Lambdas. I think having an example of this would go a long way to benefit the community.
This might be a bug report, but I think mainly its just a question: how can you set up a SAM CLI project for multiple Lambda Functions when using TypeScript/esbuild?
My Attempt
Folder structure:
```txt src/ LambdaEntrypoints/ Function1.ts Function2.ts SharedLib1/ index.ts SharedLib2/ index.ts test/ SharedLib1/ SharedLib2/ package.json tsconfig.json samconfig.toml template.yaml ```tsconfig.json
```jsonc { "extends": "@tsconfig/node16/tsconfig.json", "include": [ "src/**/*.ts", "test/**/*.ts", ], "compilerOptions": { "outDir": "dist/", "incremental": true, "declaration": true, "declarationMap": true, "sourceMap": true, "composite": true, "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictBindCallApply": true, "strictPropertyInitialization": true, "noImplicitThis": true, "alwaysStrict": true, "esModuleInterop": true, } } ```template.yaml
```yml AWSTemplateFormatVersion: "2010-09-09" Transform: AWS::Serverless-2016-10-31 Globals: Function: Architectures: [arm64] Runtime: nodejs16.x CodeUri: . Timeout: 3 Tracing: Active Resources: Function1: Type: AWS::Serverless::Function Metadata: BuildMethod: esbuild BuildProperties: UseNpmCi: true Minify: true Sourcemap: true EntryPoints: [ src/LambdaEntrypoints/Function1.ts ] Properties: # FunctionName: Auto-generate Handler: Function1.handleEvent Function2: Type: AWS::Serverless::Function Metadata: BuildMethod: esbuild BuildProperties: UseNpmCi: true Minify: true Sourcemap: true EntryPoints: [ src/LambdaEntrypoints/Function2.ts ] Properties: # FunctionName: Auto-generate Handler: Function2.handleEvent ````src/LambdaEntrypoints/Function1.ts` (or `Function2.ts`)
```ts import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda"; // Other imports export async function handleEvent(event: APIGatewayProxyEventV2): PromiseExpected result
Running
sam build Function1 && sam local invoke Function1
(or equivalent forFunction2
) would successfully build and run my Lambda Function locally.Actual result
Build succeeds, generating this `.aws-sam` folder contents
```txt build/ Function1/ Function1.js Function1.js.map Function2/ Function2.js Function2.js.map template.yaml build.toml ```But `sam local invoke Function1` fails with this output
```log Invoking Function1.handleEvent (nodejs16.x) Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs16.x:rapid-1.61.0-arm64. Mounting /workspaces/Farmageddon/backend2/.aws-sam/build/Function1as /var/task:ro,delegated inside runtime container START RequestId: bcc02ecf-4892-4702-ab63-c22fe7698b25 Version: $LATEST 2022-10-29T05:42:00.806Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'Function1'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'Function1'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1000:17)"," at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)"," at async start (file:///var/runtime/index.mjs:1200:23)"," at async file:///var/runtime/index.mjs:1206:1"]} 29 Oct 2022 05:42:01,072 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID= 2022-10-29T05:42:03.102Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'Function1'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'Function1'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1000:17)"," at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)"," at async start (file:///var/runtime/index.mjs:1200:23)"," at async file:///var/runtime/index.mjs:1206:1"]} END RequestId: ebb45e29-693a-46e1-ac8b-af1f3008cb4a REPORT RequestId: ebb45e29-693a-46e1-ac8b-af1f3008cb4a Init Duration: 2.94 ms Duration: 4545.67 ms Billed Duration: 4546 ms Memory Size: 128 MB Max Memory Used: 128 MB {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'Function1'\nRequire stack:\n- /var/runtime/index.mjs","trace":["Runtime.ImportModuleError: Error: Cannot find module 'Function1'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1000:17)"," at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)"," at async start (file:///var/runtime/index.mjs:1200:23)"," at async file:///var/runtime/index.mjs:1206:1"]}vscode ```