Serverless on Google Cloud Platform

Vikram Shinde
Jul 8, 2020 · 5 min read

The full stack app using serverless services on GCP.

Serverless Architecture

Introduction

I have created an app using serverless services by GCP.

  1. Receives the file in Google Cloud Storage.
  2. Process the file using Google Cloud Function.
  3. Store the data in Cloud Datastore/Firestore.
  4. Publish a message to the asynchronous messaging Cloud Pub/Sub.
  5. This triggers another Google Cloud Function as subscriber.
  6. The Source code is maintained in Cloud Source Repository.
  7. Once code is commited and pushed, it triggers Cloud Build.
  8. It builds the code and creates the docker image on Container Registry.
  9. This deploys the new version on Cloud Run.
  10. Monitor the performance using Stackdriver Logging, Alerting, Debugging and Monitoring.

Services

  • Cloud Run: serverless containers (CaaS)
  • App Engine: serverless platforms (PaaS)
  • Cloud Functions: serverless functions (FaaS)

Cloud Functions

Good Fit Characteristics

  • “Glueing” systems together
  • Deploying simple APIs

Bad Fit Characteristics

  • Deploying large, complex APIs
  • Systems that require a high level of control or need custom runtimes or binaries

Cloud Run

Good Fit Characteristics

  • Event-driven applications and systems
  • Applications that require custom system and language dependencies

Bad Fit Characteristics

  • Compliance requirements that demand strict controls over the low-level environment and infrastructure (might be okay with the Knative GKE mode)

CI/CD

The CI/CD pipeline is created to deploy code on each commit.

Deploy this serverless app using Terraform

Setup

  • Terraform: This guide uses Terraform to deploy resources.
  • Git: Git is used to clone the example code and trigger new deployments.
  • GCP: You will need a GCP account with billing enabled.

Create GCP Project

Create Service Account

  • Assign the roles: Editor, Security Admin and Source Repository Administrator.
  • Download the key and renamed it as terraform-key.json

Clone the Repository

$ git clone https://github.com/vikramshinde12/serverless-in-gcp.git$ cd serverless-in-gcp/terraform

Next, copy the terrform.tfvars.example file to terraform.tfvars. You will need to replace the value of the project variables.

Then, copy the service account key terraform-key.json to this folder.

Execute following command to set Google Credentials.

$ export GOOGLE_CLOUD_KEYFILE_JSON=terraform-key.json

Execute Terraform scripts

First initialize the Terraform.

$ terraform init
Initializing Terraform
$ terraform plan
Terraform plan

Now, apply the changed to the GCP platform.

$ terraform apply

The output will show you the progress and eventually the URL.

terraform apply

Click on the URL, this will open the sample application.

$ terraform output url

So far we’ve deployed full stack with a Sample Google App, however, in the next step we’ll deploy a custom app of our own using CI/CD pipeline

Deploying a Custom App

$ cd ..

Then run the following commands to add our Cloud Source Repository as a Git remote:

$ git remote add google https://source.developers.google.com/p/[PROJECT_ID]/r/[REPO_NAME]

Note: You must substitute [PROJECT_ID] with your GCP Project ID and [REPO_NAME] with your Cloud Source Repositories repo name. You can get the repo url from output of Terraform in previous step.

Now, it’s time to push the app source code to our Cloud Source Repo.

Google Cloud Build will trigger new build and automatically deploy the resulting Docker container on Cloud Run. This is handled automatically as cloudbuild.yaml file in the root of the repository.

Push the code

git push --all google

Access the Sample Application

  • Go to Firestore
  • Select Native mode
  • Click on Create Database.

2. Upload the csv file in to source-bucket, containing id, name, dept values.

3. This will trigger cloud function which populate data in Firestore database.

4. Hit the API on the Cloud run app to fetch the data from the table.

Hit GET API /id/<>

Clean Up

$ terraform destroy

Next, delete the Terraform Admin project and all of its resources:

$ gcloud projects delete [project_id]

Conclusion

Terraform is great because of its vibrant open source community, its simple module paradigm and the fact that it’s cloud agnostic.

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Vikram Shinde

Written by

Cloud & DevOps Enthusiast. My old blogs can be found at below links http://vikshinde.blogspot.com/

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Vikram Shinde

Written by

Cloud & DevOps Enthusiast. My old blogs can be found at below links http://vikshinde.blogspot.com/

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store