Menu

Testing GCP Cloud Functions Locally with Docker Compose and Summon

DEV Community: serverless·Raphaël Pinson·7 months ago
#DcPrXt5D
#dev#class#code#highlight#secrets
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.