Go-gin GROM connect gcloudsql on Google Cloud App Engine
2023-01-12
Summarized form tutorials:
- Go Application deployment on Google App engine flexible environment
- Connecting to Cloud SQL (Postgres) from a Go web app
Prepare files
Create app.yaml and Dockerfile in app root.
1 | /main.go |
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 | runtime: custom |
Dockerfile
Updated golang version to 1.19
1 | # FOR GAE Flexible Environment Custom Runtime |
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 | _ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres" |
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.