Skip to content

A node script (runnable on AWS Lambda) to manage incremental snapshots of AWS Lightsail instances.

License

Notifications You must be signed in to change notification settings

weareadjacent/lambda-lightsail-backup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lambda Lightsail Backup

A node script (runnable on AWS Lambda) to manage incremental snapshots of AWS Lightsail instances.

Inspired by/based on vidanov/lambda-nodejs-lightsail-backup.

Setup

Step 1. Create the IAM policy

  1. As root, sign into the AWS console.

  2. Go to the IAM page.

  3. Click Policies, and then Create Policy.

  4. 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": [
                    "*"
                ]
            }
        ]
    }
    
  5. Click Review Policy.

  6. Give it the name LightsailSnapshots, a description if desired, and click Create Policy.

Step 2. Create the IAM role

  1. Click Roles in the IAM menu, then Create Role.
  2. Under the AWS Service tab, click Lambda in the services list.
  3. Click Next: Permissions.
  4. In the search field, type AWSLambdaBasicExecutionRole and check the box for it.
  5. Also in the search field, type LightsailSnapshots and check the box for it too.
  6. Click Next: Review.
  7. Give the role name LightsailSnapshotsRole click Create Role.

Step 3. Create Lambda function

  1. Go to the Lambda Dashboard.

  2. Click Create Function.

  3. 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.
  4. Click Create Function.

  5. In the design panel, click Add triggers, then CloudWatch Event.

  6. In the Rule section, click Create a new rule.

  7. Name the rule NightlyBackup

  8. In Schedule expression, enter cron(0 7 * * ? *) and click Add.

  9. In the designer box, click on lightsail-backups again and paste everything from src/index.js.

  10. 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).

  11. 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.

  12. 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
    
  13. Under Basic settings, set Timeout to 1 minute.

  14. Click Save at the top right of the page.

Step 4. Test the function

  1. Click Configure test events in the dropdown left of the Test button at the top of the page. Use preselected values.
  2. Set the name to test and click Create.
  3. Click the Test button.

About

A node script (runnable on AWS Lambda) to manage incremental snapshots of AWS Lightsail instances.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published