diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e79eb23 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.terraform* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e41046e --- /dev/null +++ b/.pre-commit-config.yaml @@ -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 diff --git a/README.md b/README.md index f031af6..ffdbe0d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" + } + }, + } ] } ``` + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.3.0 | +| [aws](#requirement\_aws) | >= 2.46.0 | + +## Providers + +| Name | Version | +|------|---------| +| [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 | +|------|-------------|------|---------|:--------:| +| [dlm\_policies](#input\_dlm\_policies) | DLM Policies to be created |
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)
})) | n/a | yes |
+| [stack\_prefix](#input\_stack\_prefix) | Stack Prefix for resource generation | `string` | `"dlm_lifecycle"` | no |
+| [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 |
+|------|-------------|
+| [arns](#output\_arns) | Amazon Resource Name (ARN) of the DLM Lifecycle Policy |
+| [ids](#output\_ids) | Identifier of the DLM Lifecycle Policy |
+
## 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)
\ No newline at end of file
+AWS DLM Lifecycle Policy: [terraform.io/docs/providers/aws/r/dlm_lifecycle_policy](https://www.terraform.io/docs/providers/aws/r/dlm_lifecycle_policy)
diff --git a/main.tf b/main.tf
index 81e3226..e074dd8 100644
--- a/main.tf
+++ b/main.tf
@@ -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"]
}
}
diff --git a/outputs.tf b/outputs.tf
index 92119cd..15e058b 100644
--- a/outputs.tf
+++ b/outputs.tf
@@ -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
}
diff --git a/variables.tf b/variables.tf
index 4b66113..28f9845 100644
--- a/variables.tf
+++ b/variables.tf
@@ -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)
+ }))
}
diff --git a/versions.tf b/versions.tf
index f784f7b..b5d848d 100644
--- a/versions.tf
+++ b/versions.tf
@@ -1,5 +1,5 @@
terraform {
- required_version = ">= 0.12"
+ required_version = ">= 1.3.0"
required_providers {
aws = ">= 2.46.0"
}