Contents

The cheapest way to build GCP

In this article, I’ll describe the cheapest way to deploy server with GCP in a side project


tl;dr

  • Deploy API server with Cloud run
  • Deploy DB server with Cloud SQL
  • Build CI/CD pipeline with github action and Cloub build

Deploy MySQL Database server

with Cloud SQL in Google cloud platform

Cloud SQL meta

At first fill in your database meta data.

  1. Set instance id
  2. Set databse password
  3. MySQL version
  4. Region (taiwan)
  5. Set single zone

The myply mainly serves to korean users, but I selected Taiwan(asia-east1) because of pricing issue. detail price description

Cloud SQL Status
  • Select db-f1-micro

I set a machine type to Shared core with 1vCPU, 0.614. This spec is called db-f1-micro. It costs $7.665 per month, which is the cheapest spec in cloud sql. If I calculated it in , the currency of the south korea.

$7.665 * 1300 = ₩9,964 per month

db-f1-micro

Based on Official cloud sql docs

The db-f1-micro and db-g1-small machine types aren’t included in the Cloud SQL SLA. These machine types are configured to use a shared-core CPU, and are designed to provide low-cost test and development instances only. Don’t use them for production instances.

Note: The db-f1-micro and db-g1-small machine types are not included in the Cloud SQL SLA. These machine types are designed to provide low-cost test and development instances only. Do not use them for production instances.

  • Select HDD storage
    • 10GB is the lowest storage size.

$0.09 per GB/month * 10GB(min) = ₩1,170 per month

Cloud SQL misc.1
Cloud SQL misc.2
Cloud SQL misc.3

So total GCP cloud SQL database server will cost ₩ 11,134($8.56) per month.

Buil CI/CD pipeline

with github action and Cloud Build

Before deploy cloud run, you should set cloud build to apply continuous integration and continuous deployment(CICD).


Link with github repository
  • Enable Cloud Build API (almost free)
  • Enable Container analysis API (free)
Install GCP Build plugin to your repository
  • Set your repository to cloud build
Select branch to be triggered and Dockerfile locaiton
  • Select branch to be triggered
  • Enter Dockerfile loaction
    • If you have multi phase (e.g. local, sandbox, beta ,prod) it would be useful to set dockerfile name as dockerfile.prod.
 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
FROM golang:1.18-alpine AS builder

LABEL maintainer="leoo.j <minkj1992@gmail.com> (https://minkj1992.github.io)"

# Move to working directory (/build).
WORKDIR /build

# Copy and download dependency using go mod.
COPY go.mod go.sum ./
RUN go mod download

# Copy the code into the container.
COPY . .

# Set necessary environment variables needed for our image and build the API server.
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN go build -ldflags="-s -w" -o apiserver ./application/cmd/main.go

FROM scratch

# Copy binary and config files from /build to root folder of scratch container.
COPY --from=builder ["/build/apiserver", "/build/.env", "/"]

# Command to run when starting the container.
ENTRYPOINT ["/apiserver"]

Deploy API server

with Cloud run in Google cloud platform

  • Select Continuously deploy new revisions from a source repository.
  • Select region as Taiwan(asia-east1)
  • Select Cpu is only allocated during request processing
  • Set number of instance (Autoscaling) (0 ~ 4)
Set Container status
  • Set application server’s port number
  • Set memory 128Mib(lowest)
  • Set Number of vCPUs less than 1.
  • Set Execution environment to First generation (slower than 2nd generation)
Connect Cloud SQL instance
  • Click Connections tab > Cloud SQL connections > +Add Connection button
Connect Cloud SQL instance 2

Conclusion

Through the steps so far, I have covered the cheapest way to deploy a gcp service for a side project topic. Now if you have followed all the steps so far, go to your github repository and merge it into the main branch. Then the service will deployed according to the Dockerfile you set.