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.
gcloud
CLI installed and initialized:- Follow the GCloud CLI Installation Guide.
git clone [email protected]: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_world
If 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 apply
Retrieve 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_URL
Replace FUNCTION_URL
with the URL retrieved in the previous step.
gcloud functions logs read helloWorld
To avoid incurring costs, delete the deployed function:
gcloud functions delete helloWorld
make destroy
Happy 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!