lambda运行两次,并且运行时没有提供原因而退出。

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

lambda running twice, and Runtime exited without providing a reason

问题

我在一个容器中运行的Go编写的lambda函数,镜像是使用alpine-golang构建的,并在alpine上运行。

在测试过程中,我注意到从日志中可以看到lambda在退出之前运行了两次,并显示以下错误:

Error: Runtime exited without providing a reason Runtime.ExitError

从我的本地系统来看,这段代码运行良好,没有错误。我之前尝试在没有容器的情况下运行,但仍然遇到运行时问题。我的代码中唯一的错误处理和日志记录机制是log.Printlnfmt.Printf。有人知道发生了什么吗?

编辑:

我捕获了退出代码,结果是0,但lambda退出时显示了以下错误:

Runtime exited with error: exit status 1 Runtime.ExitError
英文:

I got a lambda written in Go running on a container, the image was built with alpine-golang and run with alpine.

When testing i noticed from the logs the lambda is ran twice before exiting with the following:

Error: Runtime exited without providing a reason Runtime.ExitError

From my local system this the code runs fine without errors, i earlier tried running without a container but still faced runtime issues. The only error handling and logging mechs in my code is log.Println and fmt.Printf. Anyone got an idea of what is going on?

EDIT:

I trapped the exit code, which is turns out to be 0 but lambda exits with

 Runtime exited with error: exit status 1 Runtime.ExitError

答案1

得分: 1

我强烈建议选择“无容器”路径。只需将可执行文件打包成.zip归档文件即可。不要忘记使用GOOS=linux编译代码,以使其与AWS Lambda兼容。

在Linux上,您可以使用以下命令获取您的归档文件:

GOOS=linux go build -o 可执行文件名 路径/到/main.go
zip 归档文件名 可执行文件名

请注意,在函数的运行时设置中,您必须将Handler设置为可执行文件名

要处理Lambda函数,您必须使用github.com/aws/aws-lambda-go/lambda包,并在主函数中启动处理程序函数,如lambda.Start(handler)

完整的代码示例:

package main

import (
	"context"
	"log"

	"github.com/aws/aws-lambda-go/lambda"
)

func main() {
	lambda.Start(handler)
}

func handler(ctx context.Context) {
	log.Println("成功执行")
}
英文:

I really suggest going for the "without container" path. Just pack your executable into a .zip archive. Don't forget to compile with GOOS=linux for your code to be compatible with AWS Lambda.

On Linux you can use the following commands to get your archive:

GOOS=linux go build -o executableName path/to/main.go
zip archive.zip executableName

Note that you have to set Handler to be executableName in function's Runtime settings.

For handling lambda function, you have to use github.com/aws/aws-lambda-go/lambda package and in main start the handler function like lambda.Start(handler).

Full code example:

package main

import (
	"context"
	"log"

	"github.com/aws/aws-lambda-go/lambda"
)

func main() {
	lambda.Start(handler)
}

func handler(ctx context.Context) {
	log.Println("successfully executed")
}

答案2

得分: 0

请确保您遵循AWS提供的构建容器镜像的推荐指南。您可以在以下链接中找到相关文档:https://docs.aws.amazon.com/lambda/latest/dg/go-image.html

为了使Dockerfile与Lambda一起工作,它应该如下所示:

FROM public.ecr.aws/lambda/provided:al2 as build
# 安装编译器
RUN yum install -y golang
RUN go env -w GOPROXY=direct
# 缓存依赖项
ADD go.mod go.sum ./
RUN go mod download
# 构建
ADD . .
RUN go build -o /main
# 将构建产物复制到干净的镜像中
FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /main /main
ENTRYPOINT ["/main"]

Lambda在某种程度上很奇怪,如果您的Dockerfile在本地机器上运行良好,那么它会运行一次,然后结束,然后再运行一次并且没有给出任何原因就崩溃了。

英文:

Make sure you are following the recommended guide lines aws provide with building the container image. https://docs.aws.amazon.com/lambda/latest/dg/go-image.html

your Dockerfile should look like this to work with lambda,

FROM public.ecr.aws/lambda/provided:al2 as build
# install compiler
RUN yum install -y golang
RUN go env -w GOPROXY=direct
# cache dependencies
ADD go.mod go.sum ./
RUN go mod download
# build
ADD . .
RUN go build -o /main
# copy artifacts to a clean image
FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /main /main
ENTRYPOINT [ "/main" ]  

Lambda is very strange where if you have the Dockerfile like you would on a local machine then it runs it once, ends, then a second time and crashes with no reason given

huangapple
  • 本文由 发表于 2022年9月27日 02:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/73858389.html
匿名

发表评论

匿名网友

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

确定