我正在尝试使用AWS的Hello World示例Typescript模板部署AWS Lambda函数与sam
,并且示例模板不工作。
我很确定这是AWS的一个bug。
这很容易复制:
我在一个EC2实例与ubuntu。运行:
sam init
并遵循以下配置:
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
...
Template: 1
Use the most popular runtime and package type? (Python and zip) [y/N]: N
Which runtime would you like to use?
1 - aot.dotnet7 (provided.al2)
2 - dotnet6
3 - dotnet5.0
4 - dotnetcore3.1
5 - go1.x
6 - go (provided.al2)
7 - graalvm.java11 (provided.al2)
8 - graalvm.java17 (provided.al2)
9 - java11
10 - java8.al2
11 - java8
12 - nodejs18.x
13 - nodejs16.x
14 - nodejs14.x
15 - nodejs12.x
Runtime: 14
What package type would you like to use?
1 - Zip
2 - Image
Package type: 1
Based on your selections, the only dependency manager available is npm.
We will proceed copying the template using npm.
Select your starter template
1 - Hello World Example
2 - Hello World Example TypeScript
Template: 2
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: N
Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: N
Project name [sam-app]:
-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: nodejs14.x
Architectures: x86_64
Dependency Manager: npm
Application Template: hello-world-typescript
Output Directory: .
Configuration file: sam-app/samconfig.toml
Next steps can be found in the README file at sam-app/README.md
现在我们得到这个模板:
ubuntu@ip-111-31-16-16:~/MyAPP/lambda$ tree sam-app/
sam-app/
├── README.md
├── events
│ └── event.json
├── hello-world
│ ├── app.ts
│ ├── jest.config.ts
│ ├── package.json
│ ├── tests
│ │ └── unit
│ │ └── test-handler.test.ts
│ └── tsconfig.json
├── samconfig.toml
└── template.yaml
4 directories, 9 files
现在如果我进入/sam-app
然后输入:
ubuntu@ip-111-31-16-16:~/MyAPP/lambda/sam-app$ sam local invoke --event events/event.json
我得到这个错误:Error: Cannot find module 'app'
;完整的消息在这里:
ubuntu@ip-111-31-16-16:~/MyApp/lambda/sam-app$ sam local invoke --event events/event.json
Invoking app.lambdaHandler (nodejs14.x)
Local image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs14.x
Building image..............................................................................................................................................................
Using local image: public.ecr.aws/lambda/nodejs:14-rapid-x86_64.
Mounting /home/ubuntu/MyApp/lambda/sam-app/hello-world as /var/task:ro,delegated, inside runtime container
START RequestId: 4a6ba3fe-f24c-4f10-9840-e66c2abafc50 Version: $LATEST
2023-04-10T18:13:12.492Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'nRequire stack:n- /var/runtime/UserFunction.jsn- /var/runtime/Runtime.jsn- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:225:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:300:17)"," at Object.<anonymous> (/var/runtime/index.js:43:34)"," at Module._compile (internal/modules/cjs/loader.js:1114:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)"," at Module.load (internal/modules/cjs/loader.js:979:32)"," at Function.Module._load (internal/modules/cjs/loader.js:819:12)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)"," at internal/main/run_main_module.js:17:47"]}
10 Apr 2023 18:13:12,511 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
2023-04-10T18:13:12.641Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'nRequire stack:n- /var/runtime/UserFunction.jsn- /var/runtime/Runtime.jsn- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:225:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:300:17)"," at Object.<anonymous> (/var/runtime/index.js:43:34)"," at Module._compile (internal/modules/cjs/loader.js:1114:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)"," at Module.load (internal/modules/cjs/loader.js:979:32)"," at Function.Module._load (internal/modules/cjs/loader.js:819:12)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)"," at internal/main/run_main_module.js:17:47"]}
END RequestId: 2865c9f0-be40-47a2-b050-796ebd0aeaec
REPORT RequestId: 2865c9f0-be40-47a2-b050-796ebd0aeaec Init Duration: 0.65 ms Duration: 472.07 ms Billed Duration: 473 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'nRequire stack:n- /var/runtime/UserFunction.jsn- /var/runtime/Runtime.jsn- /var/runtime/index.js","trace":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:225:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:300:17)"," at Object.<anonymous> (/var/runtime/index.js:43:34)"," at Module._compile (internal/modules/cjs/loader.js:1114:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)"," at Module.load (internal/modules/cjs/loader.js:979:32)"," at Function.Module._load (internal/modules/cjs/loader.js:819:12)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)"," at internal/main/run_main_module.js:17:47"]}
注意,在sam init
步骤中,在此步骤中:
Select your starter template
1 - Hello World Example
2 - Hello World Example TypeScript
Template: 2
如果我选择1而不是2,并保持其他所有内容不变,我就不会得到这个错误。我将得到一个成功运行的测试消息。
怎么回事,AWS?
我能够按照您在成功创建sam应用程序时分享的步骤运行hello-world应用程序。
我使用了nodejs18.x并且确保docker引擎在本地运行。
我做的其他事情。
- 使用终端,进入sam-app/hello-world,运行
npm install
- 安装node_modules后,运行
tsc app.ts
进行编译。这将创建app.js文件。 - 然后运行
cd ..
到父文件夹,你有模板。yaml - 运行
sam local invoke --event events/event.json
.
命令日志
user@0.0.0.0 ts-helloworld % sam local invoke --event events/event.json
Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?
user@0.0.0.0 ts-helloworld % sam local invoke --event events/event.json
Invoking app.lambdaHandler (nodejs18.x)
Image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs18.x
Building image...............................................................................................................................................................................................................................................................................................................................................................
Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs18.x:rapid-1.66.0-x86_64.
Mounting /Users/user/app-repos/sam-ts-helloworld/ts-helloworld/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 47d04232-46f5-4c6c-89da-f0ec3eb66033 Version: $LATEST
{"statusCode":200,"body":"{"message":"hello world"}"}END RequestId: 47d04232-46f5-4c6c-89da-f0ec3eb66033
REPORT RequestId: 47d04232-46f5-4c6c-89da-f0ec3eb66033 Init Duration: 2.26 ms Duration: 1069.46 ms Billed Duration: 1070 ms Memory Size: 128 MB Max Memory Used: 128 MB