https://github.com/amaziahub/gcp-py-cloud-function
A walk through of creating and deploying a simple gcp could function
https://github.com/amaziahub/gcp-py-cloud-function
automation cloud-function cloud-functions cloudcomputing cloudfunctions developerexperience devops function gcloud gcp gcp-cloud-functions googlecloud infrastructureascode python serverless terraform
Last synced: about 2 months ago
JSON representation
A walk through of creating and deploying a simple gcp could function
- Host: GitHub
- URL: https://github.com/amaziahub/gcp-py-cloud-function
- Owner: amaziahub
- License: apache-2.0
- Created: 2024-12-10T14:22:33.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-12-10T15:54:11.000Z (7 months ago)
- Last Synced: 2025-05-01T01:54:26.003Z (about 2 months ago)
- Topics: automation, cloud-function, cloud-functions, cloudcomputing, cloudfunctions, developerexperience, devops, function, gcloud, gcp, gcp-cloud-functions, googlecloud, infrastructureascode, python, serverless, terraform
- Language: HCL
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**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.




## Prerequisites
1. A Google Cloud account with billing enabled.
2. Python 3.10 or later installed locally.
3. `gcloud` CLI installed and initialized:
- Follow the [GCloud CLI Installation Guide](https://cloud.google.com/sdk/docs/install).---
## Setup Instructions
### Step 1: Clone This Repository
```bash
git clone [email protected]:amaziahub/gcp-py-cloud-function.git
cd gcp-py-cloud-function
```### Step 2: Create a Google Cloud Project
- Go to the [Google Cloud Console](https://console.cloud.google.com/)
- Create a new project
- Note the project ID for later use---
## Deploying the Function
### Step 3: Initialize `gcloud` CLI
1. Set your project ID:
```bash
gcloud config set project YOUR_PROJECT_ID
```2. Enable necessary APIs:
```bash
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
```---
### Step 4: Deploy the Cloud Function using `gcloud`
Run the following command to deploy the function:
```bash
gcloud functions deploy helloWorld \
--runtime python310 \
--trigger-http \
--allow-unauthenticated \
--entry-point hello_world
```---
## Deploying with Terraform
If you prefer to manage your Google Cloud Function deployment using **Terraform**, follow these steps:### Step 1: Install Terraform
Ensure that Terraform is installed on your machine. If not, follow the [Terraform installation guide](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli).### Step 2: Configure Your Google Cloud Project in Terraform
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.### Step 3: Deploy with Terraform
1. Initialize Terraform:
```bash
make init
```
2. Apply the configuration to deploy the function:
```bash
make apply
```## Testing the Function
### Step 5: Get the Function URL
Retrieve the function URL:
```bash
gcloud functions describe helloWorld --format='value(httpsTrigger.url)'
```### Step 6: Test the Function
Use `curl` to test the function:
```bash
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "Amazia"}' \
FUNCTION_URL
```
Replace `FUNCTION_URL` with the URL retrieved in the previous step.---
## Logs and Debugging
### View Logs
```bash
gcloud functions logs read helloWorld
```
---## Cleanup
### Delete the Function
To avoid incurring costs, delete the deployed function:
#### with `gcloud`
```bash
gcloud functions delete helloWorld
```
#### with `terraform`
```bash
make destroy
```---
Happy Coding! 🚀
---
## How to Use It
```yaml
---### 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!```