Skip to content

This repository shows how Google Cloud Platform (GCP) cloud functions can be developed locally and automatically deployed on GCP.

Notifications You must be signed in to change notification settings

raki2305/gcp_cloudfunction_dev

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Cloud Function Template for local development

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. 👋

Steps

Set up automatic authentication of local GCP developments

It is assumed that the Google Cloud SDK is already installed.

  1. Open your cmd.
  2. Switch to the current project with gcloud: gcloud config set project your-project-name
  3. With gcloud allow local developments and generate a token: gcloud auth application-default login
  4. 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!

Using this Repo (Template)

  1. First, clone this repository and set it up on the Google Cloud Source Repositories .
  2. Clone the cloud repository you just created.
  3. Create a separate branch for each future cloud function.
  4. 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.
  5. 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.
  6. 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
  7. Push your development.

Using Cloud Build Service for Deployment

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:

Option 1: Set up manually via the Googe web interface

  1. Within the Google Cloud Build service on the left side click on "Trigger".

  2. Click on Create Trigger

  3. 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
  4. Finally click on "Create".

Option 2: Automated setup with Terraform (for the lazy guys ;-) )

Prerequisite: Terraform is installed. If not please download here.

  1. Open the file terraform/main.tf
  2. Replace all placeholders (capital letters) accordingly.
  3. Open command line and go to the project folder gcf_datalake/terraform.
  4. Please execute the following commands:
    1. terraform init (Downloads the Google components and sets the project in Terraform).
    2. terraform apply (Runs the Terraform script and creates the resources)
  5. Optional: Deletion of the just created trigger with terraform destroy

Note: With each push to the specified branch, the cloud function is automatically deployed.

About

This repository shows how Google Cloud Platform (GCP) cloud functions can be developed locally and automatically deployed on GCP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published