This repository shows how Google Cloud Platform (GCP) cloud functions can be developed locally and automatically deployed on GCP. Please feel free to improve this approach as i am a learner as you are 😄 .
Furthermore you are welcome to read our article about this template on Medium: Click here
Created by Rakibul Haque and Christian Croseck. 👋
It is assumed that the Google Cloud SDK is already installed.
- Open your cmd.
- Switch to the current project with gcloud:
gcloud config set project your-project-name
- With gcloud allow local developments and generate a token:
gcloud auth application-default login
- A window pops up. Confirm everything. gcloud now generates a key file in json format with which our applications are automatically authenticates itself against the GCP. So no credentials need to be specified in the code!
- First, clone this repository and set it up on the Google Cloud Source Repositories .
- Clone the cloud repository you just created.
- Create a separate branch for each future cloud function.
- Under src/ you will find a main.py file and a requirements.txt.
- The main.py contains your future cloud function.
- After developing the cloud function, specify all used packages in requirements.txt.
- Under utils/ you will find a main_local.py. You can use this file to simulate a trigger. Please note the comments in the file.
- Under config/ you will find a cloudbuild.yaml file that automates the deployment of the cloud function using the Google Cloud Build service. Replace the corresponding placeholders there. Please note the comments in the file
- Push your development.
Cloud build is used to set up a CI/CD pipeline. Set up a trigger that the cloud function is redeployed with each push to its specified branch. There are the following two options for setting up a trigger:
-
Within the Google Cloud Build service on the left side click on "Trigger".
-
Click on Create Trigger
-
Please fill in everything accordingly.
- In section Event please select "Push to branch".
- In section Source please refer to your cloud repository and your branch which contains your cloud function.
- In section Configuration
- Type = "Cloud Build configuration file (yaml or json)".
- Location = /config/cloudbuild.yaml
-
Finally click on "Create".
Prerequisite: Terraform is installed. If not please download here.
- Open the file terraform/main.tf
- Replace all placeholders (capital letters) accordingly.
- Open command line and go to the project folder gcf_datalake/terraform.
- Please execute the following commands:
terraform init
(Downloads the Google components and sets the project in Terraform).terraform apply
(Runs the Terraform script and creates the resources)
- Optional: Deletion of the just created trigger with
terraform destroy
Note: With each push to the specified branch, the cloud function is automatically deployed.