@@ -35,36 +35,38 @@ shown below.
3535Use these terminal commands to initialize environment variables:
3636``` bash
3737export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
38- export DB_HOST=' 127.0.0.1:5432'
39- export DB_USER=' <DB_USER_NAME>'
40- export DB_PASS=' <DB_PASSWORD>'
41- export DB_NAME=' <DB_NAME>'
38+ export INSTANCE_HOST=' 127.0.0.1'
39+ export DB_PORT=' 5432'
40+ export DB_USER=' <YOUR_DB_USER_NAME>'
41+ export DB_PASS=' <YOUR_DB_PASSWORD>'
42+ export DB_NAME=' <YOUR_DB_NAME>'
4243```
4344Note: Saving credentials in environment variables is convenient, but not secure - consider a more
4445secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
4546help keep secrets safe.
4647
4748Then use this command to launch the proxy in the background:
4849``` bash
49- ./cloud_sql_proxy -instances=< project-id > :< region > :< instance-name > =tcp:5432 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
50+ ./cloud_sql_proxy -instances=< PROJECT-ID > :< INSTANCE-REGION > :< INSTANCE-NAME > =tcp:5432 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
5051```
5152
5253#### Windows/PowerShell
5354Use these PowerShell commands to initialize environment variables:
5455``` powershell
5556$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
56- $env:DB_HOST="127.0.0.1:5432"
57- $env:DB_USER="<DB_USER_NAME>"
58- $env:DB_PASS="<DB_PASSWORD>"
59- $env:DB_NAME="<DB_NAME>"
57+ $env:INSTANCE_HOST="127.0.0.1"
58+ $env:DB_PORT="5432"
59+ $env:DB_USER="<YOUR_DB_USER_NAME>"
60+ $env:DB_PASS="<YOUR_DB_PASSWORD>"
61+ $env:DB_NAME="<YOUR_DB_NAME>"
6062```
6163Note: Saving credentials in environment variables is convenient, but not secure - consider a more
6264secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
6365help keep secrets safe.
6466
6567Then use this command to launch the proxy in a separate PowerShell session:
6668``` powershell
67- Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id >:<region >:<instance-name >=tcp:5432 -credential_file=<CREDENTIALS_JSON_FILE>"
69+ Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<PROJECT-ID >:<INSTANCE-REGION >:<INSTANCE-NAME >=tcp:5432 -credential_file=<CREDENTIALS_JSON_FILE>"
6870```
6971
7072### Launch proxy with Unix Domain Socket
@@ -79,26 +81,21 @@ sudo mkdir /path/to/the/new/directory
7981sudo chown -R $USER /path/to/the/new/directory
8082```
8183
82- You'll also need to initialize an environment variable containing the directory you just created:
83- ``` bash
84- export DB_SOCKET_DIR=/path/to/the/new/directory
85- ```
86-
8784Use these terminal commands to initialize other environment variables as well:
8885``` bash
8986export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
90- export INSTANCE_CONNECTION_NAME= ' <MY- PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
91- export DB_USER=' <DB_USER_NAME >'
92- export DB_PASS=' <DB_PASSWORD >'
93- export DB_NAME=' <DB_NAME >'
87+ export INSTANCE_UNIX_SOCKET= ' ./cloudsql/< PROJECT-ID >:<INSTANCE-REGION>:<INSTANCE-NAME>'
88+ export DB_USER=' <YOUR_DB_USER_NAME >'
89+ export DB_PASS=' <YOUR_DB_PASSWORD >'
90+ export DB_NAME=' <YOUR_DB_NAME >'
9491```
9592Note: Saving credentials in environment variables is convenient, but not secure - consider a more
9693secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
9794help keep secrets safe.
9895
9996Then use this command to launch the proxy in the background:
10097``` bash
101- ./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
98+ ./cloud_sql_proxy -dir=./cloudsql --instances=< PROJECT-ID > : < INSTANCE-REGION > : < INSTANCE-NAME > --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
10299```
103100
104101### Testing the application
@@ -107,38 +104,73 @@ Next, setup install the requirements into a virtual environment:
107104virtualenv --python python3 env
108105source env/bin/activate
109106pip install -r requirements.txt
110- `````
107+ ```
111108
112109Finally, start the application:
113110``` bash
114- python main .py
111+ python app .py
115112```
116113
117114Navigate towards ` http://127.0.0.1:8080 ` to verify your application is running correctly.
118115
119- # # Google App Engine Standard
116+ ## Deploy to App Engine Standard
120117
121118To run on GAE-Standard, create an App Engine project by following the setup for these
122119[ instructions] ( https://cloud.google.com/appengine/docs/standard/python3/quickstart#before-you-begin ) .
123120
124121First, update ` app.standard.yaml ` with the correct values to pass the environment
125- variables into the runtime.
122+ variables into the runtime. Your ` app.standard.yaml ` file should look like this:
123+
124+ ``` yaml
125+ runtime : python37
126+ entrypoint : gunicorn -b :$PORT app:app
127+
128+ env_variables :
129+ INSTANCE_UNIX_SOCKET : /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
130+ DB_USER : <YOUR_DB_USER_NAME>
131+ DB_PASS : <YOUR_DB_PASSWORD>
132+ DB_NAME : <YOUR_DB_NAME>
133+ ` ` `
134+
135+ Note: Saving credentials in environment variables is convenient, but not secure - consider a more
136+ secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
137+ help keep secrets safe.
126138
127139Next, the following command will deploy the application to your Google Cloud project:
140+
128141` ` ` bash
129142gcloud app deploy app.standard.yaml
130143```
131144
132- # # Google App Engine Flexible
145+ ## Deploy to App Engine Flexible
133146
134147To run on GAE-Flexible, create an App Engine project by following the setup for these
135148[ instructions] ( https://cloud.google.com/appengine/docs/flexible/python/quickstart#before-you-begin ) .
136149
137150First, update ` app.flexible.yaml ` with the correct values to pass the environment
138- variables into the runtime. Also update this file to configure either a TCP or a
139- Unix domain socket connection to your database.
151+ variables into the runtime. Your ` app.flexible.yaml ` file should look like this:
152+
153+ ``` yaml
154+ runtime : custom
155+ env : flex
156+ entrypoint : gunicorn -b :$PORT app:app
157+
158+ env_variables :
159+ INSTANCE_UNIX_SOCKET : /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
160+ DB_USER : <YOUR_DB_USER_NAME>
161+ DB_PASS : <YOUR_DB_PASSWORD>
162+ DB_NAME : <YOUR_DB_NAME>
163+
164+ beta_settings :
165+ cloud_sql_instances : <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
166+ ` ` `
167+
168+ Note: Saving credentials in environment variables is convenient, but not secure - consider a more
169+ secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
170+ help keep secrets safe.
140171
141172Next, the following command will deploy the application to your Google Cloud project:
173+
142174` ` ` bash
143175gcloud app deploy app.flexible.yaml
144176```
@@ -157,45 +189,58 @@ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
1571892 . Deploy the service to Cloud Run:
158190
159191``` sh
160- gcloud run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql
192+ gcloud run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
193+ --add-cloudsql-instances ' <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
194+ --set-env-vars INSTANCE_UNIX_SOCKET=' /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
195+ --set-env-vars DB_USER=' <YOUR_DB_USER_NAME>' \
196+ --set-env-vars DB_PASS=' <YOUR_DB_PASSWORD>' \
197+ --set-env-vars DB_NAME=' <YOUR_DB_NAME>'
161198```
162199
163200Take note of the URL output at the end of the deployment process.
164201
165- 3. Configure the service for use with Cloud Run
166-
167- ` ` ` sh
168- gcloud run services update run-sql \
169- --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
170- --set-env-vars INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
171- DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
172- ` ` `
173202Replace environment variables with the correct values for your Cloud SQL
174203instance configuration.
175204
176- This step can be done as part of deployment but is separated for clarity.
177-
178205It is recommended to use the [ Secret Manager integration] ( https://cloud.google.com/run/docs/configuring/secrets ) for Cloud Run instead
179206of using environment variables for the SQL configuration. The service injects the SQL credentials from
180207Secret Manager at runtime via an environment variable.
181208
182209Create secrets via the command line:
183210``` sh
184- echo -n $INSTANCE_CONNECTION_NAME | \
185- gcloud secrets create [INSTANCE_CONNECTION_NAME_SECRET ] --data-file=-
211+ echo -n $INSTANCE_UNIX_SOCKET | \
212+ gcloud secrets create [INSTANCE_UNIX_SOCKET_SECRET ] --data-file=-
186213```
187214
188215Deploy the service to Cloud Run specifying the env var name and secret name:
189216``` sh
190217gcloud beta run deploy SERVICE --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
191- --add-cloudsql-instances $INSTANCE_CONNECTION_NAME \
192- --update-secrets INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME_SECRET ]:latest,\
218+ --add-cloudsql-instances < PROJECT-ID > : < INSTANCE-REGION > : < INSTANCE-NAME > \
219+ --update-secrets INSTANCE_UNIX_SOCKET=[INSTANCE_UNIX_SOCKET_SECRET ]:latest,\
193220 DB_USER=[DB_USER_SECRET]:latest, \
194221 DB_PASS=[DB_PASS_SECRET]:latest, \
195222 DB_NAME=[DB_NAME_SECRET]:latest
196223```
197224
198- 4 . Navigate your browser to the URL noted in step 2.
225+ 3 . Navigate your browser to the URL noted in step 2.
199226
200227For more details about using Cloud Run see http://cloud.run .
201228Review other [ Python on Cloud Run samples] ( ../../../run/ ) .
229+
230+ ## Deploy to Cloud Functions
231+
232+ To deploy the service to [ Cloud Functions] ( https://cloud.google.com/functions/docs ) run the following command:
233+
234+ ``` sh
235+ gcloud functions deploy votes --runtime python39 --trigger-http --allow-unauthenticated \
236+ --set-env-vars INSTANCE_UNIX_SOCKET=/cloudsql/< PROJECT-ID> :< INSTANCE-REGION> :< INSTANCE-NAME> \
237+ --set-env-vars DB_USER=$DB_USER \
238+ --set-env-vars DB_PASS=$DB_PASS \
239+ --set-env-vars DB_NAME=$DB_NAME
240+ ```
241+
242+ Take note of the URL output at the end of the deployment process or run the following to view your function:
243+
244+ ``` sh
245+ gcloud app browse
246+ ```
0 commit comments