gcp-py-cloud-function
This project demonstrates how to create, deploy, and test a simple Google Cloud Function using Python. The function responds to HTTP requests and returns a JSON message.
- A Google Cloud account with billing enabled.
- Python 3.10 or later installed locally.
gcloudCLI installed and initialized:- Follow the GCloud CLI Installation Guide.
git clone git@github.com:amaziahub/gcp-py-cloud-function.git
cd gcp-py-cloud-function- Go to the Google Cloud Console
- Create a new project
- Note the project ID for later use
-
Set your project ID:
gcloud config set project YOUR_PROJECT_ID -
Enable necessary APIs:
gcloud services enable cloudfunctions.googleapis.com gcloud services enable cloudbuild.googleapis.com
Run the following command to deploy the function:
gcloud functions deploy helloWorld \
--runtime python310 \
--trigger-http \
--allow-unauthenticated \
--entry-point hello_worldIf you prefer to manage your Google Cloud Function deployment using Terraform, follow these steps:
Ensure that Terraform is installed on your machine. If not, follow the Terraform installation guide.
In the main.tf file, update the project and region with your own values.
You can also modify the function runtime, name, and other settings as required.
- Initialize Terraform:
make init- Apply the configuration to deploy the function:
make applyRetrieve the function URL:
gcloud functions describe helloWorld --format='value(httpsTrigger.url)'Use curl to test the function:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "Amazia"}' \
FUNCTION_URLReplace FUNCTION_URL with the URL retrieved in the previous step.
gcloud functions logs read helloWorldTo avoid incurring costs, delete the deployed function:
gcloud functions delete helloWorldmake destroyHappy Coding! 🚀
---
### How to Use It
Replace `your-username/your-repo-name` with your GitHub repository’s actual details. This README provides all the necessary steps to recreate, deploy, and test your Cloud Function, along with a structured overview of the project. Let me know if you need further customization!