|
| 1 | +# Google Cloud Function HTTP request Python example |
| 2 | + |
| 3 | +This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). |
| 4 | + |
| 5 | +It handles a Google Cloud Function that responds to an HTTP request. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. |
| 10 | + |
| 11 | +* The code was written for Python 3 and Google Cloud Python Client Library. |
| 12 | + |
| 13 | +## Using the code |
| 14 | + |
| 15 | +* Access the Google Cloud console. |
| 16 | + |
| 17 | +* Create a Google Cloud Function: |
| 18 | + * Name: `<CLOUD_FUNCTION_NAME>` |
| 19 | + * Memory allocated: `256 MB` |
| 20 | + * Trigger: `HTTP` |
| 21 | + * URL: `https://<GOOGLE_CLOUD_REGION>-<PROJECT>.cloudfunctions.net/<CLOUD_FUNCTION_NAME>` |
| 22 | + * Source code. You can use 2 options: |
| 23 | + * Inline editor: |
| 24 | + Edit the code of the `main.py` in the browser. |
| 25 | + * ZIP upload: |
| 26 | + Upload a ZIP file containing the `main.py` and `requirements.txt` files. |
| 27 | + * ZIP file: `<ZIP_LOCAL_NAME>` |
| 28 | + * Stage bucket: `<BUCKET_NAME_FOR_STAGGING>` |
| 29 | + * Runtime: `Python 3.7 (Beta)` |
| 30 | + * Function to execute: `http_request` |
| 31 | + * Region: `<GOOGLE_CLOUD_REGION>` |
| 32 | + * Timeout: `60 seconds` |
| 33 | + |
| 34 | +* Save the Google Cloud Function. |
| 35 | + |
| 36 | + The function is deployed and run. |
| 37 | + |
| 38 | +* Test the function. |
| 39 | + |
| 40 | + You can test it in 2 ways: |
| 41 | + |
| 42 | + * First way: Test the funcion functionality in the Google Cloud console. |
| 43 | + |
| 44 | + Go to the `Function details` and select `Testing`. |
| 45 | + |
| 46 | + Enter the `Triggering event` content: |
| 47 | + |
| 48 | + { |
| 49 | + "message": "Hello Peter!" |
| 50 | + } |
| 51 | + |
| 52 | + Click `Test the function`. |
| 53 | + |
| 54 | + You should see the next message in the Google Cloud `console output`: |
| 55 | + |
| 56 | + ```bash |
| 57 | + Hello Peter! |
| 58 | + ``` |
| 59 | + |
| 60 | + * Second way: Test the function with a browser. |
| 61 | + |
| 62 | + Go to the URL: `https://<GOOGLE_CLOUD_REGION>-<PROJECT>.cloudfunctions.net/<CLOUD_FUNCTION_NAME>` using a browser. |
| 63 | + |
| 64 | + You should see the response: |
| 65 | + |
| 66 | + ```bash |
| 67 | + Hello World! |
| 68 | + ``` |
0 commit comments