This page describes how to create a web service in the VM using the Gin web framework that is written in Golang. You can choose to create the web service in any other framework that you want to use.
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
mkdir SERVICE_REPO cd SERVICE_REPO
When you create a web service, you must be aware of the following considerations:
CONNECTOR_ENV_.Log the required information and push the logs to Cloud Logging. This helps connector consumers track and debug failures. To publish logs to Cloud Logging, you can use the following Cloud Logging client available in Go: https://pkg.go.dev/cloud.google.com/go/logging#NewClient
You must initialize the logger in main and add a middleware in Gin to track all the incoming requests. You must track the method, path, status, and latency for a request. To filter the logs, use the appropriate severity while logging. In the web service, read the log level from the environment variable. The log level is taken as optional input from during the VM creation. By default, Info logs can be used. The following are the log levels:
Set up the server to gracefully shutdown and handle the in progress requests. For information about how to gracefully restart or stop the server, see Graceful restart or stop.
Gin servers inherently support concurrent requests using Go routines. By default, an undefined number of requests are allowed to be processed by Go routines. However, in some cases, when requests are expected to be resource intensive, use worker pools to restrict and buffer the requests on the server. For more information, see Worker pools example.
EXPORT CONNECTOR_ENV_PORT = 8081 go get . go run .
These commands bundle the required libraries and run the server.
curl -X POST -H "Content-Type: application/json" -H "X-Custom-Header: MyValue" -d '{"name": "Alice", "address": "123 Main St", "gender": "F"}' http://localhost:8081/postData/456
curl -v http://localhost:8081/getData -H "TestKey: MyValue"
go build -o SERVICE_NAME
sudo cp SERVICE_NAME /opt
sudo chmod +x SERVICE_NAME ./SERVICE_NAME
FROM alpine:latest WORKDIR /opt COPY . . CMD ["./SERVICE_NAME"]
sudo docker build -t connector-container .
--restart=unless-stopped to restart the service in case of unexpected failure.All logs in stdout can be routed to Cloud Logging by using the gcplogs Log driver while running the docker container. This helps to track the startup or unexpected failure or shutdown of the service.
To route the logs to Cloud Logging, run the following command:sudo docker run --name connector-service -e CONNECTOR_ENV_PORT=$CONNECTOR_ENV_PORT -p $CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT --restart=unless-stopped ----log-driver=gcplogs connector-container
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-09 UTC.