Skip to content

Commit d6cbd8d

Browse files
authored
Merge pull request #10 from geekcell/vault-creation-optional
fix: Make vault creation optional
2 parents 279b7cd + a1f9c7e commit d6cbd8d

6 files changed

+30
-12
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +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_create_backup_vault"></a> [create\_backup\_vault](#input\_create\_backup\_vault) | Whether to create a backup vault or use a pre-existing one. | `bool` | `true` | no |
5859
| <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 |
5960
| <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 |
6061
| <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 |
@@ -68,7 +69,7 @@ great choice.
6869
| <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 |
6970
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to add to the AWS Backup. | `map(any)` | `{}` | no |
7071
| <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 |
71-
| <a name="input_vault_name"></a> [vault\_name](#input\_vault\_name) | Name of the backup vault to create. | `string` | n/a | yes |
72+
| <a name="input_vault_name"></a> [vault\_name](#input\_vault\_name) | Name of the backup vault to create or use and existing one. | `string` | n/a | yes |
7273

7374
## Outputs
7475

@@ -87,10 +88,11 @@ great choice.
8788

8889
## Resources
8990

90-
- resource.aws_backup_plan.main (main.tf#45)
91-
- resource.aws_backup_selection.main (main.tf#103)
92-
- resource.aws_backup_vault.main (main.tf#27)
93-
- resource.aws_backup_vault_lock_configuration.main (main.tf#35)
91+
- resource.aws_backup_plan.main (main.tf#53)
92+
- resource.aws_backup_selection.main (main.tf#113)
93+
- resource.aws_backup_vault.main (main.tf#33)
94+
- resource.aws_backup_vault_lock_configuration.main (main.tf#43)
95+
- data source.aws_backup_vault.main (main.tf#27)
9496

9597
# Examples
9698
### Basic Example

main.tf

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ locals {
2424
)
2525
}
2626

27+
data "aws_backup_vault" "main" {
28+
count = var.create_backup_vault ? 0 : 1
29+
30+
name = var.vault_name
31+
}
32+
2733
resource "aws_backup_vault" "main" {
34+
count = var.create_backup_vault ? 1 : 0
35+
2836
name = var.vault_name
2937
force_destroy = var.vault_force_destroy
3038
kms_key_arn = var.enable_customer_managed_kms ? module.kms[0].key_arn : var.kms_key_id
@@ -49,7 +57,7 @@ resource "aws_backup_plan" "main" {
4957
for_each = local.merged_rules
5058

5159
content {
52-
target_vault_name = aws_backup_vault.main.name
60+
target_vault_name = var.vault_name
5361

5462
rule_name = rule.value.name
5563
schedule = rule.value.schedule
@@ -97,6 +105,8 @@ resource "aws_backup_plan" "main" {
97105
}
98106
}
99107

108+
depends_on = [aws_backup_vault.main]
109+
100110
tags = var.tags
101111
}
102112

outputs.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
output "backup_vault_id" {
22
description = "The ID of the backup vault."
3-
value = aws_backup_vault.main.id
3+
value = var.create_backup_vault ? aws_backup_vault.main[0].id : data.aws_backup_vault.main[0].id
44
}
55

66
output "backup_vault_arn" {
77
description = "The ARN of the backup vault."
8-
value = aws_backup_vault.main.arn
8+
value = var.create_backup_vault ? aws_backup_vault.main[0].arn : data.aws_backup_vault.main[0].arn
99
}
1010

1111
output "backup_plan_id" {

tests/custom_rules.tftest.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ run "create_vault_with_custom_rules" {
4141
}
4242

4343
assert {
44-
condition = length(aws_backup_vault.main.kms_key_arn) >= 1
44+
condition = length(aws_backup_vault.main[0].kms_key_arn) >= 1
4545
error_message = "Expected Backup Plan to be encrypted by default AWS KMS key."
4646
}
4747

tests/predefined_rules.tftest.hcl

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ run "create_vault_with_predefined_rules" {
2121
}
2222

2323
assert {
24-
condition = length(aws_backup_vault.main.kms_key_arn) >= 1
24+
condition = length(aws_backup_vault.main[0].kms_key_arn) >= 1
2525
error_message = "Expected Backup Plan to be encrypted by default AWS KMS key."
2626
}
2727

@@ -46,7 +46,7 @@ run "create_vault_with_predefined_rules" {
4646
}
4747

4848
assert {
49-
condition = length(aws_backup_vault.main.tags) == 2
49+
condition = length(aws_backup_vault.main[0].tags) == 2
5050
error_message = "Expected Vault to have 2 tags."
5151
}
5252

variables.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ variable "tags" {
66
}
77

88
# Backup Vault
9+
variable "create_backup_vault" {
10+
description = "Whether to create a backup vault or use a pre-existing one."
11+
default = true
12+
type = bool
13+
}
14+
915
variable "vault_name" {
10-
description = "Name of the backup vault to create."
16+
description = "Name of the backup vault to create or use and existing one."
1117
type = string
1218
}
1319

0 commit comments

Comments
 (0)