sam deploy --container-env-var-file not working
Description:
I want to pass environment variables into the container but the environment variable is not being displayed.
Steps to reproduce:
sam init# hello world example with Image package type- Create
env.json
{
"Parameters": {
"GITHUB_TOKEN": "MY_GITHUB_TOKEN"
}
}
- Add GITHUB_TOKEN ARG and ENV in
Dockerfile
FROM public.ecr.aws/lambda/python:3.10
COPY app.py requirements.txt ./
RUN python3.10 -m pip install -r requirements.txt -t .
# Add ARG and ENV
ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=${GITHUB_TOKEN}
RUN echo "GITHUB_TOKEN=${GITHUB_TOKEN}"
CMD ["app.lambda_handler"]
sam build --use-container --container-env-var-file env.json
Observed result:
$ sam build --use-container --container-env-var-file env.json --debug
2023-10-25 17:50:43,313 | Config file location: /Users/darren/sam-app/samconfig.toml
2023-10-25 17:50:43,316 | Loading configuration values from [default.['build'].parameters] (env.command_name.section) in config file
at '/Users/darren/sam-app/samconfig.toml'...
2023-10-25 17:50:43,316 | Configuration values successfully loaded.
2023-10-25 17:50:43,317 | Configuration values are: {'stack_name': 'sam-app', 'parallel': True}
2023-10-25 17:50:43,320 | Using SAM Template at /Users/darren/sam-app/template.yaml
2023-10-25 17:50:43,340 | Using config file: samconfig.toml, config environment: default
2023-10-25 17:50:43,340 | Expand command line arguments to:
2023-10-25 17:50:43,341 | --template_file=/Users/darren/sam-app/template.yaml --use_container
--container_env_var_file=env.json --parallel --mount_with=READ --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache
2023-10-25 17:50:43,399 | 'build' command is called
2023-10-25 17:50:43,400 | Starting Build inside a container
2023-10-25 17:50:43,403 | No Parameters detected in the template
2023-10-25 17:50:43,418 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the
resource logical id as the resource id
2023-10-25 17:50:43,419 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the
resource logical id as the resource id
2023-10-25 17:50:43,419 | 0 stacks found in the template
2023-10-25 17:50:43,420 | No Parameters detected in the template
2023-10-25 17:50:43,431 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the
resource logical id as the resource id
2023-10-25 17:50:43,432 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the
resource logical id as the resource id
2023-10-25 17:50:43,432 | 2 resources found in the stack
2023-10-25 17:50:43,433 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'
2023-10-25 17:50:43,433 | --base-dir is not presented, adjusting uri ./hello_world relative to
/Users/darren/sam-app/template.yaml
2023-10-25 17:50:43,434 | --base-dir is not presented, adjusting uri . relative to /Users/darren/sam-app/template.yaml
2023-10-25 17:50:43,438 | 2 resources found in the stack
2023-10-25 17:50:43,438 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'
2023-10-25 17:50:43,439 | Error occurred while trying to track an event: Event 'BuildFunctionRuntime' does not accept value 'None'.
2023-10-25 17:50:43,440 | Instantiating build definitions
2023-10-25 17:50:43,442 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(None,
/Users/darren/sam-app, Image, , 972bf608-3c12-4d3d-a81c-7ae41434b4a1, {'Dockerfile': 'Dockerfile', 'DockerContext':
'/Users/darren/sam-app/hello_world', 'DockerTag': 'python3.10-v1'}, {'GITHUB_TOKEN': 'MY_GITHUB_TOKEN'}, x86_64, []),
Function: Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime=None,
memory=None, timeout=3, handler=None, imageuri=None, packagetype='Image', imageconfig=None,
codeuri='/Users/darren/sam-app', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api',
'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'Dockerfile': 'Dockerfile',
'DockerContext': '/Users/darren/sam-app/hello_world', 'DockerTag': 'python3.10-v1', 'SamResourceId': 'HelloWorldFunction'},
inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], function_url_config=None,
function_build_info=<FunctionBuildInfo.BuildableImage: ('BuildableImage', 'Regular IMAGE function which can be build with SAM CLI')>,
stack_path='', runtime_management_config=None))
2023-10-25 17:50:43,446 | Async execution started
2023-10-25 17:50:43,447 | Invoking function functools.partial(<bound method ParallelBuildStrategy.build_single_function_definition of
<samcli.lib.build.build_strategy.ParallelBuildStrategy object at 0x103bd3100>>, <samcli.lib.build.build_graph.FunctionBuildDefinition
object at 0x103c13430>)
2023-10-25 17:50:43,448 | Building codeuri: /Users/darren/sam-app runtime: None metadata: {'Dockerfile': 'Dockerfile',
'DockerContext': '/Users/darren/sam-app/hello_world', 'DockerTag': 'python3.10-v1'} architecture: x86_64 functions:
HelloWorldFunction
2023-10-25 17:50:43,449 | Waiting for async results
2023-10-25 17:50:43,449 | Building to following folder /Users/darren/sam-app/.aws-sam/build/HelloWorldFunction
2023-10-25 17:50:43,450 | Building image for HelloWorldFunction function
2023-10-25 17:50:43,460 | Setting DockerBuildArgs: {} for HelloWorldFunction function
2023-10-25 17:50:46,542 | <Image: 'helloworldfunction:python3.10-v1'> image is built for HelloWorldFunction function
Step 1/7 : FROM public.ecr.aws/lambda/python:3.10
---> 927735922542
Step 2/7 : COPY app.py requirements.txt ./
---> c4de5e1ee1aa
Step 3/7 : RUN python3.10 -m pip install -r requirements.txt -t .
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
---> Running in bbc338f2ee2c
Collecting requests
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 1.8 MB/s eta 0:00:00
Collecting charset-normalizer<4,>=2
Downloading charset_normalizer-3.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (139 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.2/139.2 kB 4.8 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 20.3 MB/s eta 0:00:00
Collecting idna<4,>=2.5
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 14.7 MB/s eta 0:00:00
Collecting urllib3<3,>=1.21.1
Downloading urllib3-2.0.7-py3-none-any.whl (124 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 kB 18.6 MB/s eta 0:00:00
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2023.7.22 charset-normalizer-3.3.1 idna-3.4 requests-2.31.0 urllib3-2.0.7
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[notice] A new release of pip is available: 23.0.1 -> 23.3.1
[notice] To update, run: pip install --upgrade pip
Removing intermediate container bbc338f2ee2c
---> e0884c4d724e
Step 4/7 : ARG GITHUB_TOKEN
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
---> Running in a690c7b159a1
Removing intermediate container a690c7b159a1
---> e9a4c466ba39
Step 5/7 : ENV GITHUB_TOKEN=${GITHUB_TOKEN}
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
---> Running in bc4c3f5b2b62
Removing intermediate container bc4c3f5b2b62
---> e6efd9f2a8c2
Step 6/7 : RUN echo "GITHUB_TOKEN=${GITHUB_TOKEN}"
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
---> Running in efc7cf83c059
GITHUB_TOKEN=
Removing intermediate container efc7cf83c059
---> 6fd9b01e0f0c
Step 7/7 : CMD ["app.lambda_handler"]
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
---> Running in a6c8de7bdaed
Removing intermediate container a6c8de7bdaed
---> 5efbbeb9402f
Successfully built 5efbbeb9402f
Successfully tagged helloworldfunction:python3.10-v1
2023-10-25 17:50:46,544 | Async execution completed
2023-10-25 17:50:46,546 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the
resource logical id as the resource id
2023-10-25 17:50:46,547 | 2 resources found in the stack
2023-10-25 17:50:46,547 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'
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
2023-10-25 17:50:46,550 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-10-25 17:50:46,608 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-10-25 17:50:46,609 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'efc3191a-8464-452a-9a61-a81b07cf7ea5',
'installationId': '2c02f21c-1f92-4caa-9cce-fffe055a9c38', 'sessionId': 'bceadddb-b849-49f5-8e2b-5f0a50524e74',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.10.13', 'samcliVersion': '1.99.0', 'awsProfileProvided': False,
'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin':
None, 'projectName': '593ab2ca51e925b9f6c2f258bc55ed5926cf6d2c78239a685d65907e4ec7edd3', 'initialCommit': None}, 'duration': 3210,
'exitReason': 'success', 'exitCode': 0}}]}
2023-10-25 17:50:46,609 | Unable to find Click Context for getting session_id.
2023-10-25 17:50:46,612 | Sending Telemetry: {'metrics': [{'events': {'requestId': '194fe6bb-5a10-48cb-bdaf-7a7e01a6a592',
'installationId': '2c02f21c-1f92-4caa-9cce-fffe055a9c38', 'sessionId': 'bceadddb-b849-49f5-8e2b-5f0a50524e74',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.10.13', 'samcliVersion': '1.99.0', 'commandName': 'sam build',
'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id':
'40934ad755e04bfeab146e77ac294d9d', 'time_stamp': '2023-10-26 00:50:43.313', 'exception_name': None}, {'event_name':
'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '31be5cf48fdb4738931d42dcae204d6f', 'time_stamp': '2023-10-26
00:50:43.340', 'exception_name': None}]}}}]}
2023-10-25 17:50:46,808 | Telemetry response: 200
2023-10-25 17:50:46,817 | Telemetry response: 200
Expected result:
I expected the GITHUB_TOKEN to be printed when running sam build. However, nothing is printed:
Step 6/7 : RUN echo "GITHUB_TOKEN=${GITHUB_TOKEN}"
GITHUB_TOKEN=
Also, I can clearly see GITHUB_TOKEN being set in the BuildDefinition --debug logs.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: MacOS
sam --version: SAM CLI, version 1.99.0- AWS region: us-east-1
Add --debug flag to command you are running
Hi @darhsu thanks for opening the issue. Looking at the old issues and the code snippet it seems the 2 build parameters container-env-var and container-env-var-file only apply for ZIP package-type Lambda Functions to pass them into the build container. If you want to pass environment variables to the Dockerfile of your Lambda Function, you can use the DockerBuildArgs under Metadata, more info in doc.
Does this address the issue you are raising? https://github.com/aws/aws-sam-cli/issues/3571#issuecomment-1802661781
Closing as the issue has been inactive for a while. Feel free to try the above suggestions and open a new issue if you still have questions or concerns.
⚠️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.
@tomblaze The solution you are pointing out will show up the token while running the command Metadata section is not hidden.