This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module to enable DML lifecycle policies
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Data Lifecycle Manager (DLM) lifecycle policy for managing snapshots | ||
|
||
This module creates an IAM role and a policy that manage the creation of EBS snapshots, Data Lifecycle Manager policy let you create snapshots according to the schedule that you choose. | ||
|
||
### Example how to use | ||
|
||
Define the module in your terraform project: | ||
``` | ||
variable "ebs_name" { | ||
description = "EBS name/tag to query" | ||
default = "myebstagname" | ||
} | ||
Define variables | ||
... | ||
module "ebs-backup-policy" { | ||
source = "git::https://github.com/fpco/terraform-aws-foundation//modules/dlm-lifecycle-policy" | ||
ebs_name = "${var.ebs_name_for_backup}" | ||
dml_description = "${var.dml_description}" | ||
schedule_name = "${var.schedule_name}" | ||
schedule_interval = "${var.schedule_interval}" | ||
schedule_interval_times = "${var.schedule_interval_times}" | ||
schedule_interval_retain_rule = "${var.schedule_interval_retain_rule}" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Create the iam role | ||
resource "aws_iam_role" "dlm_lifecycle_role" { | ||
name = "dlm-lifecycle-role" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "dlm.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
# DLM lifecycle Policy | ||
resource "aws_iam_role_policy" "dlm_lifecycle" { | ||
name = "dlm-lifecycle-policy" | ||
role = "${aws_iam_role.dlm_lifecycle_role.id}" | ||
|
||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"ec2:CreateSnapshot", | ||
"ec2:DeleteSnapshot", | ||
"ec2:DescribeVolumes", | ||
"ec2:DescribeSnapshots" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"ec2:CreateTags" | ||
], | ||
"Resource": "arn:aws:ec2:*::snapshot/*" | ||
} | ||
] | ||
} | ||
EOF | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* ## Data Lifecycle Manager (DLM) lifecycle policy for managing snapshots | ||
* | ||
* The purpose of this module is to provide a policy to create snapshots accourding an schudule. | ||
* This module creates an IAM role and a policy that manage the creation of EBS snapshots, Data Lifecycle Manager policy let you create snapshots according to the schedule that you choose. | ||
* | ||
* The module supports: | ||
* | ||
* * Generate snapshots of volumes attached to ec2 instances | ||
* * Assume the IAM Role policy to manage DLM lifecycle policy | ||
* | ||
*/ | ||
|
||
# Filter for the EBS volume that will apply the policy | ||
data "aws_ebs_volume" "ebs" { | ||
most_recent = true | ||
|
||
filter { | ||
name = "volume-type" | ||
values = "${var.ebs_type}" | ||
} | ||
|
||
filter { | ||
name = "tag:Name" | ||
values = ["${var.ebs_name}"] | ||
} | ||
} | ||
|
||
# DLM lifecycle schedule | ||
resource "aws_dlm_lifecycle_policy" "gitlab-ebs-lifecycle-policy" { | ||
description = "${var.dml_description}" | ||
execution_role_arn = "${aws_iam_role.dlm_lifecycle_role.arn}" | ||
state = "ENABLED" | ||
|
||
policy_details { | ||
resource_types = "${var.dml_resource_type}" | ||
|
||
schedule { | ||
name = "${var.schedule_name}" | ||
|
||
create_rule { | ||
interval = "${var.schedule_interval}" | ||
interval_unit = "${var.schedule_interval_unit}" | ||
times = "${var.schedule_times}" | ||
} | ||
|
||
retain_rule { | ||
count = "${var.schedule_retain_rule}" | ||
} | ||
|
||
tags_to_add = { | ||
SnapshotCreator = "DLM" | ||
} | ||
|
||
copy_tags = "${var.schedule_copy_tags}" | ||
} | ||
|
||
target_tags = { | ||
Snapshot = "${var.schedule_target_tags}" | ||
} | ||
} | ||
} | ||
|
||
#resource "aws_ebs_snapshot" "ebs-snapshot" { | ||
# volume_id = "${data.aws_ebs_volume.ebs.id}" | ||
# | ||
# tags = { | ||
# Name = "${var.ebs_name}-ebs" | ||
# } | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
variable "ebs_name" { | ||
description = "EBS name/tag to query" | ||
default = "" | ||
} | ||
|
||
variable "ebs_type" { | ||
description = "EBS type to query" | ||
type = "list" | ||
default = ["gp2"] | ||
} | ||
|
||
variable "dml_description" { | ||
description = "DLM lifecycle policy description" | ||
default = "DLM lifecycle policy" | ||
} | ||
|
||
variable "dml_resource_type" { | ||
description = "DLM resource type" | ||
default = ["VOLUME"] | ||
} | ||
|
||
variable "schedule_name" { | ||
description = "Snapshots schedule name" | ||
default = "One week of daily snapshots" | ||
} | ||
|
||
|
||
variable "schedule_interval" { | ||
description = "Snapshots schedule interval" | ||
default = 24 | ||
} | ||
|
||
variable "schedule_interval_unit" { | ||
description = "Snapshots schedule interval unit" | ||
default = "HOURS" | ||
} | ||
|
||
variable "schedule_times" { | ||
description = "Snapshots schedule time" | ||
type = "list" | ||
default = ["23:45"] | ||
} | ||
|
||
variable "schedule_retain_rule" { | ||
description = "Snapshots schedule interval times" | ||
default = 14 | ||
} | ||
|
||
variable "schedule_copy_tags" { | ||
description = "Copy all user-defined tags on a source volume to snapshots of the volume created by this policy." | ||
default = false | ||
} | ||
|
||
variable "schedule_target_tags" { | ||
description = "A mapping of tag keys and their values. Any resources that match the resource_types and are tagged with any of these tags will be targeted." | ||
default = "true" | ||
} |