Skip to content

Commit 1154c7f

Browse files
authored
cloud-sql (postgres): add Cloud Run support (GoogleCloudPlatform#1509)
1 parent d7ae96f commit 1154c7f

4 files changed

Lines changed: 73 additions & 7 deletions

File tree

cloud-sql/mysql/mysql/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,27 @@ gcloud app browse
9292

9393
## Deploy to Cloud Run
9494

95-
See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql)
95+
See the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run)
9696
for more details on connecting a Cloud Run service to Cloud SQL.
9797

9898
1. Build the container image:
9999

100100
```sh
101-
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql
101+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
102102
```
103103

104104
2. Deploy the service to Cloud Run:
105105

106106
```sh
107-
gcloud beta run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
107+
gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql
108108
```
109109

110110
Take note of the URL output at the end of the deployment process.
111111

112112
3. Configure the service for use with Cloud Run
113113

114114
```sh
115-
gcloud beta run services update run-mysql \
115+
gcloud beta run services update run-sql \
116116
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
117117
--set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
118118
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]

cloud-sql/postgres/knex/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2019 Google LLC. All rights reserved.
2+
# Use of this source code is governed by the Apache 2.0
3+
# license that can be found in the LICENSE file.
4+
5+
# Use the official lightweight Node.js 10 image.
6+
# https://hub.docker.com/_/node
7+
FROM node:10-slim
8+
9+
# Create and change to the app directory.
10+
WORKDIR /usr/src/app
11+
12+
# Copy application dependency manifests to the container image.
13+
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
14+
# Copying this separately prevents re-running npm install on every code change.
15+
COPY package*.json ./
16+
17+
# Install dependencies.
18+
# If you add a package-lock.json speed your build by switching to 'npm ci'.
19+
# RUN npm ci --only=production
20+
RUN npm install --production
21+
22+
# Copy local code to the container image.
23+
COPY . ./
24+
25+
# Run the web service on container startup.
26+
CMD [ "npm", "start" ]
27+

cloud-sql/postgres/knex/README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ and password that you specify for the default 'postgres' user.
1717
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
1818
Download a JSON key to use to authenticate your connection.
1919

20-
2120
5. Use the information noted in the previous steps:
2221
```bash
2322
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
@@ -29,10 +28,12 @@ export DB_NAME='my_db'
2928
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
3029
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe.
3130

32-
## Running locally
31+
## Initialize the Cloud SQL database
32+
33+
Setting up the Cloud SQL database for the app requires setting up the app for local use.
3334

3435
1. To run this application locally, download and install the `cloud_sql_proxy` by
35-
following the instructions [here](https://cloud.google.com/sql/docs/postgres/sql-proxy#install).
36+
[following the instructions](https://cloud.google.com/sql/docs/postgres/sql-proxy#install).
3637

3738
Once the proxy is ready, use the following command to start the proxy in the
3839
background:
@@ -114,3 +115,39 @@ Navigate towards `http://127.0.0.1:8080` to verify your application is running c
114115
gcloud app browse
115116
```
116117
118+
## Deploy to Cloud Run
119+
120+
See the [Cloud Run documentation](https://cloud.google.com/sql/docs/postgres/connect-run)
121+
for more details on connecting a Cloud Run service to Cloud SQL.
122+
123+
1. Build the container image:
124+
125+
```sh
126+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
127+
```
128+
129+
2. Deploy the service to Cloud Run:
130+
131+
```sh
132+
gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql
133+
```
134+
135+
Take note of the URL output at the end of the deployment process.
136+
137+
3. Configure the service for use with Cloud Run
138+
139+
```sh
140+
gcloud beta run services update run-sql \
141+
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
142+
--set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
143+
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
144+
```
145+
Replace environment variables with the correct values for your Cloud SQL
146+
instance configuration.
147+
148+
This step can be done as part of deployment but is separated for clarity.
149+
150+
4. Navigate your browser to the URL noted in step 2.
151+
152+
For more details about using Cloud Run see http://cloud.run.
153+
Review other [Node.js on Cloud Run samples](../../../run/).

run/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
|[Image Processing][image_processing] | Event-driven image analysis & transformation | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_image_processing] |
1515
|[Manual Logging][manual_logging] | Structured logging without client library | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_manual_logging] |
1616
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | - |
17+
|[Cloud SQL (Postgres)][postgres] | Use Postgres with Cloud Run | - |
1718
|[Hello Broken][hello_broken] | Something is wrong, how do you fix it? | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_hello_broken] |
1819

1920
For more Cloud Run samples beyond Node.js, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
@@ -118,6 +119,7 @@ for more information.
118119
[image_processing]: image-processing/
119120
[manual_logging]: logging-manual/
120121
[mysql]: ../cloud-sql/mysql/mysql
122+
[postgres]: ../cloud-sql/postgres/knex
121123
[hello_broken]: hello-broken/
122124
[run_button_helloworld]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/knative/docs&cloudshell_working_dir=docs/serving/samples/hello-world/helloworld-nodejs
123125
[run_button_system_package]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/nodejs-docs-samples&cloudshell_working_dir=run/system-package

0 commit comments

Comments
 (0)