亚马逊 Lambda Python 图书馆
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
这个库为Python Lambda函数提供构造.
要使用此模块, 您需要安装 Docker .
Python 函数
定义 a PythonFunction数字 :
new lambda.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/function', // required
runtime: Runtime.PYTHON_3_8, // required
index: 'my_index.py', // optional, defaults to 'index.py'
handler: 'my_exported_func', // optional, defaults to 'handler'
});
所有其他属性 lambda.Function 支持,另见 AWS Lambda 构建库。 。 。 .
Python 层
您可以创建基于蟒的羊肉地层 PythonLayerVersion如果 PythonLayerVersion 检测到 a requirements.txt
或者说 Pipfile 或者说 poetry.lock 页:1 pyproject.toml 在进入路径,然后 PythonLayerVersion 将会在
层.
定义 a PythonLayerVersion数字 :
new lambda.PythonLayerVersion(this, 'MyLayer', {
entry: '/path/to/my/layer', // point this to your library's directory
})
一个地层也可以用作一个地层的一部分 PythonFunction数字 :
new lambda.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/function',
runtime: Runtime.PYTHON_3_8,
layers: [
new lambda.PythonLayerVersion(this, 'MyLayer', {
entry: '/path/to/my/layer', // point this to your library's directory
}),
],
});
包装
若为 requirements.txt, (中文(简体) ), Pipfile 或者说 poetry.lock 在输入路径中存在,构造将处理安装所有所需的模块 Lambda 兼容多克容器 根据 runtime 而与多克平台基于Lambda功能的目标架构.
Python 捆绑只有在源目录中的文件有变化时才会被再创建并发布. 因此(并作为一项一般最佳做法),强烈建议采用一个锁箱文件 列出所有过渡性依赖及其准确版本。 这将确保当任何依赖版本更新时,捆绑资产被重新创建并上传.
为此,我们建议使用[pipenv或[poetry有锁文件支持.
使用 Packaging 类,其中:
- 根据当前文件推断出包装类型.
- 如果它看到一个
Pipfile或一个poetry.lock文件,它导出为兼容requirements.txt带有证书的文件(如果在源文件或捆绑容器中可以提供的话). - 使用
pip。 。 。 . - 将依赖性复制到捆绑在Lambda包上的资产中.
有要求的Lambda(txt)
.
├── lambda_function.py # exports a function named 'handler'
├── requirements.txt # has to be present at the entry path
带管子的Lambda
.
├── lambda_function.py # exports a function named 'handler'
├── Pipfile # has to be present at the entry path
├── Pipfile.lock # your lock file
有诗歌的Lambda 洛克
.
├── lambda_function.py # exports a function named 'handler'
├── pyproject.toml # your poetry project definition
├── poetry.lock # your poetry lock file has to be present at the entry path
自定义捆绑
自定义捆绑可以通过在附加的构建参数中通过指向将 URL 索引到私有重置中,或者使用一个完全自定义的多克图像来捆绑依赖关系来实现. 目前支持的建设参数有:
PIP_INDEX_URLPIP_EXTRA_INDEX_URLHTTPS_PROXY
提及 PyPI 索引的捆绑附加构建参数可以指定为:
const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);
new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
buildArgs: { PIP_INDEX_URL: "https://your.index.url/simple/", PIP_EXTRA_INDEX_URL: "https://your.extra-index.url/simple/" },
},
});
如果使用自定义的 Docker 图像来捆绑,则依赖性将安装在 pip, (中文(简体) ), pipenv 或者说 poetry 使用 Packaging 课。 与函数在同一目录中的不同的捆绑多克图像可以指定为:
const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);
new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: { image },
});
自定义装入密码
要使用密码Artifact PyPI repo,请输入 PIP_INDEX_URL 用于捆绑函数的可定制(在构建环境中需要AWS CLI):
import { execSync } from 'child_process';
const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);
const domain = 'my-domain';
const domainOwner = '111122223333';
const repoName = 'my_repo';
const region = 'us-east-1';
const codeArtifactAuthToken = execSync(`aws codeartifact get-authorization-token --domain ${domain} --domain-owner ${domainOwner} --query authorizationToken --output text`).toString().trim();
const indexUrl = `https://aws:${codeArtifactAuthToken}@${domain}-${domainOwner}.d.codeartifact.${region}.amazonaws.com/pypi/${repoName}/simple/`;
new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
environment: { PIP_INDEX_URL: indexUrl },
},
});
索引URL或令牌只在捆绑时使用,因此不包含在最终资产中. 只设置环境变量 PIP_INDEX_URL 或者说 PIP_EXTRA_INDEX_URL 应当为安装 Python 私人存储器而工作 pip, (中文(简体) ), pipenv 财务报告和审定财务报表 poetry 基础依赖.
如果您还想要使用代码 Artifact repo 来构建用于捆绑的 Docker 图像, 请使用 buildArgs然而,请注意,设置用于捆绑的自定义构建参数会迫使基部捆绑图像每次被重建(即跳过多克缓存). 构建参数可定制为:
import { execSync } from 'child_process';
const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);
const domain = 'my-domain';
const domainOwner = '111122223333';
const repoName = 'my_repo';
const region = 'us-east-1';
const codeArtifactAuthToken = execSync(`aws codeartifact get-authorization-token --domain ${domain} --domain-owner ${domainOwner} --query authorizationToken --output text`).toString().trim();
const indexUrl = `https://aws:${codeArtifactAuthToken}@${domain}-${domainOwner}.d.codeartifact.${region}.amazonaws.com/pypi/${repoName}/simple/`;
new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
buildArgs: { PIP_INDEX_URL: indexUrl },
},
});