|
| 1 | +# Connecting Cloud Run to Cloud SQL with the Node.js Connector |
| 2 | + |
| 3 | +This guide provides a comprehensive walkthrough of how to connect a Cloud Run service to a Cloud SQL instance using the Cloud SQL Node.js Connector. It covers connecting to instances with both public and private IP addresses and demonstrates how to handle database credentials securely. |
| 4 | + |
| 5 | +## Develop a Node.js Application |
| 6 | + |
| 7 | +The following Node.js applications demonstrate how to connect to a Cloud SQL instance using the Cloud SQL Node.js Connector. |
| 8 | + |
| 9 | +### `mysql2/index.cjs` and `pg/index.mjs` |
| 10 | + |
| 11 | +These files contain the core application logic for connecting to a Cloud SQL for MySQL or PostgreSQL instance. They provide two separate authentication methods, each exposed at a different route: |
| 12 | +- `/`: Password-based authentication |
| 13 | +- `/iam`: IAM-based authentication |
| 14 | + |
| 15 | +### `tedious/index.ts` |
| 16 | + |
| 17 | +This file contains the core application logic for connecting to a Cloud SQL for SQL Server instance. It uses the `cloud-sql-nodejs-connector` to create a database connection pool with password-based authentication at the `/` route. |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> |
| 21 | +> Cloud SQL for SQL Server does not support IAM database authentication. |
| 22 | +
|
| 23 | +## Lazy Instantiation |
| 24 | + |
| 25 | +In a Cloud Run service, global variables are initialized when the container instance starts up. The application instance then handles subsequent requests until the container is spun down. |
| 26 | + |
| 27 | +The `Connector` and `knex` objects are defined as global variables (initially set to `null`) and are lazily instantiated (created only when needed) inside the request handlers. |
| 28 | + |
| 29 | +This approach offers several benefits: |
| 30 | + |
| 31 | +1. **Faster Startup:** By deferring initialization until the first request, the Cloud Run service can start listening for requests almost immediately, reducing cold start latency. |
| 32 | +2. **Resource Efficiency:** Expensive operations, like establishing background connections or fetching secrets, are only performed when actually required. |
| 33 | +3. **Connection Reuse:** Once initialized, the global `Connector` and `knex` instances are reused for all subsequent requests to that container instance. This prevents the overhead of creating new connections for every request and avoids hitting connection limits. |
| 34 | + |
| 35 | +## IAM Authentication Prerequisites |
| 36 | + |
| 37 | +For IAM authentication to work, you must ensure two things: |
| 38 | + |
| 39 | +1. **The Cloud Run service's service account has the `Cloud SQL Client` role.** You can grant this role with the following command: |
| 40 | + ```bash |
| 41 | + gcloud projects add-iam-policy-binding PROJECT_ID \ |
| 42 | + --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \ |
| 43 | + --role="roles/cloudsql.client" |
| 44 | + ``` |
| 45 | + Replace `PROJECT_ID` with your Google Cloud project ID and `SERVICE_ACCOUNT_EMAIL` with the email of the service account your Cloud Run service is using. |
| 46 | + |
| 47 | +2. **The service account is added as a database user to your Cloud SQL instance.** You can do this with the following command: |
| 48 | + ```bash |
| 49 | + gcloud sql users create SERVICE_ACCOUNT_EMAIL \ |
| 50 | + --instance=INSTANCE_NAME \ |
| 51 | + --type=cloud_iam_user |
| 52 | + ``` |
| 53 | + Replace `SERVICE_ACCOUNT_EMAIL` with the same service account email and `INSTANCE_NAME` with your Cloud SQL instance name. |
| 54 | + |
| 55 | +## Deploy the Application to Cloud Run |
| 56 | + |
| 57 | +Follow these steps to deploy the application to Cloud Run. |
| 58 | + |
| 59 | +### Build and Push the Docker Image |
| 60 | + |
| 61 | +1. **Enable the Artifact Registry API:** |
| 62 | + |
| 63 | + ```bash |
| 64 | + gcloud services enable artifactregistry.googleapis.com |
| 65 | + ``` |
| 66 | + |
| 67 | +2. **Create an Artifact Registry repository:** |
| 68 | + |
| 69 | + ```bash |
| 70 | + gcloud artifacts repositories create REPO_NAME \ |
| 71 | + --repository-format=docker \ |
| 72 | + --location=REGION |
| 73 | + ``` |
| 74 | + |
| 75 | +3. **Configure Docker to authenticate with Artifact Registry:** |
| 76 | + |
| 77 | + ```bash |
| 78 | + gcloud auth configure-docker REGION-docker.pkg.dev |
| 79 | + ``` |
| 80 | + |
| 81 | +4. **Build the Docker image (replace `mysql2` with `pg` or `tedious` as needed):** |
| 82 | + |
| 83 | + ```bash |
| 84 | + docker build -t REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME mysql2 |
| 85 | + ``` |
| 86 | + |
| 87 | +5. **Push the Docker image to Artifact Registry:** |
| 88 | + |
| 89 | + ```bash |
| 90 | + docker push REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME |
| 91 | + ``` |
| 92 | + |
| 93 | +### Deploy to Cloud Run |
| 94 | + |
| 95 | +Deploy the container image to Cloud Run using the `gcloud run deploy` command. |
| 96 | + |
| 97 | +**Sample Values:** |
| 98 | +* `SERVICE_NAME`: `my-cloud-run-service` |
| 99 | +* `REGION`: `us-central1` |
| 100 | +* `PROJECT_ID`: `my-gcp-project-id` |
| 101 | +* `REPO_NAME`: `my-artifact-repo` |
| 102 | +* `IMAGE_NAME`: `my-app-image` |
| 103 | +* `INSTANCE_CONNECTION_NAME`: `my-gcp-project-id:us-central1:my-instance-name` |
| 104 | +* `DB_USER`: `my-db-user` (for password-based authentication) |
| 105 | +* `DB_IAM_USER`: `my-service-account@my-gcp-project-id.iam.gserviceaccount.com` (for IAM-based authentication) |
| 106 | +* `DB_NAME`: `my-db-name` |
| 107 | +* `DB_PASSWORD`: `my-user-pass-name` |
| 108 | +* `VPC_NETWORK`: `my-vpc-network` |
| 109 | +* `SUBNET_NAME`: `my-vpc-subnet` |
| 110 | + |
| 111 | +**For MySQL and PostgreSQL (Public IP):** |
| 112 | + |
| 113 | +```bash |
| 114 | +gcloud run deploy SERVICE_NAME \ |
| 115 | + --image=REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME \ |
| 116 | + --set-env-vars=DB_USER=DB_USER,DB_IAM_USER=DB_IAM_USER,DB_NAME=DB_NAME,INSTANCE_CONNECTION_NAME=INSTANCE_CONNECTION_NAME \ |
| 117 | + --region=REGION \ |
| 118 | + --update-secrets=DB_PASSWORD=DB_PASSWORD:latest |
| 119 | +``` |
| 120 | + |
| 121 | +**For MySQL and PostgreSQL (Private IP):** |
| 122 | + |
| 123 | +```bash |
| 124 | +gcloud run deploy SERVICE_NAME \ |
| 125 | + --image=REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME \ |
| 126 | + --set-env-vars=DB_USER=DB_USER,DB_IAM_USER=DB_IAM_USER,DB_NAME=DB_NAME,INSTANCE_CONNECTION_NAME=INSTANCE_CONNECTION_NAME,IP_TYPE=PRIVATE \ |
| 127 | + --network=VPC_NETWORK \ |
| 128 | + --subnet=SUBNET_NAME \ |
| 129 | + --vpc-egress=private-ranges-only \ |
| 130 | + --region=REGION \ |
| 131 | + --update-secrets=DB_PASSWORD=DB_PASSWORD:latest |
| 132 | +``` |
| 133 | + |
| 134 | +**For SQL Server (Public IP):** |
| 135 | + |
| 136 | +```bash |
| 137 | +gcloud run deploy SERVICE_NAME \ |
| 138 | + --image=REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME \ |
| 139 | + --set-env-vars=DB_USER=DB_USER,DB_NAME=DB_NAME,INSTANCE_CONNECTION_NAME=INSTANCE_CONNECTION_NAME \ |
| 140 | + --region=REGION \ |
| 141 | + --update-secrets=DB_PASSWORD=DB_PASSWORD:latest |
| 142 | +``` |
| 143 | + |
| 144 | +**For SQL Server (Private IP):** |
| 145 | + |
| 146 | +```bash |
| 147 | +gcloud run deploy SERVICE_NAME \ |
| 148 | + --image=REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME \ |
| 149 | + --set-env-vars=DB_USER=DB_USER,DB_NAME=DB_NAME,INSTANCE_CONNECTION_NAME=INSTANCE_CONNECTION_NAME,IP_TYPE=PRIVATE \ |
| 150 | + --network=VPC_NETWORK \ |
| 151 | + --subnet=SUBNET_NAME \ |
| 152 | + --vpc-egress=private-ranges-only \ |
| 153 | + --region=REGION \ |
| 154 | + --update-secrets=DB_PASSWORD=DB_PASSWORD:latest |
| 155 | +``` |
| 156 | + |
| 157 | +> [!NOTE] |
| 158 | +> **`For PSC connections`** |
| 159 | +> |
| 160 | +> To connect to the Cloud SQL instance with PSC connection type, create a PSC endpoint, a DNS zone and DNS record for the instance in the same VPC network as the Cloud Run service and replace the `IP_TYPE` in the deploy command with `PSC`. To configure DNS records, refer to [Connect to an instance using Private Service Connect](https://docs.cloud.google.com/sql/docs/mysql/configure-private-service-connect) guide |
0 commit comments