Automating Instances: Start and stop after schedule

Are you looking to save on your cloud computing costs? One simple way to do so is by automating the stopping and starting of your instances. By stopping your instances for example at night, when they are not being used, and starting them back up in the morning, you can significantly reduce your monthly bill. With some simple steps you can easily set up a cronjob to automate your instances.
Scheduling API Calls: Automating with Cronjobs
To allow automated starting and stopping of instances, you need to create a (micro) Linux instance and set up a cronjob that leverages the Exoscale API.
Method 1: Using SSH to set up a Cronjob
Start by creating a Linux instance. Use the Exoscale CLI or go to Compute -> Instances -> Add
within the Portal. Select Ubuntu
from the Template list, choose any Zone, and choose an instance type (Micro
should be sufficient). Additionally, select the Security Group where you allow SSH. Then go ahead and create your instance.
sudo -s # Log in as root if not yet
VERSION=1.64.0
wget https://github.com/exoscale/cli/releases/download/v$VERSION/exoscale-cli_"$VERSION"_linux_amd64.deb
dpkg -i exoscale-cli_"$VERSION"_linux_amd64.deb
After installation has finished, run exo config
to submit your API key. You can already try stopping or starting an instance by issuing exo compute instance stop NAME -z de-fra-1
replacing the name and zone respectivly.
crontab -e
This will open the crontab file in a text editor. At the bottom of the file you can add a new line with the schedule and command for your cronjob. For example, to stop your instance at 10 PM and start it again at 6 AM, you could use the following schedule and command:
0 22 * * * exo compute instance stop NAME -z de-fra-1 --force --quiet
0 6 * * * exo compute instance start NAME -z de-fra-1 --force --quiet
The first line tells cron to run the exo compute instance stop NAME -z de-fra-1 --force --quiet
command at 10 PM every day, while the second tells cron to run the exo compute instance start NAME -z de-fra-1 --force --quiet
command at 6 AM every day. The exo command is the Exoscale CLI, which allows you to interact with the Exoscale API from the command line.
Method 2: Using cloud-init to fully automate the set up of the Cronjob
Instead of using SSH to configure our automation, you can use cloud-init. Cloud-init is a tool that is used to customize the initialization of a cloud instance, perform tasks such as installing packages, writing files, and running scripts. To leverage this tool, you need to write a Cloud-init or bash script which you can insert into the User Data field during instance creation.
#cloud-config
# Install dependencies
packages:
- curl
# Download the Exoscale CLI package
runcmd:
- EXOSCALE_CLI_VERSION=1.64.0
- curl -L -o /tmp/exoscale-cli.deb https://github.com/exoscale/cli/releases/download/v${EXOSCALE_CLI_VERSION}/exoscale-cli_${EXOSCALE_CLI_VERSION}_linux_amd64.deb
# Install the Exoscale CLI
- dpkg -i /tmp/exoscale-cli.deb
# Set up the cron job to stop and start the instance
write_files:
- path: /etc/cron.d/exoscale-instance-scheduling
permissions: '0644'
owner: root
content: |
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# Stop the instance at 10 PM and start it again at 6 AM
0 10 * * * root exo compute instance stop NAME -z DE-FRA-1 --force --quiet
0 6 * * * root exo compute instance start NAME -z DE-FRA-1 --force --quiet
- path: /root/.config/exoscale/exoscale.toml
permissions: '0644'
owner: root
content: |
defaultaccount = "instancecron"
[[accounts]]
account = "instancecron"
defaultZone = "de-fra-1"
endpoint = "https://api.exoscale.com/v1"
environment = ""
key = "YOURKEY"
name = "instancecron"
secret = "YOURSECRET"
To use this cloud-init script, you need to replace the name of your instance and the desired stop and start times in exo
-commands, as well as your Exoscale API key and secret in the api_key
and api_secret
variables. After inserting those into the User Data field during instance creation, it should automatically set up your cronjob. To modify it, you can either log into SSH and edit the file /etc/cron.d/exoscale-instance-scheduling
or simply delete the instance and recreate it with an updated plan.
Start Automating
Whether you are a small business owner, a startup, or an enterprise organization, automating your instance management in the cloud can help to reduce your cloud costs and maximize your return on investment. By using the Exoscale CLI and the steps outlined in this blog post, you can start now and immedidately benefit of automated instance management in your Exoscale cloud environment.