Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconaws-helpers
file-type-iconbuild-helpers
file-type-iconcheck-url
file-type-iconcircleci-helpers
file-type-icondocs-generator
file-type-iconec2-backup
file-type-iconbackup-lambda-function
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-icongit-helpers
file-type-icongruntwork-module-circleci-helpers
file-type-iconiam-policies
file-type-iconinstall-jenkins
file-type-iconjenkins-server
file-type-iconkubernetes-circleci-helpers
file-type-iconterraform-helpers
file-type-icontest
file-type-icontestdep
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME-CircleCI.adoc
file-type-iconREADME-Jenkins.adoc
file-type-iconREADME-TravisCI.adoc
file-type-iconREADME.adoc

Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconaws-helpers
file-type-iconbuild-helpers
file-type-iconcheck-url
file-type-iconcircleci-helpers
file-type-icondocs-generator
file-type-iconec2-backup
file-type-iconbackup-lambda-function
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-icongit-helpers
file-type-icongruntwork-module-circleci-helpers
file-type-iconiam-policies
file-type-iconinstall-jenkins
file-type-iconjenkins-server
file-type-iconkubernetes-circleci-helpers
file-type-iconterraform-helpers
file-type-icontest
file-type-icontestdep
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME-CircleCI.adoc
file-type-iconREADME-Jenkins.adoc
file-type-iconREADME-TravisCI.adoc
file-type-iconREADME.adoc
EC2 backup

EC2 backup

Snapshot your EC2 instances on a scheduled basis.

Preview the Code

mobile file icon

README.md

down

EC2 Backup Module

This module can be used to make scheduled backups of an EC2 Instance and its EBS Volumes. Under the hood, this module uses package-lambda to deploy a Lambda function that is triggered on a scheduled basis by Amazon CloudWatch Events and runs ec2-snapper to take a snapshot of the EC2 Instance.

Example code

  • Check out the jenkins example for working sample code.
  • See vars.tf for all parameters you can configure on this module.

Specifying an instance

To specify the instance to backup, you simply provide the instance's name via the instance_name parameter. This should correspond to a tag on your EC2 Instance with the name Name.

Configuring the schedule

You can specify how often this lambda function runs using the backup_job_schedule_expression parameter. This can be either a rate expression such as rate(1 day) or a cron expression such as cron(0 20 * * ? *). See Schedule Expressions for more information and examples.

Triggering alarms if backup fails

Every time the function runs successfully, it will increment a CloudWatch Metric. We've configured a CloudWatch alarm to go off if the metric is not updated on the expected schedule, as that implies the backup has failed to run!

You can specify the metric namespace and name using the cloudwatch_metric_namespace and cloudwatch_metric_name parameters, respectively. You can specify the SNS topic to notify when the alarm goes off using the alarm_sns_topic_arns parameter.

Cleaning up old snapshots

To prevent the number of snapshots from growing infinitely and costing you a lot of money, ec2-snapper will automatically delete older snapshots. You can specify two parameters to control how many snapshots are kept around:

  • delete_older_than: Delete all snapshots older than this duration. For example, if you set this parameter to 30d, then snapshots that are more than 30 days old will be deleted. See Delete AMIs older than for more info.

  • require_at_least: Always keep around at least this many snapshots. This helps avoid deleting too much if you have, for example, a misconfiguration of the delete_older_than parameter.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?