如何在 CodeBuild 中为 Lambda 函数添加 Python 库依赖项
How do I add a Python library dependency for a Lambda function in CodeBuild
我有一个 CodePipline,它从 CodeCommit 中抓取代码,将其捆绑在 CodeBuild 中,然后通过 CloudFormation 发布。
我想使用 Python 软件包 gspread,因为它不是标准 AWS Linux 映像的一部分,所以我需要安装它。
当前代码为 运行 我收到错误:
- [错误] Runtime.ImportModuleError:无法导入模块 'index':没有名为 'gspread'
的模块
代码结构
- buildspec.yml
- template.yml
package/
- gspread/
- gspread-3.6.0.dist-info/
- (37 other python packages)
source/
- index.py
buildspec.yml -- 已编辑
version: 0.2
phases:
install:
commands:
# Use Install phase to install packages or any pre-reqs you may need throughout the build (e.g. dev deps, security checks, etc.)
- echo "[Install phase]"
- pip install --upgrade pip
- pip install --upgrade aws-sam-cli
- sam --version
- cd source
- ls
- pip install --target . gspread oauth2client
# consider using pipenv to install everything in the environement and then copy the files installed into the /source folder
- ls
runtime-versions:
python: 3.8
pre_build:
commands:
# Use Pre-Build phase to run tests, install any code deps or any other customization before build
# - echo "[Pre-Build phase]"
build:
commands:
- cd ..
- sam build
post_build:
commands:
# Use Post Build for notifications, git tags and any further customization after build
- echo "[Post-Build phase]"
- export BUCKET=property-tax-invoice-publisher-deployment
- sam package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
- echo "SAM packaging completed on `date`"
##################################
# Build Artifacts to be uploaded #
##################################
artifacts:
files:
- outputtemplate.yml
discard-paths: yes
cache:
paths:
# List of path that CodeBuild will upload to S3 Bucket and use in subsequent runs to speed up Builds
- '/root/.cache/pip'
index.py 文件比这更多。但是要显示违规行。
--index.py--
import os
import boto3
import io
import sys
import csv
import json
import smtplib
import gspread #**<--- Right here**
def lambda_handler(event, context):
print("In lambda_handler")
我试过的
- 创建 /package 文件夹并提交 gspread 和其他包
- 运行 CodeBuild 构建中的“pip install gspread”:命令:
目前,我正在到处安装它,看看有什么用。 (目前没有任何问题)
版本:Python3.8
我认为您可能需要执行以下步骤:
使用虚拟环境在本地安装软件包。
创建 requirements.txt 让代码构建知道包要求。
在 CodeBuild buildspec.xml 中,包含安装虚拟环境的命令,然后提供 requirements.txt.
pre_build:
命令:
- pip 安装 virtualenv
- 虚拟环境
- 。 env/bin/activate
- pip 安装-r requirements.txt
详细步骤参考:
https://adrian.tengamnuay.me/programming/2018/07/01/continuous-deployment-with-aws-lambda/
我有一个 CodePipline,它从 CodeCommit 中抓取代码,将其捆绑在 CodeBuild 中,然后通过 CloudFormation 发布。
我想使用 Python 软件包 gspread,因为它不是标准 AWS Linux 映像的一部分,所以我需要安装它。
当前代码为 运行 我收到错误:
- [错误] Runtime.ImportModuleError:无法导入模块 'index':没有名为 'gspread' 的模块
代码结构
- buildspec.yml
- template.yml
package/
- gspread/
- gspread-3.6.0.dist-info/
- (37 other python packages)
source/
- index.py
buildspec.yml -- 已编辑
version: 0.2
phases:
install:
commands:
# Use Install phase to install packages or any pre-reqs you may need throughout the build (e.g. dev deps, security checks, etc.)
- echo "[Install phase]"
- pip install --upgrade pip
- pip install --upgrade aws-sam-cli
- sam --version
- cd source
- ls
- pip install --target . gspread oauth2client
# consider using pipenv to install everything in the environement and then copy the files installed into the /source folder
- ls
runtime-versions:
python: 3.8
pre_build:
commands:
# Use Pre-Build phase to run tests, install any code deps or any other customization before build
# - echo "[Pre-Build phase]"
build:
commands:
- cd ..
- sam build
post_build:
commands:
# Use Post Build for notifications, git tags and any further customization after build
- echo "[Post-Build phase]"
- export BUCKET=property-tax-invoice-publisher-deployment
- sam package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
- echo "SAM packaging completed on `date`"
##################################
# Build Artifacts to be uploaded #
##################################
artifacts:
files:
- outputtemplate.yml
discard-paths: yes
cache:
paths:
# List of path that CodeBuild will upload to S3 Bucket and use in subsequent runs to speed up Builds
- '/root/.cache/pip'
index.py 文件比这更多。但是要显示违规行。
--index.py--
import os
import boto3
import io
import sys
import csv
import json
import smtplib
import gspread #**<--- Right here**
def lambda_handler(event, context):
print("In lambda_handler")
我试过的
- 创建 /package 文件夹并提交 gspread 和其他包
- 运行 CodeBuild 构建中的“pip install gspread”:命令:
目前,我正在到处安装它,看看有什么用。 (目前没有任何问题)
版本:Python3.8
我认为您可能需要执行以下步骤:
使用虚拟环境在本地安装软件包。
创建 requirements.txt 让代码构建知道包要求。
在 CodeBuild buildspec.xml 中,包含安装虚拟环境的命令,然后提供 requirements.txt.
pre_build: 命令:
- pip 安装 virtualenv
- 虚拟环境
- 。 env/bin/activate
- pip 安装-r requirements.txt
详细步骤参考:
https://adrian.tengamnuay.me/programming/2018/07/01/continuous-deployment-with-aws-lambda/