Skip to content

Commit c9b3bbe

Browse files
author
Abdul Wahid
authored
New 'not_resources' feature + version compatibility (#17)
* Set minimum provider version to 4.26.0 to allow use of 'force_destroy' * Add new 'not_resources' feature * Lint fixes
1 parent b658aa0 commit c9b3bbe

16 files changed

+167
-21
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ override.tf.json
2828

2929
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
3030
# example: *tfplan*
31+
32+
**/.infracost

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.3.0
3+
rev: v4.4.0
44
hooks:
55
- id: check-added-large-files
66
args: ['--maxkb=500']
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: https://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.76.0
21+
rev: v1.81.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ Module managed by:
8686

8787
| Name | Version |
8888
|------|---------|
89-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.11 |
90-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
89+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
90+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.26.0 |
9191

9292
## Providers
9393

9494
| Name | Version |
9595
|------|---------|
96-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.35.0 |
96+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.26.0 |
9797

9898
## Modules
9999

@@ -112,6 +112,8 @@ No modules.
112112
| [aws_iam_role_policy_attachment.main_custom_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
113113
| [aws_iam_role_policy_attachment.main_role_backup_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
114114
| [aws_iam_role_policy_attachment.main_role_restore_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
115+
| [aws_iam_role_policy_attachment.main_role_s3_backup_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
116+
| [aws_iam_role_policy_attachment.main_role_s3_restore_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
115117
| [aws_sns_topic.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
116118
| [aws_sns_topic_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy) | resource |
117119
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -131,6 +133,7 @@ No modules.
131133
| <a name="input_plan_name"></a> [plan\_name](#input\_plan\_name) | The display name of a backup plan | `string` | n/a | yes |
132134
| <a name="input_rules"></a> [rules](#input\_rules) | A list of rules mapping rule configurations for a backup plan | `any` | `[]` | no |
133135
| <a name="input_selection_name"></a> [selection\_name](#input\_selection\_name) | The display name of a resource selection document | `string` | `null` | no |
136+
| <a name="input_selection_not_resources"></a> [selection\_not\_resources](#input\_selection\_not\_resources) | An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to exclude from a backup plan. | `list(string)` | `[]` | no |
134137
| <a name="input_selection_resources"></a> [selection\_resources](#input\_selection\_resources) | A list of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to assign to a backup plan | `list(string)` | `[]` | no |
135138
| <a name="input_selection_tags"></a> [selection\_tags](#input\_selection\_tags) | A list of selection tags map | `list(any)` | `[]` | no |
136139
| <a name="input_sns_topic_arn"></a> [sns\_topic\_arn](#input\_sns\_topic\_arn) | The Amazon Resource Name (ARN) that specifies the topic for a backup vault’s events | `string` | `null` | no |

examples/exclusions/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Example deployment flow
2+
3+
```bash
4+
terraform init
5+
terraform validate
6+
terraform plan
7+
terraform apply --auto-approve
8+
```

examples/exclusions/main.tf

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
######
2+
# KMS
3+
######
4+
data "aws_kms_key" "backup" {
5+
key_id = "alias/aws/backup"
6+
}
7+
8+
resource "aws_s3_bucket" "example" {
9+
bucket = "umotif-test-bucket"
10+
11+
tags = {
12+
Environment = "test"
13+
}
14+
}
15+
16+
#########
17+
# Backup
18+
#########
19+
module "backup" {
20+
source = "../.."
21+
22+
# Create a vault
23+
vault_name = "${var.name_prefix}-vault-exclusions"
24+
vault_kms_key_arn = data.aws_kms_key.backup.arn
25+
26+
# Create a backup plan
27+
plan_name = "${var.name_prefix}-backup-plan"
28+
29+
rules = [
30+
{
31+
name = "${var.name_prefix}-backup-rule"
32+
schedule = "cron(0 12 * * ? *)"
33+
start_window = "65"
34+
completion_window = "180"
35+
recovery_point_tags = {
36+
Project = "test"
37+
Region = "eu-west-1"
38+
}
39+
40+
lifecycle = {
41+
delete_after = 90
42+
}
43+
}
44+
]
45+
46+
selection_name = "${var.name_prefix}-backup-selection"
47+
48+
selection_resources = ["*"]
49+
selection_not_resources = [aws_s3_bucket.example.arn]
50+
51+
tags = {
52+
Environment = "test"
53+
}
54+
}

examples/exclusions/outputs.tf

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
output "backup_vault_id" {
2+
description = "The name of the AWS Backup Vault"
3+
value = module.backup.backup_vault_id
4+
}
5+
6+
output "backup_vault_arn" {
7+
description = "The Amazon Resource Name (ARN) that identifies the AWS Backup Vault"
8+
value = module.backup.backup_vault_arn
9+
}
10+
11+
output "backup_vault_recovery_points" {
12+
description = "The number of recovery points that are stored in a backup vault"
13+
value = module.backup.backup_vault_recovery_points
14+
}
15+
16+
output "backup_plan_id" {
17+
description = "The name of the backup plan"
18+
value = module.backup.backup_plan_id
19+
}
20+
21+
output "backup_plan_arn" {
22+
description = "The Amazon Resource Name (ARN) that identifies the backup plan"
23+
value = module.backup.backup_plan_arn
24+
}
25+
26+
output "backup_plan_version" {
27+
description = "Unique, randomly generated, Unicode, UTF-8 encoded string that serves as the version ID of the backup plan."
28+
value = module.backup.backup_plan_version
29+
}
30+
31+
output "backup_selection_id" {
32+
description = "The identifier of the backup selection"
33+
value = module.backup.backup_selection_id
34+
}
35+
36+
output "backup_vault_iam_role_name" {
37+
description = "The name of the backup IAM role"
38+
value = module.backup.backup_vault_iam_role_name
39+
}
40+
41+
output "backup_vault_iam_role_arn" {
42+
description = "The ARN of the backup IAM role"
43+
value = module.backup.backup_vault_iam_role_arn
44+
}

examples/exclusions/variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "name_prefix" {
2+
description = "A prefix used for naming resources."
3+
type = string
4+
default = "example"
5+
}

examples/exclusions/versions.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = "~> 1.5.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.26.0"
8+
}
9+
}
10+
}

examples/external-vault/versions.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 1.0.11"
2+
required_version = ">= 1.5.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.26.0"
88
}
99
}
10-
}
10+
}

examples/multiple-dbs/versions.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 1.0.11"
2+
required_version = ">= 1.5.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.26.0"
88
}
99
}
10-
}
10+
}

examples/one-db/versions.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 1.0.11"
2+
required_version = ">= 1.5.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.26.0"
88
}
99
}
10-
}
10+
}

examples/vault/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ output "backup_vault_iam_role_name" {
3939
}
4040

4141
output "backup_vault_iam_role_arn" {
42-
output = module.backup.backup_vault_iam_role_name.arn
42+
value = module.backup.backup_vault_iam_role_name.arn
4343
}

examples/vault/versions.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 1.0.11"
2+
required_version = ">= 1.5.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.26.0"
88
}
99
}
10-
}
10+
}

main.tf

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ resource "aws_backup_plan" "main" {
1818

1919
dynamic "rule" {
2020
for_each = var.rules
21+
2122
content {
2223
rule_name = lookup(rule.value, "name")
2324
target_vault_name = var.vault_name != null ? aws_backup_vault.main[0].name : lookup(rule.value, "target_vault_name", "Default")
@@ -29,6 +30,7 @@ resource "aws_backup_plan" "main" {
2930

3031
dynamic "lifecycle" {
3132
for_each = length(lookup(rule.value, "lifecycle")) == 0 ? [] : [lookup(rule.value, "lifecycle", {})]
33+
3234
content {
3335
cold_storage_after = lookup(lifecycle.value, "cold_storage_after", 0)
3436
delete_after = lookup(lifecycle.value, "delete_after", 90)
@@ -37,6 +39,7 @@ resource "aws_backup_plan" "main" {
3739

3840
dynamic "copy_action" {
3941
for_each = length(lookup(rule.value, "copy_action", {})) == 0 ? [] : [lookup(rule.value, "copy_action", {})]
42+
4043
content {
4144
destination_vault_arn = lookup(copy_action.value, "destination_vault_arn", null)
4245

@@ -71,7 +74,8 @@ resource "aws_backup_selection" "main" {
7174
name = var.selection_name
7275
plan_id = aws_backup_plan.main.id
7376

74-
resources = var.selection_resources
77+
resources = var.selection_resources
78+
not_resources = var.selection_not_resources
7579

7680
dynamic "selection_tag" {
7781
for_each = var.selection_tags
@@ -128,6 +132,16 @@ resource "aws_iam_role_policy_attachment" "main_role_restore_policy_attach" {
128132
role = aws_iam_role.main.name
129133
}
130134

135+
resource "aws_iam_role_policy_attachment" "main_role_s3_backup_policy_attach" {
136+
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Backup"
137+
role = aws_iam_role.main.name
138+
}
139+
140+
resource "aws_iam_role_policy_attachment" "main_role_s3_restore_policy_attach" {
141+
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Restore"
142+
role = aws_iam_role.main.name
143+
}
144+
131145
resource "aws_iam_policy" "main_custom_policy" {
132146
description = "AWS Backup Tag policy"
133147
policy = data.aws_iam_policy_document.main_custom_policy.json

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ variable "selection_resources" {
3030
default = []
3131
}
3232

33+
variable "selection_not_resources" {
34+
description = "An array of strings that either contain Amazon Resource Names (ARNs) or match patterns of resources to exclude from a backup plan."
35+
type = list(string)
36+
default = []
37+
}
38+
3339
variable "selection_tags" {
3440
description = "A list of selection tags map"
3541
type = list(any)

versions.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
terraform {
2-
required_version = ">= 1.0.11"
2+
required_version = ">= 1.5.0"
33

44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0.0"
7+
version = ">= 4.26.0"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)