gpt4 book ai didi

docker - 如何将定制的docker容器与无服务器框架一起使用

转载 作者:行者123 更新时间:2023-12-02 09:30:41 25 4
gpt4 key购买 nike

我使用无服务器框架将 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

和我的 docker 文件

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

但貌似 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

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

最佳答案

AWS Lambda 支持使用 Docker 镜像作为函数的部署格式。这使您可以创建更复杂的运行时环境,更好地满足您的需求。

以下是如何配置 serverless.yml 文件以与 Docker 配合使用的示例:


service: your-service-name

provider:
name: aws
runtime: provided
lambdaHashingVersion: 20201221

functions:
hello:
image:
name: your-docker-image:tag
command:
- your.handler

在此示例中,your-docker-image:tag 是您要用于 Lambda 函数的 Docker 镜像的名称。您应该将其替换为您自己的 Docker 镜像的名称。

your.handler 是函数处理程序的路径。这应该是处理程序所在文件的名称和处理程序本身的名称,以点分隔。

需要注意的是,Docker 镜像需要上传到 AWS 的 Elastic Container Registry (ECR)。无服务器框架会自动处理此过程,但请确保您拥有使用 ECR 所需的权限。

另请注意,运行时设置为“提供”。这指示 AWS Lambda 使用您随 Docker 镜像提供的自定义运行时。

另一个重要细节是 lambdaHashingVersion:20201221。在 AWS Lambda 中使用容器镜像需要此选项,并表示正在使用新的哈希方案。

关于docker - 如何将定制的docker容器与无服务器框架一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com