Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Module to enable Data Lifecycle Manager to take snapshots of a EBS #197

Merged
merged 3 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions modules/dlm-lifecycle-iam-role/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Data Lifecycle Manager (DLM) lifecycle policy for managing snapshots

This module creates the IAM role and the policy that allows the AWS Data Lifecycle Manager to create snapshots.

### Example how to use

Define variables

```
module "ebs-backup-policy" {
source = "github.com/fpco/terraform-aws-foundation//modules/dlm-lifecycle-iam"

iam_role_name = "dlm-lifecycle-role"
}
```
56 changes: 56 additions & 0 deletions modules/dlm-lifecycle-iam-role/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
variable "iam_role_name" {
description = "The IAM role name for the DLM lifecyle policy"
type = string
default = "dlm-lifecycle-role"
}

# Create the iam role
resource "aws_iam_role" "dlm_lifecycle_role" {
name = var.iam_role_name
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "dlm.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this as-is, but we should update this code to use the data source so we can use HCL and don't have to do the messy JSON thing.


# DLM lifecycle Policy
resource "aws_iam_role_policy" "dlm_lifecycle_policy" {
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
}
3 changes: 3 additions & 0 deletions modules/dlm-lifecycle-iam-role/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}
25 changes: 25 additions & 0 deletions modules/dlm-lifecycle-policy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Data Lifecycle Manager (DLM) lifecycle policy for managing snapshots

This module creates the policy that manage the creation of EBS snapshots through AWS Data Lifecycle Manager, the policy let you manage the schedule of the snapshot as well as the number of snapshots.

### Example how to use

Define variables

```
module "ebs-backup-policy" {
source = "github.com/fpco/terraform-aws-foundation//modules/dlm-lifecycle-policy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should source the module from the registry (we need a ticket for making sure all of our examples and docs do that - #268).


name_prefix = project-name-backup
description = "DLM lifecycle policy"
ebs_target_tags = "ebs-to-take-snapshot-name-ec2-volume"
policy_name = "One week of daily snapshots"
policy_interval = 24
policy_time = ["23:45"]
policy_copy_tags = false
policy_retain_rule = 14
policy_tags_to_add = "${merge(map("Name", "${var.name}-dlm", "SnapshotCreator", "DLM lifecycle"))}"
resource_type = ["VOLUME"]
role_name = "dlm-lifecycle-role"
}
```
48 changes: 48 additions & 0 deletions modules/dlm-lifecycle-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* ## 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
*
*/

# Get the data of role to pass the resource
data "aws_iam_role" "dlm_lifecycle_role" {
name = var.role_name
}

# DLM lifecycle schedule
resource "aws_dlm_lifecycle_policy" "ebs-lifecycle-policy" {
description = var.description
execution_role_arn = data.aws_iam_role.dlm_lifecycle_role.arn
state = "ENABLED"

policy_details {
resource_types = var.resource_type

schedule {
name = "${var.name_prefix} ${var.policy_name}"

create_rule {
interval = var.policy_interval
interval_unit = var.policy_interval_unit
times = var.policy_times
}

retain_rule {
count = var.policy_retain_rule
}

tags_to_add = var.policy_tags_to_add
copy_tags = var.policy_copy_tags
}

target_tags = var.ebs_target_tags

}
}
69 changes: 69 additions & 0 deletions modules/dlm-lifecycle-policy/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
variable "name_prefix" {
description = "Name to prefix the DML lifecycle"
type = "string"
}

variable "ebs_target_tags" {
description = "Tags to filter the volume that we want to take the snapshot."
type = map
default = {}
}

variable "description" {
description = "DLM lifecycle policy description"
type = string
default = "DLM lifecycle policy"
}

variable "resource_type" {
description = "DLM resource type"
type = list(string)
default = ["VOLUME"]
}

variable "policy_name" {
description = "Snapshots schedule name"
type = string
default = "One week of daily snapshots"
}

variable "policy_interval" {
description = "Snapshots schedule interval"
type = number
default = 24
}

variable "policy_interval_unit" {
description = "Snapshots schedule interval unit"
type = string
default = "HOURS"
}

variable "policy_times" {
description = "Time at which the snapshot will take."
type = list
default = ["23:45"]
}

variable "policy_retain_rule" {
description = "Snapshots schedule retein rule, how many snapshots are retaining"
type = number
default = 14
}

variable "policy_copy_tags" {
description = "Copy all user-defined tags on a source volume to snapshots of the volume created by this policy."
type = bool
default = true
}

variable "policy_tags_to_add" {
description = "Tags to add to the snapshot"
type = "map"
default = {"SnapshotCreator" = "DLM lifecycle"}
}

variable "role_name" {
description = "The IAM role name for the DLM lifecyle policy"
type = string
}
3 changes: 3 additions & 0 deletions modules/dlm-lifecycle-policy/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}