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

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

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

问题

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

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

  1. ~/W/r/sommelier (feature-integrationTests|✔) $ sam build -t tests/helloworld/template.yaml
  2. Building codeuri: /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations runtime: provided.al2 metadata: {'BuildMethod': 'go1.x'} architecture: arm64 functions: HelloWorldFunction
  3. Running GoModulesBuilder:Build
  4. 构建成功
  5. 构建的构件:.aws-sam/build
  6. 构建的模板:.aws-sam/build/template.yaml
  7. 下一步可以使用的命令
  8. =========================
  9. [*] 验证 SAM 模板:sam validate
  10. [*] 调用函数:sam local invoke
  11. [*] 在云中测试函数:sam sync --stack-name {{stack-name}} --watch
  12. [*] 部署:sam deploy --guided

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

  1. ~/W/r/sommelier (feature-integrationTests|✔) $ sam local invoke -t tests/helloworld/template.yaml
  2. 正在调用 bootstrap (provided.al2)
  3. 跳过拉取镜像并使用本地镜像:public.ecr.aws/sam/emulation-provided.al2:rapid-1.70.0-arm64
  4. /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations 挂载为 /var/task:ro,delegated 在运行时容器内部
  5. START RequestId: 53716d16-562c-42f1-bad7-b415e27004b3 Version: $LATEST
  6. 24 Jan 2023 09:56:03,133 [ERROR] (rapid) Init failed error=fork/exec /var/task/bootstrap: no such file or directory InvokeID=
  7. 函数 'HelloWorldFunction' 5 秒后超时
  8. END RequestId: 5eeafe42-303b-40b4-bf96-d74809bb8636
  9. 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
  10. 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

  1. ~/W/r/sommelier (feature-integrationTests|✔) $ sam build -t tests/helloworld/template.yaml
  2. Building codeuri: /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations runtime: provided.al2 metadata: {'BuildMethod': 'go1.x'} architecture: arm64 functions: HelloWorldFunction
  3. Running GoModulesBuilder:Build
  4. Build Succeeded
  5. Built Artifacts : .aws-sam/build
  6. Built Template : .aws-sam/build/template.yaml
  7. Commands you can use next
  8. =========================
  9. [*] Validate SAM template: sam validate
  10. [*] Invoke Function: sam local invoke
  11. [*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
  12. [*] Deploy: sam deploy --guided

But when I try to invoke, it fails as following

  1. ~/W/r/sommelier (feature-integrationTests|✔) $ sam local invoke -t tests/helloworld/template.yaml
  2. Invoking bootstrap (provided.al2)
  3. Skip pulling image and use local one: public.ecr.aws/sam/emulation-provided.al2:rapid-1.70.0-arm64.
  4. Mounting /Users/kuldeep/Work/repos/sommelier/lambdas/import-recommendations as /var/task:ro,delegated inside runtime container
  5. START RequestId: 53716d16-562c-42f1-bad7-b415e27004b3 Version: $LATEST
  6. 24 Jan 2023 09:56:03,133 [ERROR] (rapid) Init failed error=fork/exec /var/task/bootstrap: no such file or directory InvokeID=
  7. Function 'HelloWorldFunction' timed out after 5 seconds
  8. END RequestId: 5eeafe42-303b-40b4-bf96-d74809bb8636
  9. 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
  10. 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:

确定