Scaleway is everything you wish you had from a Cloud Provider. Its simple to use, fast in creating resources and cheap. Thanks to its console, we can create, start, stop and restart our instances with a simple click!
But what if we needed to start&stop an instance at a predetermined time? Well buckle up and let’s find out how to do so thanks to Serverless functions!
TL;DR: Download this zip, create a serverless function, set all the env variables requested and setup a cron schedule to call the function.
Let’s create our instance
Creating an instance on Scaleway is super-easy, the process takes about a minute, here is the walkthrough:
Once the instance was created we need to save the Instance ID
, we’ll need it later to let the serverless function know with instance to stop/start.
Let’s write our serverless function
Let’s define the base of our function
In the handle
function, let’s check that the env variables are correctly set
Let’s also check that the request body is well formatted
Let’s then initialize the Scaleway SDK
And finally, let’s execute our server action and return something back from the function
The final code will look something like this
Let’s then zip our code the zip should contain the following files:
- handler.js
- package.json
- package-lock.json
- node_modules
Deploying the Serverless function
The next step is to deploy our function to Scaleway, let’s start by creating the function namespace:
And then let’s create our function:
Schedule the function to stop & start our instance
Lastly we need to actually schedule the function to start and stop our instance at certain times.
In the “Function Deployment” page there is an “Advanced option” section, where we can find “Triggers”
Let’s then add a trigger to stop and a trigger to start our instance when we want.
And that’s it! Leave the rest to Scaleway!