luc*_*s10 3 python amazon-web-services docker aws-lambda aws-sam-cli
我正在尝试在本地测试 lambda 函数,该函数是从 aws 的公共 docker 映像创建的,但是我想从我的 github 安装我自己的 python 库,根据文档AWS sam Build我必须添加一个变量像这样在 Dockerfile 中获取:
FROM public.ecr.aws/lambda/python:3.8
COPY lambda_preprocessor.py requirements.txt ./
RUN yum install -y git
RUN python3.8 -m pip install -r requirements.txt -t .
ARG GITHUB_TOKEN
RUN python3.8 -m pip install git+https://${GITHUB_TOKEN}@github.com/repository/library.git -t .
Run Code Online (Sandbox Code Playgroud)
为了传递 GITHUB_TOKEN,我可以创建一个包含 docker 环境变量的 .json 文件。
{
"LambdaPreprocessor": {
"GITHUB_TOKEN": "TOKEN_VALUE"
}
}
Run Code Online (Sandbox Code Playgroud)
只需在 sam 构建中传递文件地址:sam build --use-container --container-env-var-file env.json或者直接使用以下命令传递不带 .json 的值:sam build --use-container --container-env-var GLOBAL_ENV_VAR=TOKEN_VALUE
我的问题是,我没有通过 .json 文件或直接将其放入命令中来获取 GITHUB_TOKEN 变量--container-env-var GITHUB_TOKEN=TOKEN_VALUE
使用sam build --use-container --container-env-var GLOBAL_ENV_VAR=TOKEN_VALUE --debug表明创建 lambda 图像时不会使用它。
对我有用的唯一方法是将令牌直接放入 Dockerfile 中,而不是作为构建参数。
Building image for LambdaPreprocessor function
Setting DockerBuildArgs: {} for LambdaPreprocessor function
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会发生这种情况,我做错了什么吗?
如果您需要查看 template.yaml,这是 lambda 定义。
LambdaPreprocessor:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Architectures:
- x86_64
Timeout: 180
Metadata:
Dockerfile: Dockerfile
DockerContext: ./lambda_preprocessor
DockerTag: python3.8-v1
Run Code Online (Sandbox Code Playgroud)
我在 Windows 10 上使用 vscode 和 wsl 2 以及 ubuntu 20.04 lts 进行此操作
小智 5
我也有这个问题。我了解到的是,在Metadata该领域DockerBuildArgs:,你也可以添加。例子:
Metadata:
DockerBuildArgs:
MY_VAR: <some variable>
Run Code Online (Sandbox Code Playgroud)
当我添加这个时,它确实会进入字典DockerBuildArgs。
| 归档时间: |
|
| 查看次数: |
1528 次 |
| 最近记录: |