Simple script to toggle up/down a VM instance in Google Cloud Compute Engine.
I've recently set up a VSCode server in a Compute Engine instance using the amazing code-server project and it's great! It's really cool to have access to a performant, custom development environment wherever I am :D
Buuuut, I'm obviously not using it at all times, so keeping it running 24/7 would be a waste of resources and ultimately money: I needed some way to turn it off when I'm not using it. So I made this.
There are probably better/cooler solutions but I just wanted something quick and simple to start/stop the VM. Now, I can turn on/off the server with just a command! I don't know about you but I call that M A G I C 🎩🐇✨
- Node.js installed. I've tested it v12 LTS so I guess you should use that at least (?)
- An instance you want to turn on/off in Google Cloud Compute Engine as the name of the project suggests 😅
- Clone the project somewhere
- Create a Google Cloud Service Account Key. It must have write access to Compute Engine resources.
- Create a
.env
file based on the provided.env.example
. - (Optional) Create an alias in your
.zshrc
,.bashrc
or similar file:
alias codeup=`node /path-to-the-project/index.js up`
alias codedown=`node /path-to-the-project/index.js down`
alias codetgl=`node /path-to-the-project/index.js`
node index.js [up|down]
up
: starts the machine and pings until it's reachabledown
: stops the machine- When called with no arguments, it will check the current status and toggle it (i.e.
RUNNING
-> stop,TERMINATED
-> start)