You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/languages/python/index.md
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -242,7 +242,7 @@ functions:
242
242
+ ADDITIONAL_PACKAGE: "libpq-dev gcc python3-dev"
243
243
```
244
244
245
-
### Example with Postgresql
245
+
### Example with PostgreSQL
246
246
247
247
stack.yml
248
248
@@ -258,22 +258,34 @@ functions:
258
258
image: pgfn:latest
259
259
build_options:
260
260
- libpq
261
+
environment:
262
+
db_host: "postgresql.default.svc.cluster.local"
263
+
secrets:
264
+
- db-password
261
265
```
262
266
263
-
Alternatively you can specify `ADDITIONAL_PACKAGE` in the `build_args` section for the function.
267
+
The `build_options: libpq` shorthand installs the packages needed to compile `psycopg2`. If you need more control over which packages are installed, you can use `build_args` instead:
264
268
265
269
```yaml
266
270
build_args:
267
271
ADDITIONAL_PACKAGE: "libpq-dev gcc python3-dev"
268
272
```
269
273
274
+
The database host is set as an environment variable so it can be changed per deployment without rebuilding the image. The database password is stored as an [OpenFaaS secret](/reference/secrets/) to keep it out of environment variables and the function image.
Create a database and table to use with the example:
277
289
278
290
```sql
279
291
CREATE DATABASE main;
@@ -284,20 +296,33 @@ CREATE TABLE users (
284
296
name TEXT,
285
297
);
286
298
287
-
-- Insert the original Postgresql author's name into the test table:
299
+
-- Insert the original PostgreSQL author's name into the test table:
288
300
289
301
INSERT INTO users (name) VALUES ('Michael Stonebraker');
290
302
```
291
303
292
304
handler.py:
293
305
306
+
The handler reads the database password from the mounted secret and the host from the `db_host` environment variable set in `stack.yaml`. It opens a connection, queries the `users` table, and returns the results.
withopen("/var/openfaas/secrets/"+ name, "r") as f:
344
+
return f.read().strip()
316
345
```
317
346
318
-
Always read the secret from an OpenFaaS secret at `/var/openfaas/secrets/secret-name`. The use of environment variables is an anti-pattern and will be visible via the OpenFaaS API.
347
+
Always read secrets from an OpenFaaS secret at `/var/openfaas/secrets/secret-name`. The use of environment variables for sensitive values is an anti-pattern — they are visible via the OpenFaaS API.
0 commit comments