Code Monkey home page Code Monkey logo

lambda-ec2backup's Introduction

What is this?

This is a simple but powerfull script to backup your EC2 instances using lambda

How to work?

Cloudwatch Event -> Lambda -> EC2 create ami -> EC2 delete ami -> EC2 delete old snap

Variables to use

Lambda Environ

  • BACKUP_RETENTION = Retention period for delete, value in days (Default: 7, type: int)
  • BACKUP_TAG = Tag to select the instance to backup (default: BackupIT, type: string)
  • BACKUP_ONLYRUNNING = Backup the instance if it has the tag and is running (default: false, type: bool)
  • BACKUP_DEBUG = Verbose script (default: false, type: bool)

EC2 Tags

  • BackupIT/CUSTOM SETTINGS = Check BACKUP_TAG
  • BACKUP_COPYTAG = Copy the tags from EC2 and apply it on AMI snap (default: true, type: bool)
  • BACKUP_REBOOT = Specify ig you want to restart on AMI snap (default: true, type: bool)

How to use it?

Create a IAM roles as below

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeImages",
                "ec2:DeregisterImage",
                "ec2:DescribeInstances",
                "ec2:DeleteSnapshot",
                "ec2:DescribeTags",
                "ec2:CreateTags",
                "ec2:CreateImage",
                "ec2:DescribeSnapshots"
            ],
            "Resource": "*"
        }
    ]
}

Create a new lambda function

Lambda -> Functions -> Create function -> Author from scratch:

  • Name: as you want
  • Runtime: Python 3.7
  • Role: Select "Create a new role from one or more templates"
  • Role name: same as "Name" -> Create function

Wait until the function is created.

  • Copy paste the code into "Function code"
  • Change the timeout to 5 min
  • Add Cloudwatch Events as below:
    • Rule: "Create a new rule"
    • Rule Name: as you want
    • Rule type: "Schedule expression"
    • Rate: rate(1 day)
    • Enable trigger

Add the variables to the EC2 Instances

lambda-ec2backup's People

Contributors

mirio avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.