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
Module to enable Data Lifecycle Manager to take snapshots of a EBS #197
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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" | ||
} | ||
``` |
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,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 | ||
} | ||
|
||
# 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 | ||
} |
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,3 @@ | ||
terraform { | ||
required_version = ">= 0.12" | ||
} |
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,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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
``` |
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,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 | ||
|
||
} | ||
} |
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,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 | ||
} |
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,3 @@ | ||
terraform { | ||
required_version = ">= 0.12" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.