I use GCP Cloud Functions quite a bit, but testing them locally can be challenging.
Here's how I do it.
Package and run with Docker
The first step is to build one container per function. I use Docker for this, with two steps.
Add an executable entrypoint
My functions are written in Go, so I add a cmd/main.go to my codebase to run an HTTP server that calls the function logic:
package main
import ( "log" "os"
// Blank-import the function package so the init() runs _ "yourfunctionpackage"
"github.com/GoogleCloudPlatform/functions-framework-go/funcframework" )
func main () { // Use PORT environment variable, or default to 8080. port := "8080" if envPort := os . Getenv ( "PORT" ); envPort != "" { port = envPort } if err := funcframework . Start ( port ); err != nil { log .…