Make a connection to gcloud sql with jinzhu/gorm in gcloud function
1 min readMay 26, 2019
Assume you already have gcloud functions and gcloud sql, Also you want these two connect together properly. Means without hard-coding/passing instance ip-address. How could we do that ?
The problem is it requires you to input specific ip-address or length of ip-address with subnet to be set of allowed ip-address that can able to connect to. This is less security and leads to be pwned.
So, To be able to connect to gcloud sql from gcloud functions, These are follow steps that need to be done.
- Enable Cloud SQL Admin API https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview
- use database/sql as dialect instead of jinzhu/gorm SQL dialect
import (
_ "database/sql"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
)
- use cloud SQL protocol in connection string
user:password@cloudsql(project:zone:instanceId)/database
Reference: https://gist.github.com/erans/795f7616049f310b2a71a50e5df4529a
#please let me know if you’ve found any bad gramma, word.