Cloud Confusing

Explaining hosting, AWS, Wordpress, static sites, and all manner of cloud solutions.

Amazon’s Lightsail is a great option if you want affordable hosting and the power of a virtual private server (VPS), but the lack of cost means a lack of convenient features. One of the most glaring of these missing features if Lightsail’s complete lack of automated backups. This is a glaring oversight for Lightsail and it’s especially odd given that Lightsail has a built in — and very easy to use — snapshot tool.

How to: Cheap WordPress Hosting with Lightsail

Lightsail snapshots are one-click backups of your entire instance, which are better than simple backups, but there is no way to automate them through the Lightsail UI.

Using Amazon Web Service’s API and AWS Lambda you can automate Lightsail snapshots without much difficulty. In fact, thanks to a good web Samaritan the code is already done, so you just need to do the setup on your AWS account.

Using this Github repo (backed up for historic purposes), and the instructions in its readme, it’s a relatively easy process to automate your Lightsail backups via the AWS API and Lightsail’s snapshot tool. This will require either closely following instructions, or having some degree of understanding of Lambda jobs, AWS’ IAM policies, and Lightsail instances. Ultimately, it’s a relatively straightforward process that will take about 20 minutes to setup and then another 5 minutes per instance.

The cost of this is free-to-trivial — even the smallest Lightsail instances include 20GB of storage, and it’ll give you a customizable amount of nightly, weekly, and monthly snapshots. (And, to be clear, I’m using a slightly customized version of this setup for my own Lightsail boxes.)

You can automate Lightsail backups with the AWS CLI and cron jobs, but using Lambda with CloudWatch events is a straightforward, reliably, and AWS-y way to handle your backups.

Lightsail Backups In Plain English

The instructions in the repo are quite good so I won’t repeat them, but I will explain them in some easier terms. So what are you doing with your AWS account?

Lightsail

You already have an instance (right??) and you have some snapshot for it. These were made manually and will be deleted by this job. This may or may not matter to you, but it’s important to note this before you start. Also, snapshots are diffs so they aren’t huge, but the aren’t tiny either, so make sure your box has some spare space.

IAM

First of all, you you will create an IAM policy with scope of actions that is limited to Lightsail snapshot creations and deletions, then you will create a role to contain that policy.

Lambda

Lambda is AWS’s serverless compute tool. It’ll run some code — in this case Node.js — when it’s triggered to do so. The trigger we will use will be a CloudWatch event, particularly one that runs once every day. This is similar to a cron job, but it isn’t being handled by your instance, it’s an AWS service. Also, it’s worth noting that the event doesn’t have to be one a day, it could be once an hour or every time code is pushed to CodeCommit. Amazon has a robust set of events you can tap into.

When Lambda sees CloudWatch trigger it runs the Node code in the repo. This will set and then check a number of conditions and then create and delete snapshots as necessary. The Lambda job will need some small changes but these are simple: the name of your instance, a unique string to prepend to the backup file, and then the AWS zone. You can also customize how many daily, weekly, and monthly backups you store.

That’s really all there is to it. I’d stress a few things that might not seem important as you are reading:

  • The initial run of this code is the test. It’s not a dry run, it will actually run.
  • Because the test is a run, you will delete your old snapshots if they do not meet the daily/weekly/monthly conditions
  • You really do need to set the Lambda timeout to one minute. If you leave it at the default 3 seconds, the job will fail and you’ll get an error
  • Make sure you review the logs after each run. Lambda provides a really clean set of logs so it’s worth a run through to make sure the job works as intended.

And that should be it! Best of luck with your automated Lightsail backups. With this your Lightsail VPS is one step closer to being a robust hosting solution.

More reading: How to add an SSL cert to Lightsail

May 16th, 2019

Posted In: AWS

Tags: , ,