Skip to main content

Module: ec2-ami-backup

This repo contains a terraform module that creates two lambda functions that will create AMI automatically at regular intervals. It is based on the code at https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/ and https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups-2/.

Usage

Include this repository as a module in your existing terraform code:

module "lambda_ami_backup" {
source = "git::https://github.com/cloudposse/terraform-aws-ec2-ami-backup.git?ref=tags/0.3.2"

name = "${var.name}"
stage = "${var.stage}"
namespace = "${var.namespace}"
region = "${var.region}"
ami_owner = "${var.ami_owner}"
instance_id = "${var.instance_id}"
retention_days = "14"
}

Examples

Example on excluding some of attached EBS volumes:

module "lambda_ami_backup" {
source = "git::https://github.com/cloudposse/terraform-aws-ec2-ami-backup.git?ref=tags/0.3.2"

name = "${var.name}"
stage = "${var.stage}"
namespace = "${var.namespace}"
region = "${var.region}"
ami_owner = "${var.ami_owner}"
instance_id = "${var.instance_id}"
retention_days = "14"

block_device_mappings = [
{ "DeviceName" = "/dev/xvdf", "NoDevice" = "" },
{ "DeviceName" = "/dev/xvdg", "NoDevice" = "" },
]
}

Requirements

No requirements.

Providers

NameVersion
archiven/a
awsn/a
nulln/a

Modules

NameSourceVersion
labelgit::https://github.com/cloudposse/terraform-null-label.gittags/0.3.7
label_backupgit::https://github.com/cloudposse/terraform-null-label.gittags/0.3.7
label_cleanupgit::https://github.com/cloudposse/terraform-null-label.gittags/0.3.7
label_rolegit::https://github.com/cloudposse/terraform-null-label.gittags/0.3.7

Resources

NameType
aws_cloudwatch_event_rule.ami_backupresource
aws_cloudwatch_event_rule.ami_cleanupresource
aws_cloudwatch_event_target.ami_backupresource
aws_cloudwatch_event_target.ami_cleanupresource
aws_iam_role.ami_backupresource
aws_iam_role_policy.ami_backupresource
aws_lambda_function.ami_backupresource
aws_lambda_function.ami_cleanupresource
aws_lambda_permission.ami_backupresource
aws_lambda_permission.ami_cleanupresource
null_resource.scheduleresource
archive_file.ami_backupdata source
archive_file.ami_cleanupdata source
aws_iam_policy_document.ami_backupdata source
aws_iam_policy_document.defaultdata source

Inputs

NameDescriptionTypeDefaultRequired
ami_ownerAWS Account ID which is used as a filter for AMI list (e.g. 123456789012)string""no
backup_scheduleThe scheduling expression. (e.g. cron(0 20 * ? ) or rate(5 minutes)string"cron(00 19 * * ? *)"no
block_device_mappingsList of block device mappings to be included/excluded from created AMIs. With default value of [], AMIs will include all attached EBS volumeslist(string)[]no
cleanup_scheduleThe scheduling expression. (e.g. cron(0 20 * ? ) or rate(5 minutes)string"cron(05 19 * * ? *)"no
instance_idAWS Instance ID which is used for creating the AMI image (e.g. id-123456789012)anyn/ayes
nameName (e.g. bastion or db)string""no
namespaceNamespace (e.g. cp or cloudposse)string""no
rebootReboot the machine as part of the snapshot processstring"false"no
regionAWS Region where module should operate (e.g. us-east-1)string""no
retention_daysIs the number of days you want to keep the backups for (e.g. 14)string"14"no
stageStage (e.g. prod, dev, staging)string""no

Outputs

No outputs.