GCP Cloud Function: Turn On/Off Compute Engine instance

As part of the plan to be able to automatically shut down the Minecraft server during work/sleep hours when nobody is on, I needed a way for the players to be able to turn on the server on demand. I didn’t want to give any access to the Google Cloud environment or the Compute Engine directly. I found this Medium article on how to Start/Stop Compute Engine instances using a Cloud Function. It referenced this Node.js guide which shows how to access Compute Engine. Here are the steps to create the Cloud Function:

  • To create the Cloud Function, go to Compute > Cloud Functions, and create a function.
  • Give it a name, select the region that your Compute Engine instance is running in, and leave the trigger type as HTTP.
  • In my case, I set “Allow unauthenticated invocations” as I wanted any of the users to be able to turn on the server through a web link.
  • Drop down “Variables, Networking, and Advanced Settings” and adjust any settings needed. I left the default 256MB memory allocated, 60 second timeout, and left the default App Engine service account.
  • Save the Trigger URL, as this is the link you will use to initiate the Cloud Function.
  • Hit “Next” to go to the Code section.
  • For the code, you will need an index.js file, and a package.json file.

index.js:

var http = require('http');
var Compute = require('@google-cloud/compute');
var compute = Compute();
exports.startInstance = function startInstance(req, res) {
    var zone = compute.zone('instance zone here');
    var vm = zone.vm('instance name here');
    vm.start(function(err, operation, apiResponse) {
        console.log('Instance start successfully');
    });
res.status(200).send('Success start instance');
};

package.json

{
    "name": "sample-http",
    "dependencies": {
        "@google-cloud/compute": "0.7.1"
    },
    "version": "0.0.1"
}
  • Make sure to set the Entry Point as startInstance, so the code executes at the correct point. The Runtime should be Node.js 10.
  • Deploy the function. It may take a few minutes to load.
  • Test the Trigger URL. After waiting a few seconds, you should get a white screen with “Success start instance”, and the virtual machine should start up.
  • If you run into problems, go into Logging > Log Viewer, and drill down to your specific Cloud Function using the drop down and log timeframe. For example, this is what my logging showed because I had not changed the Entry Point properly:

Although I am not using it, if you want to create a separate Cloud Function to shut down your Compute Engine instance, here is the code:

index.js:

var Compute = require('@google-cloud/compute');
var compute = Compute();
exports.stopInstance = function stopInstance(req, res) {
    var zone = compute.zone('instance zone here');
    var vm = zone.vm('instance name here');
    vm.stop(function(err, operation, apiResponse) {
        console.log('instance stop successfully');
    });
    res.status(200).send('Success stop instance');
};

And that’s it! I have the URL available for my players to be able to click on to start the Minecraft server if it is off for any reason, and they need to turn it on to play.

Leave a Reply

Your email address will not be published.