Skip to content

Go application to take EC2 volume snapshots on a schedule using CloudWatch Scheduled Events, also does a clean up of old snapshots. Deploy using SAM.

License

Notifications You must be signed in to change notification settings

harryleesan/snapper

Repository files navigation

Introduction

Snapper is a simple snapshot helper that takes snapshots of EBS volumes of tagged instances on a cron schedule. This application also removes snapshots that are over a specific age.

How it works

This Go application is deployed as a AWS Lambda function which when called, checks all AWS EC2 instances for the Snapper tag. It then creates (or deletes AWS EBS snapshots) depending on the input. This Lambda function is called via AWS CloudWatch scheduled events.

For ease of use, it is deployed as a AWS CloudFormation stack. See usage below.

Usage

To enable this functionality, tag the EC2 instances with the Key Snapper and the Value with the number of days that you want the snapshots to exist.

{
  "Key": "Snapper",
  "Value": "7"
}

Pre-requisites

  • Go (Of course)
    • dep (Dependency management)
  • AWS CLI (Just need your AWS creds)
  • SAM CLI (For packaging this lambda application).

This application can be developed in Docker (See Dockerfile) without having the above pre-requisites installed (All the tools are packaged in the docker image).

docker build -t halosan/snapper-dev:latest .
docker run --rm -it -v $(pwd):/go/src/app -v $HOME/.aws:/home/1000/.aws halosan/snapper-dev:latest bash

(It is recommended to develop in the provided docker container, this ensures that you don't have version issues.)

NOTE! Ensure that your AWS lambda function has the permission to access your EC2 instances and create snapshots. This can be done by attaching your Lambda function to a role. An example policy for this role can be found in snapper-policy.

  1. Install dependencies:

    dep ensure
  2. Build the binary for the Lambda function:

    go build main.go
  3. To package this Go application using SAM and upload into S3 (Note that this will also update the snapper-serverless.yaml file):

    sam package --template-file ./template.yaml --output-template-file snapper-serverless.yaml \
    --s3-bucket vatit-lambdas --s3-prefix snapper-go
  4. To deploy the Lambda function through your own CloudFormation stack:

    sam deploy --template-file snapper-serverless.yaml --stack-name snapper

About

Go application to take EC2 volume snapshots on a schedule using CloudWatch Scheduled Events, also does a clean up of old snapshots. Deploy using SAM.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages