Skip to content

Commit ecb6947

Browse files
feat(cloudsql/postgres): update to v2 samples (GoogleCloudPlatform#7831)
1 parent d2b54e3 commit ecb6947

File tree

14 files changed

+584
-426
lines changed

14 files changed

+584
-426
lines changed

cloud-sql/postgres/sqlalchemy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ COPY ./certs /app/certs
3737
# webserver, with one worker process and 8 threads.
3838
# For environments with multiple CPU cores, increase the number of workers
3939
# to be equal to the cores available.
40-
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
40+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

cloud-sql/postgres/sqlalchemy/README.md

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,38 @@ shown below.
3535
Use these terminal commands to initialize environment variables:
3636
```bash
3737
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=''
4243
```
4344
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
4445
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
4546
help keep secrets safe.
4647

4748
Then 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
5354
Use these PowerShell commands to initialize environment variables:
5455
```powershell
5556
$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=""
6062
```
6163
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
6264
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
6365
help keep secrets safe.
6466

6567
Then use this command to launch the proxy in a separate PowerShell session:
6668
```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="
6870
```
6971

7072
### Launch proxy with Unix Domain Socket
@@ -79,26 +81,21 @@ sudo mkdir /path/to/the/new/directory
7981
sudo 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-
8784
Use these terminal commands to initialize other environment variables as well:
8885
```bash
8986
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>'
9491
```
9592
Note: Saving credentials in environment variables is convenient, but not secure - consider a more
9693
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to
9794
help keep secrets safe.
9895

9996
Then 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:
107104
virtualenv --python python3 env
108105
source env/bin/activate
109106
pip install -r requirements.txt
110-
`````
107+
```
111108

112109
Finally, start the application:
113110
```bash
114-
python main.py
111+
python app.py
115112
```
116113

117114
Navigate 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

121118
To 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

124121
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.
126138
127139
Next, the following command will deploy the application to your Google Cloud project:
140+
128141
```bash
129142
gcloud app deploy app.standard.yaml
130143
```
131144

132-
## Google App Engine Flexible
145+
## Deploy to App Engine Flexible
133146

134147
To 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

137150
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+
cloud_sql_instances: ::
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
141172
Next, the following command will deploy the application to your Google Cloud project:
173+
142174
```bash
143175
gcloud app deploy app.flexible.yaml
144176
```
@@ -157,45 +189,58 @@ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
157189
2. 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 '::' \
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=''
161198
```
162199

163200
Take 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-
```
173202
Replace environment variables with the correct values for your Cloud SQL
174203
instance configuration.
175204

176-
This step can be done as part of deployment but is separated for clarity.
177-
178205
It is recommended to use the [Secret Manager integration](https://cloud.google.com/run/docs/configuring/secrets) for Cloud Run instead
179206
of using environment variables for the SQL configuration. The service injects the SQL credentials from
180207
Secret Manager at runtime via an environment variable.
181208

182209
Create 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

188215
Deploy the service to Cloud Run specifying the env var name and secret name:
189216
```sh
190217
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,\
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

200227
For more details about using Cloud Run see http://cloud.run.
201228
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+
```

cloud-sql/postgres/sqlalchemy/app.flexible.yaml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
runtime: python
15+
runtime: custom
1616
env: flex
17-
entrypoint: gunicorn -b :$PORT main:app
17+
entrypoint: gunicorn -b :$PORT app:app
1818

19-
runtime_config:
20-
python_version: 3
21-
22-
# Choose to enable either a TCP or Unix domain socket for your database connection
23-
# # Enable a Unix domain socket
24-
# beta_settings:
25-
# cloud_sql_instances: ::
26-
27-
# Enable a TCP domain socket
28-
beta_settings:
29-
cloud_sql_instances: ::=tcp:
30-
31-
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
32-
# something like https://cloud.google.com/secret-manager/docs/overview to help keep
33-
# secrets secret.
19+
# Note: Saving credentials in environment variables is convenient, but not
20+
# secure - consider a more secure solution such as
21+
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
22+
# keep secrets safe.
3423
env_variables:
35-
INSTANCE_CONNECTION_NAME: PROJECT>::<MY-DATABASE>
36-
DB_USER: my-db-user
37-
DB_PASS: my-db-pass
38-
DB_NAME: my_db
24+
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>::<INSTANCE-NAME>
25+
DB_USER:
26+
DB_PASS:
27+
DB_NAME:
3928

40-
# TCP connection setup; remove if using a Unix domain socket instead
41-
DB_HOST: 172.17.0.1:>
29+
beta_settings:
30+
cloud_sql_instances: ::>

0 commit comments

Comments
 (0)