Skip to content

Instantly share code, notes, and snippets.

@vukhanhtruong
Last active November 20, 2023 04:45
Show Gist options
  • Save vukhanhtruong/cd1acd159909f4f32487dda67a6e1b98 to your computer and use it in GitHub Desktop.
Save vukhanhtruong/cd1acd159909f4f32487dda67a6e1b98 to your computer and use it in GitHub Desktop.
Google Cloud Functions to resize compute instance-group
const {Buffer} = require('safe-buffer');
const Compute = require('@google-cloud/compute');
const compute = new Compute();
/**
* Resize Compute Engine instance-group.
*
* Expects a PubSub message with JSON-formatted event data containing the
* following attributes:
* zone - the GCP zone the instances are located in.
* name - the name of a instance group.
* size - the number of instance(s) to resize
*
*
* @param {!object} event Cloud Function PubSub message event.
* @param {!object} callback Cloud Function PubSub callback indicating completion.
*/
exports.resizeInstanceGroupPubSub = (event, context, callback) => {
try {
const payload = _validatePayload(
JSON.parse(Buffer.from(event.data, 'base64').toString())
);
const zone = compute.zone(payload.zone);
const instanceGroupManager = zone.instanceGroupManager(payload.name);
instanceGroupManager.resize(payload.size).then(data => {
// Operation pending.
const operation = data[0];
const apiResponse = data[1];
console.log(apiResponse);
return operation.promise();
}).then(() => {
// Operation complete. Instance successfully stopped.
const message = 'Successfully resize to ' + payload.size + ' the instance group named ' + payload.name;
console.log(message);
callback(null, message);
}).catch(err => {
console.log(err);
callback(err);
});
} catch (err) {
console.log(err);
callback(err);
}
};
/**
* Validates that a request payload contains the expected fields.
*
* @param {!object} payload the request payload to validate.
* @return {!object} the payload object.
*/
function _validatePayload(payload) {
if (!payload.zone) {
throw new Error(`Attribute 'zone' missing from payload`);
} else if (!payload.name) {
throw new Error(`Attribute 'name' missing from payload`);
} else if (!payload.size) {
throw new Error(`Attribute 'size' missing from payload`);
}
return payload;
}
{
"name": "cloud-functions-schedule-resize-instance-group",
"version": "0.1.0",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha test/*.test.js --timeout=20000"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.3.0",
"mocha": "^6.0.0",
"proxyquire": "^2.0.0",
"sinon": "^7.0.0"
},
"dependencies": {
"@google-cloud/compute": "^1.0.0",
"safe-buffer": "^5.1.2"
}
}

For Triggering event enter the following:

{"data":"eyJ6b25lIjogImFzaWEtc291dGhlYXN0MS1jIiwgIm5hbWUiOiAieW91ci1pbnN0YW5jZS1ncm91cC1uYW1lIiwgInNpemUiOiAiMSJ9="}

This is simply the base64-encoded string for:

{"zone": "asia-southeast1-c", "name": "your-instance-group-name", "size": "1"}
@Daniel-svg
Copy link

how can I resize the Regional (multiple zone) coverage instance-group

@vukhanhtruong
Copy link
Author

@Daniel-svg List all instances (code example) in your project and resize them all.

@Daniel-svg
Copy link

I'm so sorry. How to use this example to change the Instance group

I want to change the Instance group - Number of instances

@vrealtiago
Copy link

@Daniel-svg
Copy link

@vrealtiago Sorry for the late reply. Thank you for making time for me. I really appreciate it. I will test again in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment