A node script (runnable on AWS Lambda) to manage incremental snapshots of AWS Lightsail instances.
Inspired by/based on vidanov/lambda-nodejs-lightsail-backup.
-
As root, sign into the AWS console.
-
Go to the IAM page.
-
Click Policies, and then Create Policy.
-
Go to the JSON tab. Paste in the following:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1510233220000", "Effect": "Allow", "Action": [ "lightsail:CreateInstanceSnapshot", "lightsail:DeleteInstanceSnapshot", "lightsail:GetInstanceSnapshot", "lightsail:GetInstanceSnapshots" ], "Resource": [ "*" ] } ] }
-
Click Review Policy.
-
Give it the name
LightsailSnapshots
, a description if desired, and click Create Policy.
- Click Roles in the IAM menu, then Create Role.
- Under the AWS Service tab, click Lambda in the services list.
- Click Next: Permissions.
- In the search field, type
AWSLambdaBasicExecutionRole
and check the box for it. - Also in the search field, type
LightsailSnapshots
and check the box for it too. - Click Next: Review.
- Give the role name
LightsailSnapshotsRole
click Create Role.
-
Go to the Lambda Dashboard.
-
Click Create Function.
-
from the preselected Author From Scratch tab:
- Set the name to
lightsail-backups
. - Set the runtime to Node.js 8.10.
- Select the existing role
LightsailSnapshotsRole
you created in the step 2.
- Set the name to
-
Click Create Function.
-
In the design panel, click Add triggers, then CloudWatch Event.
-
In the Rule section, click Create a new rule.
-
Name the rule
NightlyBackup
-
In Schedule expression, enter
cron(0 7 * * ? *)
and click Add. -
In the designer box, click on lightsail-backups again and paste everything from src/index.js.
-
Find the line in the code about region, and customize it for the region you're working in (TODO: allow this to work across more than one region).
-
You can also customize how many daily, weekly and monthly backups to keep. You should have at least 7 daily backups for this to work properly.
-
Under Environment variables, add a variable called
BACKUP_INSTANCES
, with the value being a comma-separated list of instances names you'd like to back up. E.g.:host1,host2,host3
-
Under Basic settings, set Timeout to 1 minute.
-
Click Save at the top right of the page.
- Click Configure test events in the dropdown left of the Test button at the top of the page. Use preselected values.
- Set the name to
test
and click Create. - Click the Test button.