Home  >  Article  >  Backend Development  >  How to call SAM from outside the `template` directory using Golang?

How to call SAM from outside the `template` directory using Golang?

WBOY
WBOYforward
2024-02-09 20:48:09642browse

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

php editor Youzi will introduce to you how to use Golang to call SAM from outside the `template` directory. SAM (Serverless Application Model) is a development framework for defining and deploying serverless applications, and Golang is an efficient and easy-to-use programming language. During the development process, we may need to call the SAM template from an external directory. This article will describe this process in detail. With the following steps, you will be able to use external SAM templates in Golang easily.

Question content

I have a multi-module project where one module is used to run integration tests and another module contains my application (to be tested). I want to run build and invoke from outside the sam template directory.

Use the command sam build -ttests/helloworld/template.yaml The runtime build works fine 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 call it fails as follows

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

Solution

I figured it out. Basically, sam build -t ... builds and generates artifacts in the current working directory. To run the built content we don't need to specify the template location but directly run sam local invoke

The above is the detailed content of How to call SAM from outside the `template` directory using Golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete