Serverless Slack Bot: Automate Tasks in Google VM Instance (Part 2)

Chamal Gomes
Oct 15 · 5 min read

This post is part of my series Serverless Slack Bot, visit Part 1 here. This post walks you through how to start/stop a GCE VM instance from slack slash commands.

1. Start VM Instance

Step 1.1: Sign in To Google Functions

Visit the Google cloud functions page, sign in with your Google account. Make sure the project in which you add the cloud function is the same as the project in which you have the VM already created.

Activate the cloud shell on the top right corner.

Give it about 1 minute to open up a CLI. Once opened type in the following commands respectively.

$ cd /tmp$ mkdir SlackApp$ cd SlackApp

Step 1.2: Create the function

Let begin by creating the function for Starting up a VM instance from the command line. The file structure is as follows,

├── SlackApp_StartVM #Project Folder
│ ├── index. #Where your primary code resides.
│ ├── package.json #Describing the dependencied for index.js
│ ├── config.json #including you slack token and user ID
  1. index.js
Index.js (StartVM)

2. package.json


3. config.json


Step 1.3: Deploy the function

$ gcloud deploy startInstanceHTTP --trigger-http --runtime=nodejs8 

Allow 1–2 minutes for the function to be deployed. Once complete you will be provided with URL for the function, copy this function as we require it when creating the slash command. It usually takes the form of https://{reigon}-{projectName}{functionName}.

Step 1.4: Create Slash Command

Next, go to your Slack App and click create slash command as follows,

In the “Request URL” section copy-paste your Cloud function URL which you obtained earlier when you deployed your function. Then click add slash command. Finally, now that you have created your first slash command make sure to install your slack app in your workspace.

That’s it, your DONE !!!!!!

Step 1.5: Test the function

Now go to your workspace and type /start VM “you VM name” , and you should see a message saying,

Instance your_VM_name: Successfully Started

you can confirm it by typing in your own computer's CLI if you have the gcloud SDK installed and configured. You will see your instance running as expected,

$ gcloud compute instances list 

If you don't have the gcloud SDK, just visit google cloud console and visit your VM instances located in the Compute engine tab on the sidebar.

2. Stop VM Instance with Slack

For stopping the VM follow Step 1.1 to 1.5 with the ONLY change being the index.js code shown below.

index.js (StopVM)

Make sure to add these functions in a separate directory in the cloud shell and deploy, keep in mind this is a separate Cloud function and you will receive a separate URL to be pasted in the Slack slash command. The deployments should be done as follows

$ gcloud deploy stopInstanceHTTP --trigger-http --runtime=nodejs8 

Analogy of Index.js

instanceName variable: to be the default name of your VM, in this case, my one is high-intensive. You can change it to your VM name obviously instead. In this case, you only have to type /startVM in the slack command to start the VM.

verify webhook function: we make sure that the request we receive is from your workspace and it's from you. You don't want anyone else starting up your VM and costing you money at the end of the month.

Parse function: to pass in the VM name in the response. I tried concatenating but it gave me a lot of trouble, hence end up creating a separate function for parsing the concatenated response with your VM name.

Start/Stop HTTP function: have several error catchers and flag variables to catch separate errors. Such as “invalid credentials”, “VM does not exist”, “Only posts requests accepted”, etc..

Wait for method: This is to make sure that your VM started/stopped entirely before sending you the message to slack. Some of the posts which I saw Medium and elsewhere tend to disregard this important aspect. In some Google Cloud function tutorials which I followed they tend to use a promise instead of the wait for method, but I find using the Wait for method much convenient and intuitive.

Motivation: The motivation for such operation would include starting up your VM in hopes of running a startup script and obtain results within a matter of seconds as a Slack message. Through slack integration, such operation could be done through a simple slash command in your slack mobile application, without the need to access your terminal/computer.

Conclusion: We started from Part 1 where CBOT could simply say “hi” in response to a user text, and with this post now you can start and stop your VM from one simple command on Slack. What's next? Of course, there are many more functionalities you can add if you follow the google cloud API documentation here. In the following posts, I will be utilizing much more advanced functionalities to simplify practical aspects of your life using the Slack bot. I would like to thank Shalitha, Ivan and Shwetha for helping me with configurations and proof checking.

Chamal Gomes

Written by

Actuarial Research, DQL, DL, ML, Cloud Computing, RaspberryPI Dev, Education officer at MLSA University of Melbourne

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade