Sam build (PackageType: Image) - unable to set environment variables, specifically to authenticate for private npm codeartifact packages
Description:
I am trying to pass environment variables to my docker container, but when I do a printenv I can see that the variables are not seen by the container. Is this not supported? Is there a workaround? or what I am doing wrong?
Seems it is exactly what is discussed in this closed issue. Specifically, this comment: #2972 (comment). Not sure if the feature request was ever submitted or the support ever occurred. please advise.
Steps to reproduce:
Add RUN printenv instruction into the Dockerfile to see the current environment variables.
Run the following command at the root level of the lambda project that is set to PackageType Image.
sam build --use-container -e MY_TOKEN="mytoken"
Observed result:
The environment variable MY_TOKEN does not show up in the list
Expected result:
The environment variable MY_TOKEN shows with the value specified in command line.
NOTE: The ideal goal I am trying to do is to set the variable CODEARTIFACT_AUTH_TOKEN so that I can install a private npm package from codeartifacts with the credentials. Unable to verify if this will work without being able to set the environment variable.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Mac BigSur
sam --version: 1.36- AWS region: us-west-2
We are also having the same issue. Specifically we are trying to use the image package type.
Thanks for reporting the issue, we will be investigating it further. Please watch this channel for more updates, and feel free to reach out.
Hi @lb-pf, the env var supplied via option -e, --container-env-var is passed into the build container. I believe your use case is to pass CODEARTIFACT_AUTH_TOKEN into the Dockerfile of your Lambda Function, but not the build container.
Please use DockerBuildArgs under Metadata of your Function definition. Here's the relevant doc.
Hope that resolves your issue. I'm closing the issue for now. Feel free to re-open it if you still have questions.
Sorry to comment on a closed item, let me know if I should open another one. The docs are pretty sparse when it comes to DockerBuildArgs. What is the suggested/recommended way to pass sensitive information via DockerBuildArgs, e.g. aws key pairs or credentials?
E.g. is it possible to use secrets manager to resolve sensitive args for the build? If not, what mechanism is recommended?
@brett-bim Did you ever come to a working solution? This is my exact question, too.
@clarkritchie Nope, never figured it out. I ended up with a workaround. We were trying to install a lambda layer in a docker image and needed AWS creds. Instead, I just downloaded the layer in a zip and copied that into the Dockerfile in our build process. I'm sure there's some way to make this work but I never could find it.
Thanks a bunch @brett-bim -- I concluded the same, the only workarounds for secrets in DockerBuildArgs are all hacks.
Due to these "workarounds", unfortunately, I was actually given the advice (by AWS) to not use PackageType = Image and not consider it as an equivalent alternative to the traditional Zip, but only as an attempt to support those who MUST use containers, due to their own internal infrastructure constraints or rules.
On Wed, Apr 6, 2022 at 8:35 AM Clark Ritchie @.***> wrote:
Thanks a bunch @brett-bim https://github.com/brett-bim -- I concluded the same, the only workarounds for secrets in DockerBuildArgs are all hacks.
— Reply to this email directly, view it on GitHub https://github.com/aws/aws-sam-cli/issues/3571#issuecomment-1090281253, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWMUNBRVK6CILWNW5XYSM7DVDWHLHANCNFSM5LOGTJFA . You are receiving this because you were mentioned.Message ID: @.***>
Yeah in general my gut feeling is that containers in a lambda are nice to have in theory but almost always going to cause way more headaches. However, we have data science lambdas running a variety of packages that exceed the size limitations of lambda so we either have to set all those up as layers (which is also a PITA) or use containers. We chose the latter but as I've tried to bring in more functionality, I run up against things like this more and more.
uh...what happend, why is this closed? i have the same dilemma
same here brother...
I think this may address some use cases for some people facing this issue (solved mine after stumbling across this issue).
Assuming a template.yaml like https://github.com/aws/aws-sam-cli/issues/2972#issuecomment-866324601, add...
Metadata:
Dockerfile: Dockerfile
DockerContext: .
DockerBuildArgs:
PARAM: !Ref MyParam
Then when running sam build you can do (from Terminal):
$ export ENV_VAR="somep4r4m"
$ sam build -t template.yaml --parameter-overrides MyParam=$ENV_VAR
You should then see:
Function
Building image for Function function
Setting DockerBuildArgs: {'PARAM': 'somep4r4m'} for Function function
And you can reference the environment variable in your Dockerfile like...
FROM python:3.8-buster as build
ARG PARAM
RUN echo $PARAM
(or an actual command...)
Note that these build-args may get packaged with the image in a way that could be decoded.