@@ -35,36 +35,38 @@ shown below.
35
35
Use these terminal commands to initialize environment variables:
36
36
``` bash
37
37
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
38
- export DB_HOST=' 127.0.0.1:5432'
39
- export DB_USER=' '
40
- export DB_PASS=' '
41
- export DB_NAME=' '
38
+ export INSTANCE_HOST=' 127.0.0.1'
39
+ export DB_PORT=' 5432'
40
+ export DB_USER=' '
41
+ export DB_PASS=' '
42
+ export DB_NAME=' '
42
43
```
43
44
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
44
45
secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
45
46
help keep secrets safe.
46
47
47
48
Then use this command to launch the proxy in the background:
48
49
``` 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 &
50
51
```
51
52
52
53
#### Windows/PowerShell
53
54
Use these PowerShell commands to initialize environment variables:
54
55
``` powershell
55
56
$env:GOOGLE_APPLICATION_CREDENTIALS=""
56
- $env:DB_HOST="127.0.0.1:5432"
57
- $env:DB_USER=""
58
- $env:DB_PASS=""
59
- $env:DB_NAME=""
57
+ $env:INSTANCE_HOST="127.0.0.1"
58
+ $env:DB_PORT="5432"
59
+ $env:DB_USER=""
60
+ $env:DB_PASS=""
61
+ $env:DB_NAME=""
60
62
```
61
63
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
62
64
secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
63
65
help keep secrets safe.
64
66
65
67
Then use this command to launch the proxy in a separate PowerShell session:
66
68
``` powershell
67
- Start-Process -filepath "C:\" -ArgumentList "-instances=<project-id >:<region >:<instance-name >=tcp:5432 -credential_file="
69
+ Start-Process -filepath "C:\" -ArgumentList "-instances=<PROJECT-ID >:<INSTANCE-REGION >:<INSTANCE-NAME >=tcp:5432 -credential_file="
68
70
```
69
71
70
72
### Launch proxy with Unix Domain Socket
@@ -79,26 +81,21 @@ sudo mkdir /path/to/the/new/directory
79
81
sudo chown -R $USER /path/to/the/new/directory
80
82
```
81
83
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
-
87
84
Use these terminal commands to initialize other environment variables as well:
88
85
``` bash
89
86
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
90
- export INSTANCE_CONNECTION_NAME= ' PROJECT>::'
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 >::'
88
+ export DB_USER=' <YOUR_DB_USER_NAME >'
89
+ export DB_PASS=' <YOUR_DB_PASSWORD >'
90
+ export DB_NAME=' <YOUR_DB_NAME >'
94
91
```
95
92
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
96
93
secure solution such as [ Secret Manager] ( https://cloud.google.com/secret-manager/docs/overview ) to
97
94
help keep secrets safe.
98
95
99
96
Then use this command to launch the proxy in the background:
100
97
``` 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 &
102
99
```
103
100
104
101
### Testing the application
@@ -107,38 +104,73 @@ Next, setup install the requirements into a virtual environment:
107
104
virtualenv --python python3 env
108
105
source env/bin/activate
109
106
pip install -r requirements.txt
110
- `````
107
+ ```
111
108
112
109
Finally, start the application:
113
110
``` bash
114
- python main .py
111
+ python app .py
115
112
```
116
113
117
114
Navigate towards ` http://127.0.0.1:8080 ` to verify your application is running correctly.
118
115
119
- # # Google App Engine Standard
116
+ ## Deploy to App Engine Standard
120
117
121
118
To run on GAE-Standard, create an App Engine project by following the setup for these
122
119
[ instructions] ( https://cloud.google.com/appengine/docs/standard/python3/quickstart#before-you-begin ) .
123
120
124
121
First, 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/::
130
+ DB_USER :
131
+ DB_PASS :
132
+ 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.
126
138
127
139
Next, the following command will deploy the application to your Google Cloud project:
140
+
128
141
` ` ` bash
129
142
gcloud app deploy app.standard.yaml
130
143
```
131
144
132
- # # Google App Engine Flexible
145
+ ## Deploy to App Engine Flexible
133
146
134
147
To run on GAE-Flexible, create an App Engine project by following the setup for these
135
148
[ instructions] ( https://cloud.google.com/appengine/docs/flexible/python/quickstart#before-you-begin ) .
136
149
137
150
First, 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/::
160
+ DB_USER :
161
+ DB_PASS :
162
+ DB_NAME :
163
+
164
+ beta_settings :
165
+
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.
140
171
141
172
Next, the following command will deploy the application to your Google Cloud project:
173
+
142
174
` ` ` bash
143
175
gcloud app deploy app.flexible.yaml
144
176
```
@@ -157,45 +189,58 @@ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
157
189
2 . Deploy the service to Cloud Run:
158
190
159
191
``` 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
' ::' \
194
+ --set-env-vars INSTANCE_UNIX_SOCKET=
' /cloudsql/::' \
195
+ --set-env-vars DB_USER=' ' \
196
+ --set-env-vars DB_PASS=' ' \
197
+ --set-env-vars DB_NAME=' '
161
198
```
162
199
163
200
Take note of the URL output at the end of the deployment process.
164
201
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
- ` ` `
173
202
Replace environment variables with the correct values for your Cloud SQL
174
203
instance configuration.
175
204
176
- This step can be done as part of deployment but is separated for clarity.
177
-
178
205
It is recommended to use the [ Secret Manager integration] ( https://cloud.google.com/run/docs/configuring/secrets ) for Cloud Run instead
179
206
of using environment variables for the SQL configuration. The service injects the SQL credentials from
180
207
Secret Manager at runtime via an environment variable.
181
208
182
209
Create secrets via the command line:
183
210
``` 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=-
186
213
```
187
214
188
215
Deploy the service to Cloud Run specifying the env var name and secret name:
189
216
``` sh
190
217
gcloud 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,\
193
220
DB_USER=[DB_USER_SECRET]:latest, \
194
221
DB_PASS=[DB_PASS_SECRET]:latest, \
195
222
DB_NAME=[DB_NAME_SECRET]:latest
196
223
```
197
224
198
- 4 . Navigate your browser to the URL noted in step 2.
225
+ 3 . Navigate your browser to the URL noted in step 2.
199
226
200
227
For more details about using Cloud Run see http://cloud.run .
201
228
Review 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