@@ -36,36 +36,38 @@ shown below.
3636Use these terminal commands to initialize environment variables:
3737``` bash
3838export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
39- export DB_HOST=' 127.0.0.1:3306'
40- export DB_USER=' <DB_USER_NAME>'
41- export DB_PASS=' <DB_PASSWORD>'
42- export DB_NAME=' <DB_NAME>'
39+ export INSTANCE_HOST=' 127.0.0.1'
40+ export DB_PORT=' 3306'
41+ export DB_USER=' <YOUR_DB_USER_NAME>'
42+ export DB_PASS=' <YOUR_DB_PASSWORD>'
43+ export DB_NAME=' <YOUR_DB_NAME>'
4344```
4445Note: Saving credentials in environment variables is convenient, but not secure - consider a more
4546secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
4647help keep secrets safe.
4748
4849Then use this command to launch the proxy in the background:
4950``` bash
50- ./cloud_sql_proxy -instances=< project-id > :< region > :< instance-name > =tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
51+ ./cloud_sql_proxy -instances=< PROJECT-ID > :< INSTANCE-REGION > :< INSTANCE-NAME > =tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
5152```
5253
5354#### Windows/PowerShell
5455Use these PowerShell commands to initialize environment variables:
5556``` powershell
5657$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
57- $env:DB_HOST="127.0.0.1:3306"
58- $env:DB_USER="<DB_USER_NAME>"
59- $env:DB_PASS="<DB_PASSWORD>"
60- $env:DB_NAME="<DB_NAME>"
58+ $env:INSTANCE_HOST="127.0.0.1"
59+ $env:DB_PORT="3306"
60+ $env:DB_USER="<YOUR_DB_USER_NAME>"
61+ $env:DB_PASS="<YOUR_DB_PASSWORD>"
62+ $env:DB_NAME="<YOUR_DB_NAME>"
6163```
6264Note: Saving credentials in environment variables is convenient, but not secure - consider a more
6365secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
6466help keep secrets safe.
6567
6668Then use this command to launch the proxy in a separate PowerShell session:
6769``` powershell
68- Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id >:<region >:<instance-name >=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>"
70+ Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<PROJECT-ID >:<INSTANCE-REGION >:<INSTANCE-NAME >=tcp:3306 -credential_file=<CREDENTIALS_JSON_FILE>"
6971```
7072
7173### Launch proxy with Unix Domain Socket
@@ -80,26 +82,21 @@ sudo mkdir /cloudsql
8082sudo chown -R $USER /cloudsql
8183```
8284
83- You'll also need to initialize an environment variable containing the directory you just created:
84- ``` bash
85- export DB_SOCKET_DIR=/path/to/the/new/directory
86- ```
87-
8885Use these terminal commands to initialize other environment variables as well:
8986``` bash
9087export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
91- export INSTANCE_CONNECTION_NAME= ' <MY- PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
92- export DB_USER=' <DB_USER_NAME >'
93- export DB_PASS=' <DB_PASSWORD >'
94- export DB_NAME=' <DB_NAME >'
88+ export INSTANCE_UNIX_SOCKET= ' ./cloudsql/< PROJECT-ID >:<INSTANCE-REGION>:<INSTANCE-NAME>'
89+ export DB_USER=' <YOUR_DB_USER_NAME >'
90+ export DB_PASS=' <YOUR_DB_PASSWORD >'
91+ export DB_NAME=' <YOUR_DB_NAME >'
9592```
9693Note: Saving credentials in environment variables is convenient, but not secure - consider a more
9794secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
9895help keep secrets safe.
9996
10097Then use this command to launch the proxy in the background:
10198``` bash
102- ./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
99+ ./cloud_sql_proxy -dir=./cloudsql --instances=< PROJECT-ID > : < INSTANCE-REGION > : < INSTANCE-NAME > --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
103100```
104101
105102### Testing the application
@@ -113,34 +110,69 @@ pip install -r requirements.txt
113110
114111Finally, start the application:
115112``` bash
116- python main .py
113+ python app .py
117114```
118115
119116Navigate towards ` http://127.0.0.1:8080 ` to verify your application is running correctly.
120117
121- ## Google App Engine Standard
118+ ## Deploy to App Engine Standard
122119
123120To run on GAE-Standard, create an App Engine project by following the setup for these
124121[ instructions] ( https://cloud.google.com/appengine/docs/standard/python3/quickstart#before-you-begin ) .
125122
126123First, update ` app.standard.yaml ` with the correct values to pass the environment
127- variables into the runtime.
124+ variables into the runtime. Your ` app.standard.yaml ` file should look like this:
125+
126+ ``` yaml
127+ runtime : python37
128+ entrypoint : gunicorn -b :$PORT app:app
129+
130+ env_variables :
131+ INSTANCE_UNIX_SOCKET : /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
132+ DB_USER : <YOUR_DB_USER_NAME>
133+ DB_PASS : <YOUR_DB_PASSWORD>
134+ DB_NAME : <YOUR_DB_NAME>
135+ ` ` `
136+
137+ Note: Saving credentials in environment variables is convenient, but not secure - consider a more
138+ secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
139+ help keep secrets safe.
128140
129141Next, the following command will deploy the application to your Google Cloud project:
142+
130143` ` ` bash
131144gcloud app deploy app.standard.yaml
132145```
133146
134- ## Google App Engine Flexible
147+ ## Deploy to App Engine Flexible
135148
136149To run on GAE-Flexible, create an App Engine project by following the setup for these
137150[ instructions] ( https://cloud.google.com/appengine/docs/flexible/python/quickstart#before-you-begin ) .
138151
139152First, update ` app.flexible.yaml ` with the correct values to pass the environment
140- variables into the runtime. Also update this file to configure either a TCP or a
141- Unix domain socket connection to your database.
153+ variables into the runtime. Your ` app.flexible.yaml ` file should look like this:
154+
155+ ``` yaml
156+ runtime : custom
157+ env : flex
158+ entrypoint : gunicorn -b :$PORT app:app
159+
160+ env_variables :
161+ INSTANCE_UNIX_SOCKET : /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
162+ DB_USER : <YOUR_DB_USER_NAME>
163+ DB_PASS : <YOUR_DB_PASSWORD>
164+ DB_NAME : <YOUR_DB_NAME>
165+
166+ beta_settings :
167+ cloud_sql_instances : <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
168+ ` ` `
169+
170+ Note: Saving credentials in environment variables is convenient, but not secure - consider a more
171+ secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
172+ help keep secrets safe.
142173
143174Next, the following command will deploy the application to your Google Cloud project:
175+
144176` ` ` bash
145177gcloud app deploy app.flexible.yaml
146178```
@@ -153,51 +185,64 @@ for more details on connecting a Cloud Run service to Cloud SQL.
1531851 . Build the container image:
154186
155187``` sh
156- gcloud builds submit --tag gcr.io/[ YOUR_PROJECT_ID] /run-mysql
188+ gcloud builds submit --tag gcr.io/< YOUR_PROJECT_ID> /run-mysql
157189```
158190
1591912 . Deploy the service to Cloud Run:
160192
161193``` sh
162- gcloud run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
194+ gcloud run deploy run-mysql --image gcr.io/< YOUR_PROJECT_ID> /run-mysql \
195+ --add-cloudsql-instances ' <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
196+ --set-env-vars INSTANCE_UNIX_SOCKET=' /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
197+ --set-env-vars DB_USER=' <YOUR_DB_USER_NAME>' \
198+ --set-env-vars DB_PASS=' <YOUR_DB_PASSWORD>' \
199+ --set-env-vars DB_NAME=' <YOUR_DB_NAME>'
163200```
164201
165202Take note of the URL output at the end of the deployment process.
166203
167- 3 . Configure the service for use with Cloud Run
168-
169- ``` sh
170- gcloud run services update run-mysql \
171- --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
172- --set-env-vars INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
173- DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
174- ```
175204Replace environment variables with the correct values for your Cloud SQL
176205instance configuration.
177206
178- This step can be done as part of deployment but is separated for clarity.
179-
180207It is recommended to use the [ Secret Manager integration] ( https://cloud.google.com/run/docs/configuring/secrets ) for Cloud Run instead
181208of using environment variables for the SQL configuration. The service injects the SQL credentials from
182209Secret Manager at runtime via an environment variable.
183210
184211Create secrets via the command line:
185212``` sh
186- echo -n $INSTANCE_CONNECTION_NAME | \
187- gcloud secrets create [INSTANCE_CONNECTION_NAME_SECRET ] --data-file=-
213+ echo -n $INSTANCE_UNIX_SOCKET | \
214+ gcloud secrets create [INSTANCE_UNIX_SOCKET_SECRET ] --data-file=-
188215```
189216
190217Deploy the service to Cloud Run specifying the env var name and secret name:
191218``` sh
192- gcloud beta run deploy SERVICE --image gcr.io/[ YOUR_PROJECT_ID] /run-sql \
193- --add-cloudsql-instances $INSTANCE_CONNECTION_NAME \
194- --update-secrets INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME_SECRET ]:latest,\
219+ gcloud beta run deploy SERVICE --image gcr.io/< YOUR_PROJECT_ID> /run-sql \
220+ --add-cloudsql-instances < PROJECT-ID > : < INSTANCE-REGION > : < INSTANCE-NAME > \
221+ --update-secrets INSTANCE_UNIX_SOCKET=[INSTANCE_UNIX_SOCKET_SECRET ]:latest,\
195222 DB_USER=[DB_USER_SECRET]:latest, \
196223 DB_PASS=[DB_PASS_SECRET]:latest, \
197224 DB_NAME=[DB_NAME_SECRET]:latest
198225```
199226
200- 4 . Navigate your browser to the URL noted in step 2.
227+ 3 . Navigate your browser to the URL noted in step 2.
201228
202229For more details about using Cloud Run see http://cloud.run .
203230Review other [ Python on Cloud Run samples] ( ../../../run/ ) .
231+
232+ ## Deploy to Cloud Functions
233+
234+ To deploy the service to [ Cloud Functions] ( https://cloud.google.com/functions/docs ) run the following command:
235+
236+ ``` sh
237+ gcloud functions deploy votes --runtime python39 --trigger-http --allow-unauthenticated \
238+ --set-env-vars INSTANCE_UNIX_SOCKET=/cloudsql/< PROJECT-ID> :< INSTANCE-REGION> :< INSTANCE-NAME> \
239+ --set-env-vars DB_USER=$DB_USER \
240+ --set-env-vars DB_PASS=$DB_PASS \
241+ --set-env-vars DB_NAME=$DB_NAME
242+ ```
243+
244+ Take note of the URL output at the end of the deployment process or run the following to view your function:
245+
246+ ``` sh
247+ gcloud app browse
248+ ```
0 commit comments