我正在使用Go lang开发AWS Lambda。我正在部署Go lang代码,使用chromedp,使用Docker镜像,并获得WebSocket URL超时错误。我的lambda设置是3008 MB RAM内存,512MB存储,15分钟超时。你能找到什么是错的,如何解决这个问题?下面是文件main.go
和Dockerfile
文件main.go
(chromedp零件)
func getPage(URL string, lineNum string, stationNm string) {
// settings for crawling
ctx, cancle := chromedp.NewContext(
context.Background(),
chromedp.WithLogf(log.Printf),
)
defer cancle()
opts := []chromedp.ExecAllocatorOption{
chromedp.DisableGPU,
chromedp.NoSandbox,
chromedp.Headless,
chromedp.Flag("no-zygote", true),
chromedp.Flag("single-process", true),
chromedp.Flag("homedir", "/tmp"),
chromedp.Flag("data-path", "/tmp/data-path"),
chromedp.Flag("disk-cache-dir", "/tmp/cache-dir"),
chromedp.Flag("remote-debugging-port", "9222"),
chromedp.Flag("remote-debugging-address", "0.0.0.0"),
chromedp.Flag("disable-dev-shm-usage", true),
}
allocCtx, cancel := chromedp.NewExecAllocator(ctx, opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
var htmlContent string
ch := chromedp.WaitNewTarget(ctx, func(i *target.Info) bool {
return strings.Contains(i.URL, "/timetable/web/")
})
}
文件Dockerfile
FROM public.ecr.aws/lambda/provided:al2 AS build
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Get rid of the extension warning
RUN mkdir -p /opt/extensions
RUN yum -y install golang
RUN go env -w GOPROXY=direct
# Clone git, copying go.mod, go.sum, main.go
WORKDIR /var/task/
RUN yum install git -y
RUN git clone https://github.com/seedspirit/NaverCrawler-CICD-go.git
RUN cp NaverCrawler-CICD-go/main.go /var/task/
RUN cp NaverCrawler-CICD-go/go.mod /var/task/
RUN cp NaverCrawler-CICD-go/go.sum /var/task/
# cache dependencies
RUN go mod download
RUN go build -o main .
FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /var/task/main /var/task/main
# Install Chrome dependencies
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -o chrome.rpm && \
yum install -y ./chrome.rpm && \
yum install -y fontconfig libX11 GConf2 dbus-x11
ENTRYPOINT ["/var/task/main"]
1条答案
按热度按时间ac1kyiln1#
建议使用chromedp/headless-shell,因为它体积小,更适合AWS Lambda。
我刚刚用
chromedp/headless-shell
测试了一个简单的演示,它工作正常。Dockerfile:
main.go:
这个例子基于https://github.com/Andiedie/chromedp-aws-lambda-example。请注意,列出的
chromedp.ExecAllocatorOption
是直接从该存储库复制的。它工作,但我不确定这是否是最好的选择列表。也许你需要根据自己的需要来调整。