Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: dockerbuildargs doesn't work #5903

Closed
vorpal56 opened this issue Sep 7, 2023 · 6 comments
Closed

Bug: dockerbuildargs doesn't work #5903

vorpal56 opened this issue Sep 7, 2023 · 6 comments
Labels
area/build sam build command blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale.

Comments

@vorpal56
Copy link

vorpal56 commented Sep 7, 2023

Description:

I'm building my lambda function using docker and the Metadata.DockerBuildArgs aren't accessible in the dockerfile.

Steps to reproduce:

template.yaml

...
Resources:
    Fname:
        Type: AWS::Serverless::Function
        Properties:
            PackageType: Image
            Architectures:
                - x86_64
            ...
        Metadata:
            Dockerfile: Dockerfile
            DockerContext: ./functions/my_function
            DockerTag: latest-function
            DockerBuildArgs:
                MY_VAR: some_value

functions/my_function/Dockerfile

ARG MY_VAR
ENV MY_ENV_VAR=$MY_VAR
# none of the below echo some_value
RUN echo $MY_ENV_VAR 
RUN echo $MY_VAR

sam build

Observed result:

Step 4/5 : RUN echo $MY_VAR
 ---> Running in ...

Removing intermediate container f251f1920805
 ---> 530429c1592d
Step 5/5 : RUN echo $MY_ENV_VAR
 ---> Running in ...

Expected result:

Step 4/5 : RUN echo $MY_VAR
 ---> Running in ...
some_value
Removing intermediate container f251f1920805
 ---> 530429c1592d
Step 5/5 : RUN echo $MY_ENV_VAR
 ---> Running in ...
some_value

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: linux
  2. sam --version: 1.86.1
@vorpal56 vorpal56 added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Sep 7, 2023
@mndeveci
Copy link
Contributor

mndeveci commented Sep 7, 2023

Hi there,

I've tried to re-produce the issue but I was able to see the output during the build.
Dockerfile;

FROM public.ecr.aws/lambda/python:3.10

ARG MY_VAR
ENV MY_ENV_VAR=$MY_VAR
# none of the below echo some_value
RUN echo $MY_ENV_VAR 
RUN echo $MY_VAR

Resource definition;

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./functions/my_function
      DockerTag: latest-function
      DockerBuildArgs:
        MY_VAR: some_value

If I run sam build I can see some_value in the output;

❯ sam build
Building codeuri: gh-issues/cli-5903/sam-app runtime: None metadata: {'Dockerfile': 'Dockerfile', 'DockerContext':
'gh-issues/cli-5903/sam-app/functions/my_function', 'DockerTag': 'latest-function', 'DockerBuildArgs': {'MY_VAR': 'some_value'}} architecture: x86_64
functions: HelloWorldFunction
Building image for HelloWorldFunction function
Setting DockerBuildArgs: {'MY_VAR': 'some_value'} for HelloWorldFunction function
Step 1/6 : FROM public.ecr.aws/lambda/python:3.10
 ---> ee6bca5d31b2
Step 2/6 : ARG MY_VAR2
 ---> Running in e283214f091e
Removing intermediate container e283214f091e
 ---> ed133a943872
Step 3/6 : ARG MY_VAR
 ---> Running in 281a5e54af64
Removing intermediate container 281a5e54af64
 ---> eef85de13209
Step 4/6 : ENV MY_ENV_VAR=$MY_VAR
 ---> Running in 919fbb4ea5f6
Removing intermediate container 919fbb4ea5f6
 ---> a5caa061af9e
Step 5/6 : RUN echo $MY_ENV_VAR
 ---> Running in 847633aee42d
some_value
Removing intermediate container 847633aee42d
 ---> a32eeec94650
Step 6/6 : RUN echo $MY_VAR
 ---> Running in 51e8cd3de606
some_value
Removing intermediate container 51e8cd3de606
 ---> 3e4de8c81c86
Successfully built 3e4de8c81c86
Successfully tagged helloworldfunction:latest-function


Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

If I re-run this without changing anything, the lines where it outputs some_value will be replaced by Using cache since docker caches each line of Dockerfile seperately. If you want to see it again, you can delete the image by its id, which is printed right before the tag (3e4de8c81c86 for the example above).

So you can do docker rmi {image_id} && sam build to see the output again.

Please let us know if you have other questions.

@mndeveci mndeveci added blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days area/build sam build command and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Sep 7, 2023
@vorpal56
Copy link
Author

vorpal56 commented Sep 8, 2023

If you want to see it again, you can delete the image by its id, which is printed right before the tag (3e4de8c81c86 for the example above).

@mndeveci Thanks for the quick response. Yea, I've deleted the image and reran the build. It seems like the order of ARG and FROM is important..? In my dockerfile, I specify the base image after ARG. Is this the bug?

# doesn't work as expected
ARG MY_VAR
FROM public.ecr.aws/lambda/python:3.10
RUN echo $MY_VAR
# works as expected
FROM public.ecr.aws/lambda/python:3.10
ARG MY_VAR
RUN echo $MY_VAR

@vorpal56
Copy link
Author

vorpal56 commented Sep 8, 2023

On side note: how would I specify dynamic build args for a production env vs. a development env? My use case is I have a local image for my layers and I want to use that vs. the one in ECR for example. So something like:

FROM ${IMAGE_LOCATION} as layers
...

But that means I have to define a template.prod.yaml and a template.dev.yaml with the only difference being in Metadata.DockerBuildArgs. I wasn't able to find much in the docs, but any direction would be helpful. Thanks

@mndeveci
Copy link
Contributor

mndeveci commented Sep 8, 2023

I don't know about the order of FROM and ARG keyword, I would recommend checking Dockerfile specification. For SAM CLI, we are not doing anything specific there, we are making docker API calls to build the Dockerfile specification.

For making it dynamic, you can use !Ref intrinsic function and pass parameter when you are building the image. You need to change your template like this;

Parameters:
  MyVal:
    Type: String

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./functions/my_function
      DockerTag: latest-function
      DockerBuildArgs:
        MY_VAR: !Ref MyVal

And you need to provide value for MyVal during build;

sam build --parameter-overrides MyVal=prod

@mndeveci
Copy link
Contributor

mndeveci commented Sep 8, 2023

I am resolving this issue now but please don't hesitate to ask any questions you might have.

@mndeveci mndeveci closed this as completed Sep 8, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build sam build command blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale.
Projects
None yet
Development

No branches or pull requests

2 participants