Skip to content

Commit 4f7fe7b

Browse files
committed
Change resources variable to selections object to allow more customization
1 parent b48e521 commit 4f7fe7b

File tree

6 files changed

+97
-26
lines changed

6 files changed

+97
-26
lines changed

README.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ great choice.
5555
| Name | Description | Type | Default | Required |
5656
|------|-------------|------|---------|:--------:|
5757
| <a name="input_changeable_for_days"></a> [changeable\_for\_days](#input\_changeable\_for\_days) | The number of days before the lock date. If omitted creates a vault lock in governance mode, otherwise it will create<br> a vault lock in compliance mode. When you apply this setting:<br><br> The vault will become immutable in 3 days after applying. You have 3 days of grace time to manage or delete the vault<br> lock before it becomes immutable. During this time, only those users with specific IAM permissions can make changes.<br><br> Once the vault is locked in compliance mode, it cannot be managed or deleted by anyone, even the root user or AWS.<br> The only way to deactivate the lock is to terminate the account, which will delete all the backups.<br><br> Since you cannot delete the Vault, it will be charged for backups until that date. Be careful! | `number` | `null` | no |
58-
| <a name="input_custom_rules"></a> [custom\_rules](#input\_custom\_rules) | Backup rules to add to the AWS Backup Vault. See examples for usage. | <pre>list(object({<br> name = string<br> schedule = optional(string)<br><br> start_window = optional(number)<br> completion_window = optional(number)<br><br> enable_continuous_backup = optional(bool)<br> recovery_point_tags = optional(map(string), {})<br><br> lifecycle = optional(object({<br> cold_storage_after = optional(number)<br> delete_after = optional(number)<br> }))<br><br> copy_action = optional(object({<br> destination_vault_arn = optional(string)<br> lifecycle = optional(object({<br> cold_storage_after = optional(number)<br> delete_after = optional(number)<br> }))<br> }))<br> }))</pre> | `[]` | no |
58+
| <a name="input_custom_rules"></a> [custom\_rules](#input\_custom\_rules) | Backup rules to add to the AWS Backup Vault. See examples for usage. | <pre>list(object({<br> name = string<br> schedule = optional(string)<br><br> start_window = optional(number)<br> completion_window = optional(number)<br><br> enable_continuous_backup = optional(bool)<br> recovery_point_tags = optional(map(string), {})<br><br> lifecycle = optional(object({<br> cold_storage_after = optional(number)<br> delete_after = optional(number)<br> }))<br><br> copy_action = optional(object({<br> destination_vault_arn = optional(string)<br> lifecycle = optional(object({<br> cold_storage_after = optional(number)<br> delete_after = optional(number)<br> }))<br> }))<br> }))</pre> | `[]` | no |
5959
| <a name="input_enable_customer_managed_kms"></a> [enable\_customer\_managed\_kms](#input\_enable\_customer\_managed\_kms) | Whether to enable customer managed KMS encryption for the backup vault. | `bool` | `false` | no |
6060
| <a name="input_enable_vault_lock"></a> [enable\_vault\_lock](#input\_enable\_vault\_lock) | Whether to enable Vault Lock for the backup vault. | `bool` | `false` | no |
6161
| <a name="input_enable_windows_vss_backup"></a> [enable\_windows\_vss\_backup](#input\_enable\_windows\_vss\_backup) | Whether to enable Windows VSS backup for the backup plan. | `bool` | `false` | no |
@@ -64,8 +64,8 @@ great choice.
6464
| <a name="input_min_retention_days"></a> [min\_retention\_days](#input\_min\_retention\_days) | The minimum retention period that the vault retains its recovery points. | `number` | `7` | no |
6565
| <a name="input_plan_name"></a> [plan\_name](#input\_plan\_name) | The display name of the backup plan. | `string` | n/a | yes |
6666
| <a name="input_predefined_rules"></a> [predefined\_rules](#input\_predefined\_rules) | A list of predefined backup rules to add to the AWS Backup Plan. See examples for usage. | `list(string)` | `[]` | no |
67-
| <a name="input_resources"></a> [resources](#input\_resources) | An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to assign to a backup plan. | `list(string)` | `[]` | no |
6867
| <a name="input_role_arn"></a> [role\_arn](#input\_role\_arn) | The ARN of the IAM role that AWS Backup uses to authenticate when restoring or backing up the target resources. If left empty, a default role will be created. | `string` | `null` | no |
68+
| <a name="input_selections"></a> [selections](#input\_selections) | An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to assign to a backup plan. | <pre>list(object({<br> name = string<br> role_arn = optional(string)<br><br> arns = optional(list(string))<br> tag = optional(object({<br> type = string<br> key = string<br> value = string<br> }))<br> }))</pre> | `[]` | no |
6969
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to add to the AWS Backup. | `map(any)` | `{}` | no |
7070
| <a name="input_vault_force_destroy"></a> [vault\_force\_destroy](#input\_vault\_force\_destroy) | Whether to allow the backup vault to be destroyed even if it contains recovery points. | `string` | `false` | no |
7171
| <a name="input_vault_name"></a> [vault\_name](#input\_vault\_name) | Name of the backup vault to create. | `string` | n/a | yes |
@@ -98,9 +98,19 @@ great choice.
9898
module "basic-example" {
9999
source = "../../"
100100
101-
vault_name = "main"
102-
plan_name = "s3"
103-
resources = ["arn:aws:s3:::my-bucket"]
101+
vault_name = "my-project"
102+
plan_name = "customer-data"
103+
104+
selections = [
105+
{
106+
name = "s3-buckets"
107+
arns = ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-other-bucket"]
108+
},
109+
{
110+
name = "db-snaps"
111+
arns = ["arn:aws:rds:us-east-2:123456789012:db:my-mysql-instance"]
112+
}
113+
]
104114
}
105115
```
106116

@@ -109,9 +119,8 @@ module "basic-example" {
109119
module "with-rules" {
110120
source = "../../"
111121
112-
vault_name = "main"
113-
plan_name = "s3"
114-
resources = ["arn:aws:s3:::my-bucket"]
122+
vault_name = "my-project"
123+
plan_name = "customer-data"
115124
116125
predefined_rules = ["daily-snapshot", "monthly-snapshot"]
117126
custom_rules = [
@@ -128,6 +137,17 @@ module "with-rules" {
128137
}
129138
}
130139
]
140+
141+
selections = [
142+
{
143+
name = "s3-buckets"
144+
arns = ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-other-bucket"]
145+
},
146+
{
147+
name = "db-snaps"
148+
arns = ["arn:aws:rds:us-east-2:123456789012:db:my-mysql-instance"]
149+
}
150+
]
131151
}
132152
```
133153

examples/minimal/main.tf

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
module "basic-example" {
22
source = "../../"
33

4-
vault_name = "main"
5-
plan_name = "s3"
6-
resources = ["arn:aws:s3:::my-bucket"]
4+
vault_name = "my-project"
5+
plan_name = "customer-data"
6+
7+
selections = [
8+
{
9+
name = "s3-buckets"
10+
arns = ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-other-bucket"]
11+
},
12+
{
13+
name = "db-snaps"
14+
arns = ["arn:aws:rds:us-east-2:123456789012:db:my-mysql-instance"]
15+
}
16+
]
717
}

examples/with-predefined-rules/main.tf

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module "with-rules" {
22
source = "../../"
33

4-
vault_name = "main"
5-
plan_name = "s3"
6-
resources = ["arn:aws:s3:::my-bucket"]
4+
vault_name = "my-project"
5+
plan_name = "customer-data"
76

87
predefined_rules = ["daily-snapshot", "monthly-snapshot"]
98
custom_rules = [
@@ -20,4 +19,15 @@ module "with-rules" {
2019
}
2120
}
2221
]
22+
23+
selections = [
24+
{
25+
name = "s3-buckets"
26+
arns = ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-other-bucket"]
27+
},
28+
{
29+
name = "db-snaps"
30+
arns = ["arn:aws:rds:us-east-2:123456789012:db:my-mysql-instance"]
31+
}
32+
]
2333
}

main.tf

+14-4
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,23 @@ resource "aws_backup_plan" "main" {
101101
}
102102

103103
resource "aws_backup_selection" "main" {
104-
count = length(var.resources) > 0 ? 1 : 0
104+
for_each = { for sel in var.selections : sel.name => sel }
105105

106-
name = "${var.vault_name}-backup"
106+
name = "${var.vault_name}-${each.key}"
107107
plan_id = aws_backup_plan.main.id
108108

109-
iam_role_arn = coalesce(var.role_arn, module.iam_role[0].arn)
110-
resources = var.resources
109+
iam_role_arn = coalesce(each.value.role_arn, module.iam_role[0].arn)
110+
resources = each.value.arns
111+
112+
dynamic "selection_tag" {
113+
for_each = each.value.tag != null ? [each.value.tag] : []
114+
115+
content {
116+
key = selection_tag.value.key
117+
type = selection_tag.value.type
118+
value = selection_tag.value.value
119+
}
120+
}
111121
}
112122

113123
module "iam_role" {

tests/custom_rules.tftest.hcl

+17-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ run "create_vault_with_custom_rules" {
1818
}
1919
]
2020

21-
resources = [
22-
"arn:aws:s3:::example-bucket-arn",
23-
"arn:aws:elasticfilesystem:eu-central-1:*:file-system/fs-0123456789abcdef8"
21+
selections = [
22+
{
23+
name = "s3-buckets"
24+
arns = ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-other-bucket"]
25+
},
26+
{
27+
name = "db-snaps"
28+
arns = ["arn:aws:rds:eu-central-1:*:db:my-mysql-instance"]
29+
}
2430
]
2531

2632
tags = {
@@ -50,8 +56,13 @@ run "create_vault_with_custom_rules" {
5056
}
5157

5258
assert {
53-
condition = length(aws_backup_selection.main[0].resources) == 2
54-
error_message = "Expected backup selection to contain 2 resources."
59+
condition = length(aws_backup_selection.main["s3-buckets"].resources) == 2
60+
error_message = "Expected S3 backup selection to contain 2 resources."
61+
}
62+
63+
assert {
64+
condition = length(aws_backup_selection.main["db-snaps"].resources) == 1
65+
error_message = "Expected DB backup selection to contain 1 resources."
5566
}
5667

5768
assert {
@@ -60,7 +71,7 @@ run "create_vault_with_custom_rules" {
6071
}
6172

6273
assert {
63-
condition = aws_backup_selection.main[0].iam_role_arn == module.iam_role[0].arn
74+
condition = aws_backup_selection.main["s3-buckets"].iam_role_arn == module.iam_role[0].arn
6475
error_message = "Expected backup selection IAM role to be the default one."
6576
}
6677
}

variables.tf

+12-2
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,20 @@ variable "enable_windows_vss_backup" {
115115
}
116116

117117
# Backup Selection
118-
variable "resources" {
118+
variable "selections" {
119119
description = "An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to assign to a backup plan."
120120
default = []
121-
type = list(string)
121+
type = list(object({
122+
name = string
123+
role_arn = optional(string)
124+
125+
arns = optional(list(string))
126+
tag = optional(object({
127+
type = string
128+
key = string
129+
value = string
130+
}))
131+
}))
122132
}
123133

124134
variable "role_arn" {

0 commit comments

Comments
 (0)