Following are simple instructions on how to programmatically create a snapshot of your Amazon Lightsail Instance using AWS CLI.
First of all, we’ll need to install the AWS Command Line Interface on your Lightsail Linux instance. To do so, visit the Amazon CLI installation page and follow the instruction to set it up: http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-linux.html
Once AWS CLI is installed on your Lightsail Linux instance, we can go ahead and configure it. Run the following command:
You’ll get asked to supply your AWS Access Key ID, as well as AWS Secret Access Key. Then set your default region name to wherever your AWS Lightsail instances reside and set the output to JSON format, it should look something like this once configured:
When AWS is configured, you can start using AWS commands to control your Lightsail environment. For the full list of available commands visit: http://docs.aws.amazon.com/cli/latest/reference/lightsail/index.html
For example, let’s list all available/existing Lightsail instance snapshots (assuming you have already created some snapshot backups).
We can do that by issuing the following command:
aws lightsail get-instance-snapshots
The output of the above command should produce something like this:
The command to initiate the creation of the snapshot is as follows:
create-instance-snapshot --instance-snapshot-name <value> --instance-name <value> [--cli-input-json <value>] [--generate-cli-skeleton <value>]
So, let’s say I want to backup my Lightsail instance called “joe0.com-80month” and create a snapshot called: “joe0.com-27.July.2017“.
It’s as easy as to run the command from the command line in Linux:
aws lightsail create-instance-snapshot --instance-snapshot-name joe0.com-29.July.2017 --instance-name joe0.com-80month --region us-east-2
This is what the output will look like:
If you were to login to Lightsail website, under the snapshots tab of your instance, you’d see that the new backup was created called: “joe0.com-27.July.2017”
Now, let’s say that you want to automate this process and run it as a batch job every midnight. Nothing simpler than that.
Simply create a shell file on your Lightsail instance (most likely the best place is at /home/ec2-user/), you can call it: aws-snapshot.sh and paste in the above command. If you want to the date automatically added to the snapshot backup, you can configure it as follows (make sure to adjust the below code for your own snapshot name and region):
Now you can go ahead and schedule it as a cron job that runs every day at midnight. To do so
First, edit the current Cronjobs:
Then add your script entry there.
0 0 * * * /home/ec2-user/aws-snapshot.sh
Save cron job and check to see cron job is set:
And that’s it, your instance will be automatically backed up every day at midnight.
Tips… you can install JQ from https://stedolan.github.io/jq/ for work with JSON files using the command line, or install it on your Lightsail instance by using:
sudo yum install jq
Then you can run various cool commands. such as ask AWS how many snapshot backups do you currently have in your console:
Let’s ask AWS how many snapshot backups do you currently have in total:
aws lightsail get-instance-snapshots | jq '.|length'
The output will look like this:
Or you can query the names of all your currently created snapshots:
aws lightsail get-instance-snapshots | jq '..name'
The output will look like this:
Or you can sort them by name:
aws lightsail get-instance-snapshots | jq '. | sort_by(.createdAt) | ..name'
This script is now available on Github if you’d like to contribute your changes: