简体   繁体   English

无法使用 Linux Alpine docker 为 AWS lambda 构建 zip 文件

[英]Can't build zip file for AWS lambda using Linux Alpine docker

I have Dockerfile based on Alpine linux that builds lambda.zip file for AWS Lambda. Here's Dockerfile:我有基于 Alpine linux 的 Dockerfile,它为 AWS Lambda 构建了 lambda.zip 文件。这是 Dockerfile:

FROM alpine:3.12

# -- Install OS packages:
RUN apk add gcc
RUN apk add --update --no-cache \
    bash \
    build-base \
    cargo \
    curl \
    docker \
    # gcc \
    git \
    g++ \
    lftp \
    libc-dev \
    libffi-dev \
    libsodium-dev \
    libxslt-dev\
    libzmq \ 
    zeromq-dev \
    make \
    musl-dev \
    ncftp \
    nodejs \
    npm \
    openssh-client \
    openssl \
    openssl-dev \
    rsync \
    su-exec \
    tar \
    wget \
    zip 
# geos \ 
# geos-dev \
# libc-dev 

WORKDIR /tmp/

RUN echo "http://mirror.leaseweb.com/alpine/edge/community" >> /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk add --virtual .build-deps \
    --repository http://dl-cdn.alpinelinux.org/alpine/edge/community \
    --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
    libc-dev geos-dev geos && \
    runDeps="$(scanelf --needed --nobanner --recursive /usr/local \
    | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
    | xargs -r apk info --installed \
    | sort -u)" && \
    apk add --virtual .rundeps $runDeps

RUN geos-config --cflags

# -- Install python:
RUN apk add --update --no-cache python3-dev python3 \
    && python3 -m ensurepip --upgrade \
    && pip3 install --upgrade pip pipenv setuptools docker-compose awscli shapely wheel \
    && rm -r /usr/lib/python*/ensurepip  \
    && if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi \
    && if [[ ! -e /usr/bin/python ]]; then ln

Here's script that works on docker Alpine image and builds lambda.zip that contains python files and all needed dependencies like boto3, pip, six etc.这是适用于 docker Alpine 图像并构建 lambda.zip 的脚本,其中包含 python 文件和所有需要的依赖项,如 boto3、pip、六个等。

 
#!/bin/bash

set -x
set -e

pipenv install
rm -rf lambda.zip

VENV=$(pipenv --venv)
TEMPDIR=$(mktemp -d)

rsync -r --progress ./* ${TEMPDIR}
rsync -r --progress ${VENV}/lib/python3.8/site-packages/* ${TEMPDIR}

pushd ${TEMPDIR}
zip -r lambda.zip ./*
popd

cp ${TEMPDIR}/lambda.zip ./

cleanup() {
    rm -rf ${TEMPDIR}
}

trap cleanup EXIT

Then I copy this lambda.zip to my local machine, unzip it and enable pipenv on my local machine to use only that packages in lambda.zip and i try to test Shapely package, for example test.py:然后我将这个 lambda.zip 复制到我的本地机器,解压它并在我的本地机器上启用 pipenv 以仅使用 lambda.zip 中的那些包我尝试测试 Shapely package,例如 test.py:

from shapely.geometry import Polygon
print(Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]).minimum_clearance)

On AWS lambda and on my local machine I am getting same error with geos_c After:在 AWS lambda 和我的本地机器上,我在 geos_c 之后遇到了同样的错误:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from shapely.geometry import Polygon
  File "/home/stark/code/job/lamba_python_312_stack/shapely/geometry/__init__.py", line 4, in <module>
    from .base import CAP_STYLE, JOIN_STYLE
  File "/home/stark/code/job/lamba_python_312_stack/shapely/geometry/base.py", line 19, in <module>
    from shapely.coords import CoordinateSequence
  File "/home/stark/code/job/lamba_python_312_stack/shapely/coords.py", line 8, in <module>
    from shapely.geos import lgeos
  File "/home/stark/code/job/lamba_python_312_stack/shapely/geos.py", line 87, in <module>
    _lgeos = load_dll('geos_c', fallbacks=alt_paths)
  File "/home/stark/code/job/lamba_python_312_stack/shapely/geos.py", line 60, in load_dll
    raise OSError(
OSError: Could not find lib geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'].

If I unzip file, I got something like these:如果我解压缩文件,我会得到如下内容:

@user# ls

iohttp
aiohttp-3.7.4.post0.dist-info
attr
attrs-21.2.0.dist-info
boto3
boto3-1.18.38.dist-info
botocore
cron_runner.py
setuptools-58.0.4.virtualenv
shapely
Shapely-1.7.1.dist-info
...
xmltodict.py
yarl
yarl-1.6.3.dist-info

I tried a lot of different solutions on Inte.net so you can see a mess in my Dockerfile, but they didn't work.我在 Inte.net 上尝试了很多不同的解决方案,因此您可以在我的 Dockerfile 中看到一团糟,但它们都没有用。

You are installing some statically compiled dependencies in your Docker environment, like libc-dev , geos-dev , and geos .您正在 Docker 环境中安装一些静态编译的依赖项,例如libc-devgeos-devgeos You have to also include those static dependencies in the Lambda deployment zip file.您还必须在 Lambda 部署 zip 文件中包含那些 static 依赖项。 Also, to include statically compiled dependencies for use in AWS Lambda you have to use the same operating system Lambda uses, which is Amazon Linux, not Alpine Linux.此外,要包含在 AWS Lambda 中使用的静态编译依赖项,您必须使用与 Lambda 相同的操作系统,即 Amazon Linux,而不是 Alpine Linux。


Luckily there are two alternatives now that make this much easier:幸运的是,现在有两种选择可以使这更容易:

Lambda Layers Lambda层

Lambda Layers are Lambda dependencies that can be packaged up in a reusable method, that can also be shared with other developers. Lambda 层是 Lambda 依赖项,可以以可重用的方法打包,也可以与其他开发人员共享。 In this case someone has already created a shapely Lambda Layer (and someone else here ) that you can simply include in your Lambda function instead of trying to package it yourself.在这种情况下,有人已经创建了一个匀称的 Lambda 层这里还有其他人),您可以简单地将其包含在您的 Lambda function 中,而不是自己尝试 package 。

If you still want to build it yourself you could look at that project's source code to see how they are building the layer.如果您仍然想自己构建它,您可以查看该项目的源代码以了解他们是如何构建该层的。

Lambda Containers Lambda 集装箱

Instead of creating a zip deployment, you could create a Docker image and deploy it to Lambda. You do have to implement a specific interface inside your Lambda container if you go this route, and it is easiest to do this by starting with one of the official AWS Lambda base images .您可以创建一个 Docker 映像并将其部署到 Lambda,而不是创建 zip 部署。如果您使用 go 这条路线,您必须在 Lambda 容器内实现一个特定接口,最简单的方法是从以下之一开始官方 AWS Lambda 基本图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 AWS lambda 中验证和解压缩 zip 文件 - Validating and unzipping a zip file in AWS lambda 发布到 AWS Lamba - 找不到“build-lambda-zip”实用程序 - Publish to AWS Lamba - Failed to find the "build-lambda-zip" utility AWS Lambda 读取作为源代码上传的 zip 中的文件内容 - AWS Lambda read contents of file in zip uploaded as source code AWS Python Lambda 无法在 zip 上构建的文件中导入模块 CodeBuild - AWS Python Lambda Unable to Import Module In zip File constructed on CodeBuild AWS Lambda“没有名为‘grpc’的模块”,尽管它包含在 function zip 文件中 - AWS Lambda "no module named 'grpc'" despite including it in the function zip file 无服务器:无法使用部署命令在 AWS 上加载 lambda 函数 - Serverless: can't load lambda function on the AWS using deploy comand AWS Lambda 无法从 Lambda 层找到 pyyaml - AWS Lambda can't find pyyaml from Lambda layer 在使用 Node 的 AWS Lambda 中,无法让 Express 与 aws-serverless-express 一起使用 - In AWS Lambda using Node, can't get Express to work with aws-serverless-express 如何使用 docker 图像在 aws lambda 中安装 gifsicle - How to install gifsicle in aws lambda using docker image AWS Lambda - 无法在 lambda 函数中获取 Cognito 用户数据 - AWS Lambda - Can't get Cognito user data in lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM