我在 Golang 中开发了一组 Lambda 函数,并尝试使用 SAM 部署这些函数和 API 网关。
我正在本地创建可执行文件,为每个 lambda 函数创建 zip 文件,并将这些 zip 文件上传到 s3 存储桶。
我在 SAM 模板文件中给出了这个 S3 存储桶的引用。
我的 SAM 模板文件如下所示
myfunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://<<my-bucket>>/bin/handlers/myfunction.zip
Handler: myfunction
Role: !GetAtt CFLambdaExecutionRole.Arn
Events:
Getcfdemoapi:
Type: Api
Properties:
Path: /myfunction
Method: get
CreateCustomer:
Type: Api
Properties:
Path: /myfunction
Method: post
UpdateCustomer:
Type: Api
Properties:
Path: /customer
Method: put
DeleteCustomer:
Type: Api
Properties:
Path: /myfunction
Method: delete
部署成功。
我通过 API 网关调用了 lambda 函数。
我检查了 Cloud watch 日志并看到以下错误 -
Error fork/exec /var/task/myfunction : no such file or directory: PathError
SAM 模板中是否有与 CodeUri 和处理程序相关的错误?
我正在 MacOS 上创建构建并使用以下命令进行构建 -
GOOS=linux GOARCH=amd64 go build
最佳答案
此特定错误可能由许多问题引起。我想到的三个是:
myfunction
(您使用 set Handler: myfunction
因此,二进制文件必须命名为 myfunction
)要解决此问题,您可以在包含 Lambda 函数代码的目录中运行以下命令。
第一步:构建二进制文件并将名称显式设置为
myfunction
GOOS=linux GOARCH=amd64 go build -o myfunction
如果不使用 -o
选项二进制文件将以您正在构建的目录命名。因此,如果目录本身不是名为 myfunction
, 生成的二进制文件将具有错误的名称(请参阅上面可能的问题 #1)。第 2 步:创建 ZIP 存档
zip --junk-paths myfunction.zip myfunction
--junk-paths
选项将删除路径并将二进制文件放在 ZIP 存档的“根”中。 man page说:Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).
这是为了避免我上面列出的问题#3。
要检查这是否按预期工作,您可以运行以下命令:
unzip -l myfunction.zip
这应该显示如下内容:Archive: myfunction.zip
Length Date Time Name
--------- ---------- ----- ----
2142872 12-01-2020 19:19 myfunction
--------- -------
2142872 1 file
第 3 步:将 ZIP 存档上传到 S3要将 ZIP 存档上传到 S3,我建议您使用 AWS CLI。 AWS 控制台也能正常工作。
使用 AWS CLI,您可以运行以下命令:
aws s3 cp myfunction.zip s3://<<my-bucket>>/bin/handlers/myfunction.zip
此文档 cp
命令可以在这里找到:cp documentation .
关于amazon-web-services - AWS SAM 部署 : Error fork/exec/var/task/myfunction : no such file or directory: PathError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286543/