使用 SAM 使用自定义 python 函数构建 AWS Lambda 层
Building AWS Lambda layers with custom python functions using SAM
我正在尝试使用 SAM 构建我的 lambda 函数。我的 lambda 函数依赖于我计划构建为 AWS Lambda 层的自定义 python 函数。我的自定义 python 函数对 PyPI 上公开可用的 python 包具有传递依赖性,我在层的 requirements.txt 文件中指定了该包。
这是我的 lambda 函数的文件夹结构:
my-lambda-func
|-- events
|-- event.json
|-- my-func
|-- my_function.py
|-- __init__.py
|-- requirements.txt
|-- my-layer
|-- my_layer.py
|-- requirements.txt
|-- template.yaml
|-- buildspec.yml
这是我的 template.yaml 文件:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-test
Sample SAM Template for sam-test
Globals:
Function:
Timeout: 300
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: my-func/
Handler: app.lambda_handler
Layers:
- !Ref MyLayer
Runtime: python3.8
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: MyLayer
Description: My custom Lambda Layer
ContentUri: ./my-layer/
CompatibleRuntimes:
- python3.8
LicenseInfo: MIT
RetentionPolicy: Retain
Metadata:
BuildMethod: python3.8
我的构建规范文件:
version: 0.2
phases:
build:
commands:
- sam package --template-file template.yaml --s3-bucket my-bucket --output-template-file packaged-template.yml
artifacts:
files:
- packaged-template.yml
当我 运行 在本地执行 sam build
命令时,我看到正确创建了 2 个资源,包括拉取该层的传递依赖项,但是当我使用构建阶段设置 CodePipeline 以构建命令时从 builspec 文件和使用 CloudFormation 的部署阶段,该层没有按我预期的那样构建。当我从控制台下载图层时,它没有显示任何传递依赖项。
有没有人做过类似的事情。有人可以帮助我做错什么吗?谢谢!
从未使用过 Lambda 层,但吸引了我的眼球 w.r.t。我的buildspec.yml是你不是运行sam build
打包前。这是我的构建规范文件,也许它有帮助
phases:
install:
runtime-versions:
python: 3.7
build:
commands:
- pip install --upgrade aws-sam-cli
- sam build
- sam package --output-template-file packaged.yaml --s3-bucket ${FUNCTIONS_BUCKET}
artifacts:
type: zip
files:
- packaged.yaml
Stefano 是正确的,所有你包装的层都是定义。打包前需要运行sam build
我正在尝试使用 SAM 构建我的 lambda 函数。我的 lambda 函数依赖于我计划构建为 AWS Lambda 层的自定义 python 函数。我的自定义 python 函数对 PyPI 上公开可用的 python 包具有传递依赖性,我在层的 requirements.txt 文件中指定了该包。
这是我的 lambda 函数的文件夹结构:
my-lambda-func
|-- events
|-- event.json
|-- my-func
|-- my_function.py
|-- __init__.py
|-- requirements.txt
|-- my-layer
|-- my_layer.py
|-- requirements.txt
|-- template.yaml
|-- buildspec.yml
这是我的 template.yaml 文件:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-test
Sample SAM Template for sam-test
Globals:
Function:
Timeout: 300
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: my-func/
Handler: app.lambda_handler
Layers:
- !Ref MyLayer
Runtime: python3.8
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: MyLayer
Description: My custom Lambda Layer
ContentUri: ./my-layer/
CompatibleRuntimes:
- python3.8
LicenseInfo: MIT
RetentionPolicy: Retain
Metadata:
BuildMethod: python3.8
我的构建规范文件:
version: 0.2
phases:
build:
commands:
- sam package --template-file template.yaml --s3-bucket my-bucket --output-template-file packaged-template.yml
artifacts:
files:
- packaged-template.yml
当我 运行 在本地执行 sam build
命令时,我看到正确创建了 2 个资源,包括拉取该层的传递依赖项,但是当我使用构建阶段设置 CodePipeline 以构建命令时从 builspec 文件和使用 CloudFormation 的部署阶段,该层没有按我预期的那样构建。当我从控制台下载图层时,它没有显示任何传递依赖项。
有没有人做过类似的事情。有人可以帮助我做错什么吗?谢谢!
从未使用过 Lambda 层,但吸引了我的眼球 w.r.t。我的buildspec.yml是你不是运行sam build
打包前。这是我的构建规范文件,也许它有帮助
phases:
install:
runtime-versions:
python: 3.7
build:
commands:
- pip install --upgrade aws-sam-cli
- sam build
- sam package --output-template-file packaged.yaml --s3-bucket ${FUNCTIONS_BUCKET}
artifacts:
type: zip
files:
- packaged.yaml
Stefano 是正确的,所有你包装的层都是定义。打包前需要运行sam build