aws 部署python lambda_Deploy Python Lambda functions with .zip file archives - AWS Lambda

本文档介绍了如何创建.zip部署包来部署AWS Lambda的Python函数。Lambda支持两种类型的部署包:容器镜像和.zip文件。您需要确保.zip文件包含函数代码及其依赖,并且不超过50MB。如果超过限制,建议上传到Amazon S3。对于包含C扩展库的函数,推荐使用AWS SAM CLI。文中详细阐述了如何创建和更新带有或不带依赖的Python Lambda函数。
摘要由CSDN通过智能技术生成

Deploy Python Lambda functions with .zip file archives

Your AWS Lambda function's code consists of scripts or compiled programs and their

dependencies. You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages:

container images and .zip files.

You can use a built-in ZIP archive utility, or any other ZIP utility (such as 7zip) for your command line tool to create a deployment package.

The .zip file must contain your function's code, and any dependencies used to run

your function's code (if applicable) on Lambda. If your function depends only on standard

libraries, or AWS SDK libraries, you do not need to include the libraries in your

.zip file. These libraries are included with our supported Lambda runtime environments.

The .zip file must be less than 50 MB. If your .zip file is larger than 50 MB, we

recommend uploading it to an Amazon S3 bucket. For more information, see Using other AWS services to build a deployment package.

The .zip file can not contain libraries written in C or C++. If your .zip file contains

C-extension libraries, such as the Pillow (PIL) or numpy libraries, we recommend using

the AWS SAM CLI to build a deployment package. For more information, see Using other AWS services to build a deployment package.

This page describes how to create a .zip file as your deployment package, and then

use the .zip file to deploy your function code to Lambda using the AWS Command Line

Interface (AWS CLI). To upload your .zip file on the Lambda console, see Deployment packages.

Prerequisites

The following steps assume that you have created a Lambda function, and are updating

the deployment package for your function. If you haven't created a function yet, see

Building Lambda functions with Python.

The AWS Command Line Interface (AWS CLI) is an open source tool that enables you to

interact with AWS services using commands in your command-line shell. To complete

the steps in this section, you need the following:

Updating a function with no dependencies

The following steps show how to create a deployment package that contains only your

function code, and upload it to Lambda using the AWS CLI.

To update a Python function with no dependencies

Add function code files to the root of your deployment package.

~/my-function$ zip my-deployment-package.zip lambda_function.py

Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip

{

"FunctionName": "mylambdafunction",

"FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction",

"Runtime": "python3.8",

"Role": "arn:aws:iam::123456789012:role/lambda-role",

"Handler": "lambda_function.lambda_handler",

"CodeSize": 815,

"CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=",

"Version": "$LATEST",

"RevisionId": "d1e983e3-ca8e-434b-8dc1-7add83d72ebd",

...

}

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.

Updating a function with additional dependencies

If your Lambda function depends on libraries other than the AWS SDK for Python (Boto3), install the libraries to a local directory with pip, and include them in your deployment package (.zip file).

The following steps show how to install the requests libary, create a deployment package, and upload it to Lambda using the AWS CLI. The

steps assume that you are not using a virtual environment. It also assumes that your function code uses Python 3.8 and the python3.8 Lambda runtime.

Note

If you are creating a deployment package used in a layer, see Include library dependencies in a layer.

To update a Python function with dependencies

Install libraries in a package directory with pip's --target option.

~/my-function$ pip install --target ./package requests

Note

To prevent distutils errors on Debian-based systems such as Ubuntu, you may need to pass the --system

option.

Navigate to the package directory.

cd package

Create a deployment package with the installed libraries at the root.

~/my-function$ zip -r ../my-deployment-package.zip .

The last command saves the deployment package to the root of the my-function directory.

Navigate back to the my-function directory.

cd ..

Add function code files to the root of your deployment package.

~/my-function$ zip -g my-deployment-package.zip lambda_function.py

After you complete this step, you should have the following directory structure:

my-deployment-package.zip$

│ lambda_function.py

│ __pycache__

│ certifi/

│ certifi-2020.6.20.dist-info/

│ chardet/

│ chardet-3.0.4.dist-info/

...

Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip

{

"FunctionName": "mylambdafunction",

"FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction",

"Runtime": "python3.8",

"Role": "arn:aws:iam::123456789012:role/lambda-role",

"Handler": "lambda_function.lambda_handler",

"CodeSize": 2269409,

"CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=",

"Version": "$LATEST",

"RevisionId": "a9c05ffd-8ad6-4d22-b6cd-d34a00c1702c",

...

}

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.

Using a virtual environment

If your Lambda function depends on libraries other than the AWS SDK for Python (Boto3), install the libraries to a local directory with pip, and include them in your deployment package (.zip file).

The following steps show how to install the requests libary, create a deployment package, and upload it to Lambda using the AWS CLI. The

steps assume that you are using the virtualenv module for a virtual environment. It also assumes that your function code uses Python 3.8 and the python3.8 Lambda runtime.

Note

If you are creating a deployment package used in a layer, see Include library dependencies in a layer.

To update a Python function with a virtual environment

Create a virtual environment.

~/my-function$ virtualenv myvenv

Note

The virtualenv module uses Python 2.7 by default. You may need to add a local export path to your

command line profile, such as export VIRTUALENV_PYTHON=/usr/bin/python3.8 when using the virtualenv module with Python 3 and pip 3.

Activate the environment.

~/my-function$ source myvenv/bin/activate

Install libraries with pip.

(myvenv) ~/my-function$ pip install requests

Deactivate the virtual environment.

(myvenv) ~/my-function$ deactivate

Create a deployment package with the installed libraries at the root.

~/my-function$ cd myvenv/lib/python3.8/site-packages

~/my-function/myvenv/lib/python3.8/site-packages$ zip -r ../../../../my-deployment-package.zip .

The last command saves the deployment package to the root of the my-function directory.

Tip

A library may appear in site-packages or dist-packages and the first folder lib or lib64. You can use the pip show command to locate a specific package.

Add function code files to the root of your deployment package.

~/my-function/myvenv/lib/python3.8/site-packages$ cd ../../../../

~/my-function$ zip -g my-deployment-package.zip lambda_function.py

After you complete this step, you should have the following directory structure:

my-deployment-package.zip$

│ lambda_function.py

│ __pycache__

│ certifi/

│ certifi-2020.6.20.dist-info/

│ chardet/

│ chardet-3.0.4.dist-info/

...

Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip

{

"FunctionName": "mylambdafunction",

"FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction",

"Runtime": "python3.8",

"Role": "arn:aws:iam::123456789012:role/lambda-role",

"Handler": "lambda_function.lambda_handler",

"CodeSize": 5912988,

"CodeSha256": "A2P0NUWq1J+LtSbkuP8tm9uNYqs1TAa3M76ptmZCw5g=",

"Version": "$LATEST",

"RevisionId": "5afdc7dc-2fcb-4ca8-8f24-947939ca707f",

...

}

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值