如何使用无服务器框架的自定义docker容器

Hel*_*lad 7 docker aws-lambda serverless-framework serverless

我使用无服务器框架将python函数部署到aws lambda上

我的配置文件serverless.yml如下

frameworkVersion: "=1.27.3"

service: recipes

provider:
  name: aws
  endpointType: REGIONAL
  runtime: python3.6
  stage: dev
  region: eu-central-1
  memorySize: 512
  deploymentBucket:
    name: dfki-meta
  versionFunctions: false
  stackTags:
    Project: DFKIAPP
  # Allows updates to all resources except deleting/replacing EC2 instances
  stackPolicy:
    - Effect: Allow
      Principal: "*"
      Action: "Update:*"
      Resource: "*"
    - Effect: Deny
      Principal: "*"
      Action:
        - Update: Replace
        - Update: Delete
      Resource: "*"
      Condition:
        StringEquals:
          ResourceType:
            - AWS::EC2::Instance
  # Access to RDS and S3 Bucket
  iamRoleStatements:
    -  Effect: "Allow"
       Action: "s3:ListBucket"
       Resource: "*"

package:
  individually: true



functions:
  get_recipes:
    handler: handler.get_recipes
    module: recipes_crud
    package:
      include:
        - db/*
    timeout: 10
    events:
      - http:
          path: recipes
          method: get
          request:
            parameters:
              querystring:
                persona: true



plugins:
  # deploy conda package on lambda
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux
    dockerFile: prod_env_dockerfile/Dockerfile
Run Code Online (Sandbox Code Playgroud)

和我的码头文件

lambci/lambda:python3.6
FROM lambci/lambda-base:build

ENV PATH=/var/lang/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib \
    AWS_EXECUTION_ENV=AWS_Lambda_python3.6 \
    PYTHONPATH=/var/runtime \
    PKG_CONFIG_PATH=/var/lang/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig

RUN rm -rf /var/runtime /var/lang && \
  curl https://lambci.s3.amazonaws.com/fs/python3.6.tgz | tar -xz -C / && \
  sed -i '/^prefix=/c\prefix=/var/lang' /var/lang/lib/pkgconfig/python-3.6.pc && \
  curl https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz | tar -xJ && \
  cd Python-3.6.1 && \
  LIBS="$LIBS -lutil -lrt" ./configure --prefix=/var/lang && \
  make -j$(getconf _NPROCESSORS_ONLN) libinstall inclinstall && \
  cd .. && \
  rm -rf Python-3.6.1 && \
  pip3 install -U pip awscli virtualenv --no-cache-dir

RUN yum install -y wget
RUN wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
RUN bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda
RUN export PATH="$HOME/miniconda/bin:$PATH" && conda install -c prometeia -y pymssql
Run Code Online (Sandbox Code Playgroud)

但看似sls不使用我的dockerfile,它仍然会创建一个名为sls-py-reqs-custom的图像

(node:43146) ExperimentalWarning: The fs.promises API is experimental
Serverless: Installing requirements of recipes_crud/requirements.txt in .serverless/recipes_crud...
Serverless: Building custom docker image from prod_env_dockerfile/Dockerfile...
Serverless: Docker Image: sls-py-reqs-custom
Serverless: Packaging function: get_recipes...
Serverless: Excluding development dependencies...
Serverless: Injecting required Python packages to package...
Serverless: Uploading function: get_recipes (29.08 MB)...
Serverless: Successfully deployed function: get_recipes
Serverless: Successfully updated function: get_recipes
Run Code Online (Sandbox Code Playgroud)

如何强制无服务器使用我的自定义docker?

Sea*_*ine 0

你似乎有些困惑。我想主要解决您最初问题中的这两条评论:

  • 但似乎 sls 不使用我的 dockerfile
  • 如何强制无服务器使用我定制的 docker ?

TL;DR:无服务器框架不会使用您的 Dockerfile,您也不能强制它使用。这两种技术就像苹果和橘子。要解决此问题,您serverless.yaml必须简单地配置为查找函数处理程序的路径。

您正在使用名为docker-lambda的流行 Docker 映像。该图像仅用于本地测试。我能想到的最好的用例是它可以在没有互联网连接的情况下使用(露营时编码、在没有 WiFi 的飞机上等)。

引用该项目的自述文件,该图像的唯一目的是:

使用它在相同严格的 Lambda 环境中运行您的函数,并知道它们在实时部署时会表现出相同的行为。您还可以使用它来编译本机依赖项,​​因为您知道您链接到 AWS Lambda 上存在的相同库版本,然后使用 AWS CLI 进行部署。

当您准备好打包/部署等时。对于AWS云来说,这docker-lambda对你来说是零用处。