【问题标题】:AWS SAM DockerBuildArgs It does not add them when creating the lambda imageAWS SAM DockerBuildArgs It does not add them when creating the lambda image
【发布时间】:2022-12-02 05:08:09
【问题描述】:

I am trying to test a lambda function locally, the function is created from the public docker image from aws, however I want to install my own python library from my github, according to the documentation AWS sam Build I have to add a variable to be taken in the Dockerfile like this:

  • 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 .

And to pass the GITHUB_TOKEN I can create a .json file containing the variables for the docker environment.

  • .json file named env.json
{
    "LambdaPreprocessor": {
        "GITHUB_TOKEN": "TOKEN_VALUE"
    }
}

And simply pass the file address in the sam build: sam build --use-container --container-env-var-file env.json Or directly the value without the .json with the command: sam build --use-container --container-env-var GLOBAL_ENV_VAR=TOKEN_VALUE

My problem is that I don't get the GITHUB_TOKEN variable either with the .json file or by putting it directly in the command with --container-env-var GITHUB_TOKEN=TOKEN_VALUE

Using sam build --use-container --container-env-var GLOBAL_ENV_VAR=TOKEN_VALUE --debug shows that it doesn't take it when creating the lambda image.

The only way that has worked for me is to put the token directly in the Dockerfile not as an build argument.

  • Promt output
Building image for LambdaPreprocessor function
Setting DockerBuildArgs: {} for LambdaPreprocessor function

Does anyone know why this is happening, am I doing something wrong?

If you need to see the template.yaml this is the lambda definition.

  • template.yaml
LambdaPreprocessor:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Timeout: 180
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./lambda_preprocessor
      DockerTag: python3.8-v1

I'm doing it with vscode and wsl 2 with ubuntu 20.04 lts on windows 10

【问题讨论】:

  • did you find the solution? I'm having the same issue and pulling my hair trying to figure it out!!

标签: python amazon-web-services docker aws-lambda aws-sam-cli


【解决方案1】:

I am having this issue too. What I have learned is that in the Metadata field there is DockerBuildArgs: that you can also add. Example:

    Metadata:
      DockerBuildArgs:
        MY_VAR: <some variable>

When I add this it does make it to the DockerBuildArgs dict.

【讨论】: