首页 > 文章列表 > 使用 Golang 如何在 `template` 目录外部调用 SAM?

使用 Golang 如何在 `template` 目录外部调用 SAM?

169 2024-04-29
问题内容

我有一个多模块项目,其中一个模块用于运行集成测试,而另一个模块则包含我的应用程序(待测试)。我想从 sam 模板目录外部运行 buildinvoke

使用命令 sam build -ttests/helloworld/template.yaml 运行时构建工作正常,如下所示

~/w/r/sommelier (feature-integrationtests|✔) $ sam build -t tests/helloworld/template.yaml
building codeuri: /users/kuldeep/work/repos/sommelier/lambdas/import-recommendations runtime: provided.al2 metadata: {'buildmethod': 'go1.x'} architecture: arm64 functions: helloworldfunction
running gomodulesbuilder:build

build succeeded

built artifacts  : .aws-sam/build
built template   : .aws-sam/build/template.yaml

commands you can use next
=========================
[*] validate sam template: sam validate
[*] invoke function: sam local invoke
[*] test function in the cloud: sam sync --stack-name {{stack-name}} --watch
[*] deploy: sam deploy --guided

但是当我尝试调用时,它失败如下

~/W/r/sommelier (feature-integrationTests|✔) $ sam local invoke -t tests/helloworld/template.yaml
Invoking bootstrap (provided.al2)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-provided.al2:rapid-1.70.0-arm64.

Mounting /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations as /var/task:ro,delegated inside runtime container
START RequestId: 53716d16-562c-42f1-bad7-b415e27004b3 Version: $LATEST
24 Jan 2023 09:56:03,133 [ERROR] (rapid) Init failed error=fork/exec /var/task/bootstrap: no such file or directory InvokeID=
Function 'HelloWorldFunction' timed out after 5 seconds
END RequestId: 5eeafe42-303b-40b4-bf96-d74809bb8636
REPORT RequestId: 5eeafe42-303b-40b4-bf96-d74809bb8636  Init Duration: 0.13 ms  Duration: 5000.00 ms    Billed Duration: 5000 ms    Memory Size: 128 MB Max Memory Used: 128 MB 
No response from invoke container for HelloWorldFunction


我应该怎样做才能实现这个目标?


正确答案


想通了。基本上, sam build -t ...... 在当前工作目录中构建并生成工件。要运行已构建的内容,我们不需要指定模板位置,而是直接运行 sam local invoke