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
) requiredAWS Instance ID which is used for creating the AMI image (e.g.
id-123456789012
)
Optional Variables
ami_owner
(string
) optionalAWS Account ID which is used as a filter for AMI list (e.g.
123456789012
)Default value:
""
backup_schedule
(string
) optionalThe scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes)
Default value:
"cron(00 19 * * ? *)"
block_device_mappings
(list(string)
) optionalList 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
) optionalThe scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes)
Default value:
"cron(05 19 * * ? *)"
reboot
(string
) optionalReboot the machine as part of the snapshot process
Default value:
"false"
region
(string
) optionalAWS Region where module should operate (e.g.
us-east-1
)Default value:
""
retention_days
(string
) optionalIs 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.
context.tf
file of this module and part of the terraform-null-label pattern.Dependencies
Providers
archive
aws
null
Modules
Name | Version | Source | Description |
---|---|---|---|
label | tags/0.3.7 | git::https://github.com/cloudposse/terraform-null-label.git | n/a |
label_backup | tags/0.3.7 | git::https://github.com/cloudposse/terraform-null-label.git | n/a |
label_cleanup | tags/0.3.7 | git::https://github.com/cloudposse/terraform-null-label.git | n/a |
label_role | tags/0.3.7 | git::https://github.com/cloudposse/terraform-null-label.git | n/a |
Resources
The following resources are used by this module:
aws_cloudwatch_event_rule.ami_backup
(resource)aws_cloudwatch_event_rule.ami_cleanup
(resource)aws_cloudwatch_event_target.ami_backup
(resource)aws_cloudwatch_event_target.ami_cleanup
(resource)aws_iam_role.ami_backup
(resource)aws_iam_role_policy.ami_backup
(resource)aws_lambda_function.ami_backup
(resource)aws_lambda_function.ami_cleanup
(resource)aws_lambda_permission.ami_backup
(resource)aws_lambda_permission.ami_cleanup
(resource)null_resource.schedule
(resource)
Data Sources
The following data sources are used by this module:
archive_file.ami_backup
(data source)archive_file.ami_cleanup
(data source)aws_iam_policy_document.ami_backup
(data source)aws_iam_policy_document.default
(data source)