Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform*
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.72.1
hooks:
- id: terraform_fmt
- id: terraform_validate
- id: terraform_docs
- id: terraform_tflint
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v4.3.0
hooks:
- id: check-merge-conflict
- id: check-yaml
89 changes: 71 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

This terraform module is designed to help in using the AWS DLM Lifecycle. Each
volume that needs to be supported by the DLM Lifecycle must be tagged with
`Snapshot = "true"`.

## Requirements

* [AWS Terraform provider](https://www.terraform.io/docs/providers/aws/) >= 2.46
the tags defined by variable `target_tags` (default `Snapshot = "true"`)

## Usage

Expand All @@ -15,28 +11,85 @@ module "dlm-lifecycle" {
source = "julien-langlois/dlm-lifecycle-policies/aws"

dlm_policies = [
{ description = "DLM7", snapshot_name = "Rolling backup 7 days", start_time = "01:00", interval_hours = 4, retention_count = 7 },
{ description = "DLM14", snapshot_name = "Rolling backup 14 days", start_time = "04:00", interval_hours = 12, retention_count = 14 },
{ description = "DLM30", snapshot_name = "Rolling backup 30 days", start_time = "21:00", interval_hours = 2, retention_count = 30 }
{
description = "DLM7"
snapshot_name = "Rolling backup 7 days"
start_time = "01:00"
interval_hours = 4
retention_count = 7
},
{
description = "DLM14"
snapshot_name = "Rolling backup 14 days"
start_time = "04:00"
interval_hours = 12
retention_count = 14
},
{
description = "DLM30"
snapshot_name = "Rolling backup 30 days"
start_time = "21:00"
interval_hours = 2
retention_count = 30
},
{
description = "DLM40"
resource_types = "INSTANCE"
snapshot_name = "WeeklyBackupAMI"
cron_expression = "cron(0 3 * * SUN *)" # Every Sunday 3am
retention_count = 15
target_tags = {
Name = "instance-example"
Snapshot = "DLM40"
}
},
}
]
}
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.46.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.54.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_dlm_lifecycle_policy.policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dlm_lifecycle_policy) | resource |
| [aws_iam_role.dlm_lifecycle_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy.dlm_lifecycle](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |

## Inputs

| Name | Description | Type | Default | Required |
| ------------- | ------------------------------------------------------------- | :---------------: | :-------------: | :------: |
| unique\_name | Enter Unique Name to identify the Terraform Stack (lowercase) | string | `v1` | no |
| stack\_prefix | Stack Prefix for resource generation | string | `dlm_lifecycle` | no |
| dlm\_policies | Policies to be created | list(map(string)) | "" | yes |
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_dlm_policies"></a> [dlm\_policies](#input\_dlm\_policies) | DLM Policies to be created | <pre>list(object({<br> description = string<br> state = optional(string, "ENABLED")<br> resource_types = optional(string, "VOLUME")<br> snapshot_name = optional(string, "")<br> cron_expression = optional(string, null)<br> start_time = optional(string, "03:00")<br> interval_hours = optional(number, 24)<br> interval_unit = optional(string, "HOURS")<br> retention_count = optional(number, 7)<br> target_tags = optional(map(string), { Snapshot = "true" })<br> copy_tags = optional(bool, false)<br> }))</pre> | n/a | yes |
| <a name="input_stack_prefix"></a> [stack\_prefix](#input\_stack\_prefix) | Stack Prefix for resource generation | `string` | `"dlm_lifecycle"` | no |
| <a name="input_unique_name"></a> [unique\_name](#input\_unique\_name) | Enter Unique Name to identify the Terraform Stack (lowercase) | `string` | `"v1"` | no |

## Outputs

| Name | Description |
| ---- | ------------------------------------------------------ |
| arn | Amazon Resource Name (ARN) of the DLM Lifecycle Policy |
| id | Identifier of the DLM Lifecycle Policy |
| Name | Description |
|------|-------------|
| <a name="output_arns"></a> [arns](#output\_arns) | Amazon Resource Name (ARN) of the DLM Lifecycle Policy |
| <a name="output_ids"></a> [ids](#output\_ids) | Identifier of the DLM Lifecycle Policy |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Related documentation

AWS DLM Lifecycle Policy: [terraform.io/docs/providers/aws/r/dlm_lifecycle_policy](https://www.terraform.io/docs/providers/aws/r/dlm_lifecycle_policy)
AWS DLM Lifecycle Policy: [terraform.io/docs/providers/aws/r/dlm_lifecycle_policy](https://www.terraform.io/docs/providers/aws/r/dlm_lifecycle_policy)
24 changes: 12 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ resource "aws_iam_role_policy" "dlm_lifecycle" {
resource "aws_dlm_lifecycle_policy" "policies" {
count = length(var.dlm_policies)

description = lookup(var.dlm_policies[count.index], "description", "Snapshot Lifecycle Policy ${count.index}")
description = var.dlm_policies[count.index]["description"]
execution_role_arn = aws_iam_role.dlm_lifecycle_role.arn
state = lookup(var.dlm_policies[count.index], "state", "ENABLED")
state = var.dlm_policies[count.index]["state"]

policy_details {
resource_types = [lookup(var.dlm_policies[count.index], "resource_types", "VOLUME")]
resource_types = [var.dlm_policies[count.index]["resource_types"]]
schedule {
name = lookup(var.dlm_policies[count.index], "snapshot_name", "Schedule ${count.index}")
name = coalesce(var.dlm_policies[count.index]["snapshot_name"], var.dlm_policies[count.index]["description"])

create_rule {
interval = lookup(var.dlm_policies[count.index], "interval_hours", 24)
interval_unit = "HOURS"
times = [lookup(var.dlm_policies[count.index], "start_time", "03:00")]
cron_expression = var.dlm_policies[count.index]["cron_expression"]
interval = var.dlm_policies[count.index]["cron_expression"] != null ? null : var.dlm_policies[count.index]["interval_hours"]
interval_unit = var.dlm_policies[count.index]["cron_expression"] != null ? null : "HOURS"
times = var.dlm_policies[count.index]["cron_expression"] != null ? null : [var.dlm_policies[count.index]["start_time"]]
}

retain_rule {
count = lookup(var.dlm_policies[count.index], "retention_count", 7)
count = var.dlm_policies[count.index]["retention_count"]
}
tags_to_add = {
SnapshotCreator = "DLM"
}
copy_tags = lookup(var.dlm_policies[count.index], "copy_tags", false)
copy_tags = var.dlm_policies[count.index]["copy_tags"]
}

target_tags = {
Snapshot = "true"
}
target_tags = var.dlm_policies[count.index]["target_tags"]
}
}
8 changes: 4 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
output "arn" {
output "arns" {
description = "Amazon Resource Name (ARN) of the DLM Lifecycle Policy"
value = aws_dlm_lifecycle_policy.policies.*.arn
value = aws_dlm_lifecycle_policy.policies[*].arn
}

output "id" {
output "ids" {
description = "Identifier of the DLM Lifecycle Policy"
value = aws_dlm_lifecycle_policy.policies.*.id
value = aws_dlm_lifecycle_policy.policies[*].id
}
14 changes: 13 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@ variable "stack_prefix" {

variable "dlm_policies" {
description = "DLM Policies to be created"
type = list(map(string))
type = list(object({
description = string
state = optional(string, "ENABLED")
resource_types = optional(string, "VOLUME")
snapshot_name = optional(string, "")
cron_expression = optional(string, null)
start_time = optional(string, "03:00")
interval_hours = optional(number, 24)
interval_unit = optional(string, "HOURS")
retention_count = optional(number, 7)
target_tags = optional(map(string), { Snapshot = "true" })
copy_tags = optional(bool, false)
}))
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.12"
required_version = ">= 1.3.0"
required_providers {
aws = ">= 2.46.0"
}
Expand Down