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" = "" },
]
}

Variables

Required Variables

instance_id (any) required

AWS Instance ID which is used for creating the AMI image (e.g. id-123456789012)

Optional Variables

ami_owner (string) optional

AWS Account ID which is used as a filter for AMI list (e.g. 123456789012)


Default value: ""

backup_schedule (string) optional

The scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes)


Default value: "cron(00 19 * * ? *)"

block_device_mappings (list(string)) optional

List of block device mappings to be included/excluded from created AMIs. With default value of [], AMIs will include all attached EBS volumes


Default value: [ ]

cleanup_schedule (string) optional

The scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes)


Default value: "cron(05 19 * * ? *)"

reboot (string) optional

Reboot the machine as part of the snapshot process


Default value: "false"

region (string) optional

AWS Region where module should operate (e.g. us-east-1)


Default value: ""

retention_days (string) optional

Is the number of days you want to keep the backups for (e.g. 14)


Default value: "14"

Context Variables

The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.

name (string) optional

Name (e.g. bastion or db)
Required: No

Default value: ""

namespace (string) optional

Namespace (e.g. cp or cloudposse)
Required: No

Default value: ""

stage (string) optional

Stage (e.g. prod, dev, staging)
Required: No

Default value: ""

Dependencies

Providers

  • archive
  • aws
  • null

Modules

NameVersionSourceDescription
labeltags/0.3.7git::https://github.com/cloudposse/terraform-null-label.gitn/a
label_backuptags/0.3.7git::https://github.com/cloudposse/terraform-null-label.gitn/a
label_cleanuptags/0.3.7git::https://github.com/cloudposse/terraform-null-label.gitn/a
label_roletags/0.3.7git::https://github.com/cloudposse/terraform-null-label.gitn/a

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module: