Debugging Google Cloud Run functions when running in Functions Emulator

Published:

I'm using the gcloud CLI to deploy and call functions.

gcloud alpha functions local deploy ... --verbosity=debug
gcloud alpha functions local call ... --verbosity=debug

To start with, it's best to set verbosity to debug.

Sometimes a function will look like it's deployed successfully and then give a 500, or an error like 

gcloud crashed (ConnectionError): HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url...

With Functions Emulator, there's a Docker container somewhere that runs or attempts to run. If it's running you can find it's ID with docker ps. If it's not running, you can find the container ID in the deploy output (it's the 64 character hex string you'll see).

Once you have the ID, you can check it's logs - docker logs <ID>. That's how I've been finding most of my errors.

Functions Emulator is fine once you figure it out. The context that a Cloud Run function exists in needs to be recreated locally and this seems like the best way.

It's nice that it takes care of the Docker details, but you can still make use of the underlying tools to find out what's going on. There's nothing worse than not being able to see what errors you're causing after you've run some code.