Closed fortivi002 closed 8 months ago
@fortivi002 I am not familiar with DevContainer and how it interacts with the local machine.
Not sure what "I expect the browser to automatically show the changes in the code without the need to stop the process and rerun the sam local start-api command." means? Are you hitting the endpoint again and not seeing the updates?
"hot reloading" works by allows mounting the new files into the container on invoke. So assuming you are not changing the template or the handler path, things should just work. If you are updating the template or handler location or using sam build
, you will need to restart as the template is cached for the duration of the command.
@jfuss Hi, I apologize if my previous explanation was unclear. Yes, I am hitting the endpoint again, but I don't see the changes taking effect. The issue is that hot-reloading is not working as expected because I'm not modifying the template or the handler path. For example, if I change the message returned by the endpoint and save the file, the updated message is not visible when hitting the endpoint again. To see the changes, I have to stop the process and rerun 'sam local start-api'
@fortivi002 Does this work when not using DevContainer?
It will take time to setup this on my end, but if it works without it likely something happening in how DevContainer works.
@jfuss Yes , it's working when I'm not using a DevContainer.
@fortivi002 If this works without DevContainer, it's likely this is an issue with your setup.
It looks like you might be starting sam local
within the DevContainer, which could lead to docker in docker issues. Things I would try:
Hi @fortivi002, just checking in to see if there are any new updates regarding the steps to try out listed above, and if you are still experiencing problems with this.
@lucashuy Hi, the solution provided work. However, my original intention was to get it SAM working within a devcontainer. Using it locally without a devcontainer doesn't align with the scenario I'm aiming to address. If there are any insights or solutions tailored to SAM in a devcontainer, I'd greatly appreciate it.
Thanks for creating this issue @fortivi002
I've followed your steps to re-produce the issue, but I was able to start dev-container, start sam local start-api
and I was able to invoke my function. I made changes through VSCode and curled endpoint again and I was able to see my changes. I even tested by changing the source file through another editor and I was still able to see the change when I recurled the endpoint.
Couple of things to confirm;
curl
.sam build
before running sam local start-api
, any changes to source files will not be picked up by sam local start-api
process. You need to re-build and re-start local api process in order changes to take effect.Closing due to inactivity, please create another issue if you still have the same problem, or any other problems.
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.
Description:
I am using VSCode DevContainer for my project, and everything works fine except for the hot reload when using
sam local start-api
. The hot reload feature is not functioning as expected when running the project in the DevContainer environment. Specifically, the browser does not reflect the changes in the code automatically after saving.Steps to reproduce:
[Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none" RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
[Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
&& rm -rf /tmp/pip-tmp
[Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends awscli
USER root
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \ unzip awscliv2.zip && \ sudo ./aws/install
RUN curl -L https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip -o "aws-sam-cli-linux-x86_64.zip" && \ unzip "aws-sam-cli-linux-x86_64.zip" -d sam-installation && \ sudo ./sam-installation/install
[Optional] Uncomment this line to install global node packages.
RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g" 2>&1
vscode ➜ /workspaces/project/sam-app $ sam local start-api --debug 2023-08-02 14:52:32,329 | Config file location: /workspaces/project/sam-app/samconfig.toml.initialize_function_container at 0x7f70e53a9c60>,
2023-08-02 14:52:32,335 | Loading configuration values from [default.['local', 'start-api'].parameters] (env.command_name.section) in config file at '/workspaces/project/sam-app/samconfig.toml'...
2023-08-02 14:52:32,338 | Configuration values successfully loaded.
2023-08-02 14:52:32,341 | Configuration values are: {'stack_name': 'sam-app', 'warm_containers': 'EAGER'}
2023-08-02 14:52:32,357 | Using SAM Template at /workspaces/project/sam-app/template.yaml
2023-08-02 14:52:32,424 | Using config file: samconfig.toml, config environment: default
2023-08-02 14:52:32,426 | Expand command line arguments to:
2023-08-02 14:52:32,427 | --template_file=/workspaces/project/sam-app/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/home/vscode/.aws-sam/layers-pkg --warm_containers=EAGER
--container_host=localhost --container_host_interface=127.0.0.1
2023-08-02 14:52:32,609 | local start-api command is called
2023-08-02 14:52:32,620 | No Parameters detected in the template
2023-08-02 14:52:32,658 | 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-08-02 14:52:32,660 | 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-08-02 14:52:32,663 | 0 stacks found in the template
2023-08-02 14:52:32,665 | No Parameters detected in the template
2023-08-02 14:52:32,695 | 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-08-02 14:52:32,696 | 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-08-02 14:52:32,698 | 2 resources found in the stack
2023-08-02 14:52:32,700 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2023-08-02 14:52:32,703 | --base-dir is not presented, adjusting uri hello_world/ relative to /workspaces/project/sam-app/template.yaml
2023-08-02 14:52:32,713 | watch resource /workspaces/project/sam-app/template.yaml
2023-08-02 14:52:32,714 | Create Observer for resource /workspaces/project/sam-app/template.yaml with recursive True
2023-08-02 14:52:32,721 | watch resource /workspaces/project/sam-app/template.yaml's parent /workspaces/project/sam-app
2023-08-02 14:52:32,723 | Create Observer for resource /workspaces/project/sam-app with recursive False
2023-08-02 14:52:32,732 | Initializing the lambda functions containers.
2023-08-02 14:52:32,734 | Async execution started
2023-08-02 14:52:32,738 | Invoking function functools.partial(<function InvokeContext._initialize_all_functions_containers.
Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='python3.10', memory=None, timeout=3, handler='app.lambda_handler', imageuri=None, packagetype='Zip',
imageconfig=None, codeuri='/workspaces/project/sam-app/hello_world', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId':
'ServerlessRestApi'}}}, metadata={'SamResourceId': 'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], function_url_config=None,
function_build_info=<FunctionBuildInfo.BuildableZip: ('BuildableZip', 'Regular ZIP function which can be build with SAM CLI')>, stack_path='', runtime_management_config=None))
2023-08-02 14:52:32,746 | Waiting for async results
2023-08-02 14:52:32,753 | No environment variables found for function 'HelloWorldFunction'
2023-08-02 14:52:32,755 | Loading AWS credentials from session with profile 'None'
2023-08-02 14:52:32,789 | Resolving code path. Cwd=/workspaces/project/sam-app, CodeUri=/workspaces/project/sam-app/hello_world
2023-08-02 14:52:32,791 | Resolved absolute path to code is /workspaces/project/sam-app/hello_world
2023-08-02 14:52:32,807 | watch resource /workspaces/project/sam-app/hello_world
2023-08-02 14:52:32,808 | Create Observer for resource /workspaces/project/sam-app/hello_world with recursive True
2023-08-02 14:52:32,816 | watch resource /workspaces/project/sam-app/hello_world's parent /workspaces/project/sam-app
2023-08-02 14:52:32,820 | Code /workspaces/project/sam-app/hello_world is not a zip/jar file
2023-08-02 14:52:33,731 | Local image is up-to-date
2023-08-02 14:52:33,741 | Using local image: public.ecr.aws/lambda/python:3.10-rapid-x86_64.
2023-08-02 14:52:33,743 | Mounting /workspaces/project/sam-app/hello_world as /var/task:ro,delegated, inside runtime container
2023-08-02 14:52:34,500 | Async execution completed
2023-08-02 14:52:34,501 | Containers Initialization is done.
2023-08-02 14:52:34,502 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2023-08-02 14:52:34,503 | Detected Inline Swagger definition
2023-08-02 14:52:34,504 | Parsing Swagger document using 2.0 specification
2023-08-02 14:52:34,506 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2023-08-02 14:52:34,507 | Found '0' APIs in resource 'ServerlessRestApi'
2023-08-02 14:52:34,508 | Found '0' authorizers in resource 'ServerlessRestApi'
2023-08-02 14:52:34,510 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2023-08-02 14:52:34,512 | 1 APIs found in the template
2023-08-02 14:52:34,516 | Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2023-08-02 14:52:34,518 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected
instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template
2023-08-02 14:52:34,521 | Localhost server is starting up. Multi-threading = True
2023-08-02 14:52:34 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Expected result:
I expect the browser to automatically show the changes in the code without the need to stop the process and rerun the sam local start-api command.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
sam --version 1.94.0
:You need to install the Dev Containers extension to VSCode : Dev Containers v0.299.0