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

huangapple go评论70阅读模式
英文:

How to SAM invoke from outside of the `template` directory with Golang?

问题

我有一个多模块项目,其中一个模块用于运行集成测试,而另一个模块包含我的应用程序(需要进行测试)。我希望能够从 SAM 模板目录外部运行 buildinvoke

当我使用以下命令运行构建时,构建工作正常:

~/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

构建成功

构建的构件:.aws-sam/build
构建的模板:.aws-sam/build/template.yaml

下一步可以使用的命令
=========================
[*] 验证 SAM 模板:sam validate
[*] 调用函数:sam local invoke
[*] 在云中测试函数:sam sync --stack-name {{stack-name}} --watch
[*] 部署:sam deploy --guided

但是,当我尝试调用时,出现以下错误:

~/W/r/sommelier (feature-integrationTests|✔) $ sam local invoke -t tests/helloworld/template.yaml
正在调用 bootstrap (provided.al2)
跳过拉取镜像并使用本地镜像:public.ecr.aws/sam/emulation-provided.al2:rapid-1.70.0-arm64。

将 /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations 挂载为 /var/task:ro,delegated 在运行时容器内部
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=
函数 'HelloWorldFunction'5 秒后超时
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	
HelloWorldFunction 的调用容器没有响应


我应该怎么做才能实现这个?

英文:

I am having a multi-module project where one of my module is for running integration tests, while another module has my application(to be tested). I want to run build and invoke from outside of SAM template directory.

Build worked fine when ran with the command sam build -t tests/helloworld/template.yaml as shown below

~/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

But when I try to invoke, it fails as following

~/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


What should I do to achieve this?

答案1

得分: 1

我明白了。基本上,sam build -t ......命令会在当前工作目录中构建并生成构件。而要运行已构建的内容,我们无需指定模板位置,直接使用sam local invoke命令即可。

英文:

Figured it out. Basically, sam build -t ...... builds and generate artefacts in the present working directory. And to run what has been build we need not specify the template location but directly run like sam local invoke

huangapple
  • 本文由 发表于 2023年1月24日 17:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75219860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定