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-iconecs-deploy-runner-invoke-iam-policy
file-type-iconecs-deploy-runner-standard-configuration
file-type-iconecs-deploy-runner
file-type-icongit-helpers
file-type-icongruntwork-module-circleci-helpers
file-type-iconiam-policies
file-type-iconinfrastructure-deploy-script
file-type-iconinfrastructure-deployer
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-Terraform-Terragrunt-Pipeline.adoc
file-type-iconREADME-TravisCI.adoc
file-type-iconREADME.adoc
file-type-iconsetup.cfg

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-iconecs-deploy-runner-invoke-iam-policy
file-type-iconecs-deploy-runner-standard-configuration
file-type-iconecs-deploy-runner
file-type-icongit-helpers
file-type-icongruntwork-module-circleci-helpers
file-type-iconiam-policies
file-type-iconinfrastructure-deploy-script
file-type-iconinfrastructure-deployer
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-Terraform-Terragrunt-Pipeline.adoc
file-type-iconREADME-TravisCI.adoc
file-type-iconREADME.adoc
file-type-iconsetup.cfg
EC2 backup

EC2 backup

Snapshot your EC2 instances on a scheduled basis.

Code Preview

Preview the Code

mobile file icon

README.md

down

EC2 Backup Lambda Function Module

NOTE: This module is deprecated and will be removed in the future. Use the Data Lifecycle Manager based backup system instead.

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.

Difference with Data Lifecycle Manager

As an alternative to lambda functions using ec2-snapper, we also have the ec2-backup module in the repo module-server which uses AWS Data Lifecycle Managers (DLM) to manage the EBS snapshots. Unlike with lambda functions, this is an AWS native solution that does not have any infrastructure to manage.

Additionally, Data Lifecycle Managers work through the use of tags on volumes, unlike the lambda function (which selects volumes by EC2 instance). This means that the backup function is able to group all the snapshots together across deployments. For example, if you wanted to support blue green deployments for your jenkins server and you rotated instances, the snapshots for the previous instance would still be managed using the same DLM policy.

However, there are a few features that the lambda based backup functions support which are currently not available with DLM:

  • Support backup schedules with frequencies longer than 1 day (e.g., weekly). DLM does not support any frequency longer than 1 day.
    • NOTE: There is an open PR in the AWS provider to add support for this.
  • Minimum backup counts. The lambda based backup mechanism supports specifying to keep a minimum number of backups around.

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?