site logo
search logo

Channel

Your Details



Contact Us

Blog

VM Resize Using Cloud Function and Scheduling It through Cloud Scheduler

This article helps you to resize Google Compute Engine using Google Cloud Function and schedule it through Cloud Scheduler(unix Cron format) in a cost-effective way and automated manner.

VM Resize Using Cloud Function and Scheduling It through Cloud Scheduler

Introduction

In this article, we will guide you on how to resize a Google Compute Engine(GCE) VM instance using Cloud Function and schedule it through Cloud Scheduler in GCP.

Google Cloud Functions is a lightweight and effective computing solution for developers to create single-purpose, stand-alone functions that respond to Cloud events. You are free from the need to manage a server or runtime environment.

You may use Google Cloud Platform’s console 

Create Google Cloud Function

Increase instance size(Machine Type) using GCF

  1. Name your function

  2. In the Trigger field, select HTTP Trigger.

  3. In the Source code field, select Inline editor.

  4. Use the Runtime dropdown to select a runtime(Node.js 8).

The function is given below: 

const Compute = require('@google-cloud/compute');

const compute = new Compute();

exports.increaseInstance = function increaseInstance(req, res) {

const zone = compute.zone('zone-name');

const vm = zone.vm('instance-name');

vm.resize('machine-type', function(err, apiResponse) {

  if (!err) {

  }

});

 

const options = {

  start: true

};};

  1. In ‘Function to execute’ field you need to set the function name same as the name  mentioned in your code(e.g. increaseInstance) .

  2. Now you need to write your own package.json.

E.g.

{

"name": "sample-http",

"dependencies": {

"@google-cloud/compute": "0.7.1"

},

"version": "0.0.1"

}

 

  1. Click on the Finish button.

 

Decrease instance size(Machine Type) using GCF

  1. Name your function

  2. In the Trigger field, select HTTP Trigger.

  3. In the Source code field, select Inline editor.

  4. Use the Runtime dropdown to select a runtime(Node.js 8).

The function is given below: 

const Compute = require('@google-cloud/compute');

const compute = new Compute();

exports.decreaseInstance = function decreaseInstance(req, res) {

const zone = compute.zone('zone-name');

const vm = zone.vm('instance-name');

vm.resize('custom-1-6144-ext', function(err, apiResponse) {

  if (!err) {

  }

});

const options = {

  start: true

};

};

 

  1. In ‘Function to execute’ field you need to set the function name same as the name mentioned in your code(e.g. decreaseInstance) .

  2. Now you need to write your own package.json.

E.g.

{

"name": "sample-http",

"dependencies": {

"@google-cloud/compute": "0.7.1"

},

"version": "0.0.1"

}

 

  1. Click on the Finish button.

 

Schedule Cloud Function using Cloud Scheduler

  1. Go to the Cloud Scheduler page and click on create job.

  2. Give the name of the scheduler.

  3. In the ‘Frequency’ field put the time-span(unix-cron format) and specify the Timezone. E.g. every minute: "* * * * *", every 3 hours: "0 */3 * * *"", every monday at 9:00: "0 9 * * 1".

  4. Set the Target as HTTP and put the Cloud Function’s url.

  5. Click on Finish.


Get our hottest stories delivered to your inbox.

Sign up for Scrabbl Newsletters to get personalized updates on top stories and viral hits.