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

Cannot sam local start-api with an image without doing sam build first #3148

Closed
Soviut opened this issue Aug 6, 2021 · 4 comments
Closed
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days type/question

Comments

@Soviut
Copy link

Soviut commented Aug 6, 2021

Description:

Using an image, it seems you must run sam build to build the image. Without it, sam local start-api starts,
but throws the following error whenever requests are sent to it.

InvalidIntermediateImageError: ImageUri not provided for Function: HelloWorldFunction of PackageType: Image

I want to run start-api without a .aws-sam directory to be able to hot reload.

Steps to reproduce:

  • sam init
  • Which template source would you like to use? 1 - AWS Quick Start Templates
  • What package type would you like to use? 2 - Image
  • Which base image would you like to use? 1 - amazon/nodejs14.x-base
  • Project Name? sam-app
  • sam local start-api
  • GET http://localhost:3000/hello

Observed result:

I get the following 502 error.

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
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. You only need to restart SAM CLI if you update your AWS SAM template
2021-08-06 17:48:59  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Exception on /hello [GET]
Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/samcli/local/apigw/local_apigw_service.py", line 317, in _request_handler
    self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)
  File "/home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/1.27.2/libexec/lib/python3.8/site-packages/samcli/commands/local/lib/local_lambda.py", line 122, in invoke
    raise InvalidIntermediateImageError(
samcli.commands.local.lib.exceptions.InvalidIntermediateImageError: ImageUri not provided for Function: HelloWorldFunction of PackageType: Image
2021-08-06 17:49:04 127.0.0.1 - - [06/Aug/2021 17:49:04] "GET /hello HTTP/1.1" 502 -

Expected result:

The request would go through without error and respond.

Additionally, I would be able to hot reload when I made changes to my code.

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

  1. OS: Ubuntu 20 running under WSL2
  2. sam --version: 1.27.2
  3. AWS region: ca-central-1

DEBUG Enabled

Add --debug flag to command you are running

2021-08-06 18:45:50,834 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-08-06 18:45:50,834 | Using config file: samconfig.toml, config environment: default2021-08-06 18:45:50,834 | Expand command line arguments to:
2021-08-06 18:45:50,834 | --template_file=/home/soviut/projects/sam-test/sam-again/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/home/soviut/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2021-08-06 18:45:50,896 | local start-api command is called
2021-08-06 18:45:50,901 | No Parameters detected in the template
2021-08-06 18:45:50,926 | 2 stacks found in the template
2021-08-06 18:45:50,926 | No Parameters detected in the template
2021-08-06 18:45:50,947 | 2 resources found in the stack 
2021-08-06 18:45:50,947 | No Parameters detected in the template
2021-08-06 18:45:50,975 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'
2021-08-06 18:45:50,976 | --base-dir is not presented, adjusting uri ./hello-world relative to /home/soviut/projects/sam-test/sam-again/template.yaml
2021-08-06 18:45:50,976 | --base-dir is not presented, adjusting uri . relative to /home/soviut/projects/sam-test/sam-again/template.yaml
2021-08-06 18:45:50,982 | No Parameters detected in the template
2021-08-06 18:45:51,007 | No Parameters detected in the template
2021-08-06 18:45:51,028 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2021-08-06 18:45:51,028 | Detected Inline Swagger definition2021-08-06 18:45:51,028 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2021-08-06 18:45:51,029 | Found '0' APIs in resource 'ServerlessRestApi'
2021-08-06 18:45:51,029 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs2021-08-06 18:45:51,029 | 1 APIs found in the template
2021-08-06 18:45:51,033 | Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2021-08-06 18:45:51,033 | 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. You only need to restart SAM CLI if you update your AWS SAM template
2021-08-06 18:45:51,033 | Localhost server is starting up. Multi-threading = True
2021-08-06 18:45:51  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
@jfuss
Copy link
Contributor

jfuss commented Aug 9, 2021

@Soviut All our examples are setup to use sam build. If you really don't want to use sam build, you will need to adjust the template that is produced. Since you are using images, you will need to build the image regardless so sam build or docker build it doesn't really matter. None the less, that is up to you.

So update the ImageUri to point to the local docker image that is your function.

@jfuss jfuss added type/question blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days labels Aug 9, 2021
@awood45
Copy link
Member

awood45 commented Sep 3, 2021

Closing for inactivity, feel free to reopen if you have additional information or questions.

@awood45 awood45 closed this as completed Sep 3, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Sep 3, 2021

⚠️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.

@lsegal
Copy link

lsegal commented Dec 30, 2022

I ran into this same exact issue, which still seems to be a problem. I think the issue stems from the misleading documentation in AWS SAM CLI and output from sam local start-api (bolded below):

Mounting HelloWorldRuby at http://127.0.0.1:3000/hello_ruby [POST]
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
2022-12-29 22:55:30 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

But this "reload" functionality is not doing what is being described. I see that this was addressed in a comment at one point in #901 (comment) in which the issue is that this is "hot reload" is not "hot rebuild". Putting aside the fact that hot reload is nothing without the concept of a recompile, this is certainly confusing, since the documentation above explicitly contradicts this statement ("changes will be reflected automatically"). This should be clarified. As a sidenote, the real fix would be to have some auto rebuild, which was mentioned in the above comment as #921 (sam build --watch) but it seems like nothing has come of that issue.

PS: the confusion regarding this ghost "rebuild" is compounded by even more contradictory logs later down the line-- specifically, when you hit the endpoint, you see the following:

2022-12-29 22:55:41 127.0.0.1 - - [29/Dec/2022 22:55:41] "POST /hello_ruby HTTP/1.1" 200 -
Invoking Container created from helloworldruby:ruby3-v1
Building image.................
Skip pulling image and use local one: helloworldruby:rapid-1.67.0-x86_64.

Building image? 🤔

I assume the original console output was a product of the minimal use of Docker containers in Lambda workflows back in 2019 and before, but these days everything is Dockerized and that path really needs much more first-class support from SAM.

I suppose it would be easy enough to wire up a manual sam build to some watcher script, but:

A) Some automatic rebuild should really be part of the standard feature set, even if it's a separate watch command, and,
B) The docs should at the very least call out this separate path for all the Docker users out there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days type/question
Projects
None yet
Development

No branches or pull requests

4 participants