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

sam local start-api detached mode #1012

Open
frob opened this issue Feb 15, 2019 · 9 comments
Open

sam local start-api detached mode #1012

frob opened this issue Feb 15, 2019 · 9 comments
Labels
area/local/start-api sam local start-api command maintainer/need-response stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) type/feature Feature request

Comments

@frob
Copy link
Contributor

frob commented Feb 15, 2019

Describe your idea/feature/enhancement

Allow sam local start-api to run detached

Proposal

While SAM is great for local development it can fall short for CI or even test commands. In order to run tests automatically against my api-gateway I need to run sam local start-api this command continues running until I press Ctrl-C. This works well for local development but I would like to script this out into a CI that will allow me to run integration tests without deploying to aws.

Things to consider:

  1. Will this require any updates to the SAM Spec

    • No, as far as I know, this should require any updates to the spec.
  2. Additional arguments/commands

    • By adding a detached mode that also means there must be a non-interrupt way to stop the api. I suggest sam local stop-api as a way to stop the api. This command would be dependent on the directory and template that is running and thus the running instance would be a singleton.
    • I would suggest -d as this would follow the docker command scheme and be more familiar to developers.
@ndobryanskyy
Copy link
Contributor

Proposed feature is really handy, I like it, but there are some open questions and to me, this flag seems to be not that simple.

  • As I understood, user would be able to start only single local api-gateway? And stop-api will work from any directory to enable the user to easily stop the service? Or should one be able to start several api-gateways for different templates?
  • SAM should also include the command to attach to the running local api-gateway to examine logs -> which leads us to the fact, that all of the logs should be aggregated somewhere, so the user could examine the recent logs upon attaching.
  • -d flag name is already taken by the --debugger-port option as a shorthand name, so this should be more verbose like --detached or something;

Does that make sense, @frob?

@frob
Copy link
Contributor Author

frob commented Feb 28, 2019

Yes that is what I am referring to.

As I understood, user would be able to start only single local api-gateway? And stop-api will work from any directory to enable the user to easily stop the service? Or should one be able to start several api-gateways for different templates?

Does SAM CLI currently work with multiple templates? I was thinking of single endpoints, but I could see it being useful for multiple. The purpose for this is for integration testing so as many endpoints as needed for the test and I can see an argument for multiple endpoints being required for a full integration test. Honestly, it is usually these features work better if they start small and then we can add features in another issue.

SAM should also include the command to attach to the running local api-gateway to examine logs -> which leads us to the fact, that all of the logs should be aggregated somewhere, so the user could examine the recent logs upon attaching .

I am unclear about this. I kind of think that logging would just follow the current workflow for docker.

-d flag name is already taken by the --debugger-port option as a shorthand name, so this should be more verbose like --detached or something;

Good point. Docker run docs use --detach and I suggest we use that.

@jfuss jfuss added area/local/start-api sam local start-api command stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) type/feature Feature request labels Apr 23, 2019
@ghost
Copy link

ghost commented May 21, 2020

Any updates on this?

@mmartsen
Copy link

mmartsen commented Jun 2, 2020

Can it be just "dockerized"? So instead of running "sam local start-api" we run "docker-compose up"?
To me it seems doable to generate docker-compose.yml after "sam build" command with all necessary folders mapped. I think majority of CI/CD tools could work with docker-compose, also things like logging, attach/detach are already solved there.

@Santiago8888
Copy link

The second time I run into this issue, current use cases:

  1. Test lambdas on CI/CD envs. without deploying them.
  2. Lift multiple API Gateways without opening multiple terminals.

@jonowhite
Copy link

I think this would be really helpful. I have similar use cases, my current method I am testing (which might be a little dodgy) is to send the sam command to the background and redirect all logs to prevent interference.

sam local start-api  > /dev/null 2>&1 &

This keeps the local API active while retaining control of the shell. So you could run it multiple times, or re-use the shell to run tests against the local API.

Potential issue when it comes to stopping the background processes if you have multiple running and need to be careful with which you kill.

@tahoward
Copy link

Running into a similar situation. Using this in a Makefile in interim:

start:
	sam local start-api --docker-network <network_name> --warm-containers EAGER &

stop:
	kill $$(ps -wwo pid,args | grep "[/]bin/sam local start-api" | awk '{print $$1}')
	CONTAINERS=$$(docker network inspect <network_name> | jq -r '.[].Containers[].Name')
	echo "$$CONTAINERS" | xargs docker stop; echo "$$CONTAINERS" | xargs docker rm

sam local start-api --docker-network <network_name> is used to track any containers spawned by sam. It appears to orphan them when using --warm-containers and PID gets killed.

@dschiavu
Copy link

+1

Would love to see this command being implemented.

@matthew-heyner
Copy link

+1

This would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/local/start-api sam local start-api command maintainer/need-response stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) type/feature Feature request
Projects
None yet
Development

No branches or pull requests

10 participants