Summarized form tutorials:

Prepare files

Create app.yaml and Dockerfile in app root.

1
2
3
/main.go
/app.ymal
/Dockerfile

Find instance name

For example: your_project_id:east-2:my_instance_name

Instances name can be found in:
Google Cloud Dashboard -> SQL -> Instances.

It looks like:

1
<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>

In the table, find it in column ‘Instance connection name’.

Copy and paste it. Should work.

app.yaml

1
2
3
4
5
6
7
8
9
10
11
runtime: custom
env: flex

env_variables:
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
DB_USER: your_db_username
DB_PASS: your_db_password
DB_NAME: your_db_name

beta_settings:
cloud_sql_instances: <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>

Dockerfile

Updated golang version to 1.19

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# FOR GAE Flexible Environment Custom Runtime 
FROM golang:1.19

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

# install build essentials
RUN apt-get update && \
apt-get install -y wget build-essential pkg-config --no-install-recommends

RUN apt-get install -y mupdf mupdf-tools

# Move to working directory /build
WORKDIR /build

# Copy the code into the container
COPY . .

# Copy and download dependency using go mod
RUN go mod download

# Build the application
RUN go build -o main .

# Export necessary port # default GCP App Engine Port
EXPOSE 8080

# Command to run when starting the container
CMD ["/build/main"]

Server port of Go-gin

Server port should be 8080.
If you use config file, alter it accordingly.

1
router.Run(":" + config.Server.Port)

GORM

Import dialers.

The host here should be like:
/cloudsql/your_project_id:east-2:my_instance_name

Pay attention to the leading /cloudsql/ part.

1
2
3
4
5
6
7
8
9
10
11
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"

...

connectionString := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",
host, username, dbname, password)

db, err = gorm.Open("postgres", connectionString)
if err != nil {
fmt.Println("db err: ", err)
}

Others

You don’t need Google Cloud Proxy to connect, as far as your App Engine and SQL instances are in a same project.

Deploy

1
gcloud app deploy

It should works.