The idea of the July month blog is to achieve task automation in case of any breach or failure in Google Compute Instances with Cloud Functions, Pub/Sub, Cloud Build and Cloud Monitoring. We need all these Google Cloud services to achieve automation.

This blog wouldn’t be possible without a major contribution of Pranay Nanda. Pranay have an amazing in-depth knowledge and experience of Google Cloud and Cloud Solution Designing. Currently working as a Cloud Architect at MediaAgility and Organizer of GDG Cloud New Delhi, Pranay has taken numerous session on Google Cloud. He is my mentor and friend.

Image for post

Credit: Orangereebok & Me

How Google Cloud services are working together?

When some services running in VM instances got failed or stopped or crashed due to some breach or failure, the stackdriver agents in instances send the notification to Cloud Monitoring, through alerts it’ll send a notification to Mail and Pub/Sub topic which will further trigger the function. The function calls the Cloud Build API to do whatever task we want on VM; like here we’ll be ssh-ing into compute instances and create a normal text file via touch.

The VM has a normal script running that will serve as a file listener — looking for a file created by a cloud function. When the file is created it will restart the crashed or failed service and delete the normal file.

Image for post


With the combination of these tools, you can get high-level and amazing automation that drastically reduces your work and gives you time for coffee and table-tennis !!!

Let’s go…

Google Compute Instance

Google Compute Engine lets you create and run virtual machines on Google infrastructure. There are no upfront investments and you can run thousands of virtual CPUs on a system that is designed to be fast and to offer consistent performance. With Compute Engine, you have complete control over your infrastructure. You can customize operating systems, and even run applications that rely on a mix of operating systems. You can easily lift and shift your on-premises workloads into GCP without rewriting your applications or making any changes. Your VM can run Linux and Windows Server images provided by Google or customized versions of these images. And you can even import Images from many of your physical servers.

Read more about Google Compute Engine

We need one Compute Engine instance where our service is running and also need to install stackdriver agents in the VM for monitoring services.

Create one compute instance with whatever configuration you want via console or terraform or using gcloud. Here I’m going with the default configuration with allowing HTTPS and HTTP traffic and for article purpose, I’m installing apache2.

To create from gcloud, run:

gcloud compute instances create virtual-instance — zone=us-central1-a — machine-type=n1-standard-1 — tags=http-server,https-server — image=ubuntu-1804-bionic-v20200716 — image-project=ubuntu-os-cloud — boot-disk-size=10GB — boot-disk-type=pd-standard

For more arguments of gcloud compute instance create visit gcloud SDK documentation for compute instance

The service running inside an instance will be monitored by the Cloud Monitoring agent and once the service gets stopped or crashed (I’ll do stop it purposely here), the agent will alert the notification channel which triggers a sequence of events as I described below.

There is one file listener that will be running and looking for the test file in the background inside the instance.

#pub-sub #function

Automate Anything with Google Cloud Functions and Cloud Build
1.25 GEEK