Skip to content

Commit 976339d

Browse files
author
Abdul Wahid
authored
Add SNS Notifications to Backup Vault (#10)
* Add SNS Notifications to Backup Vault * Revert "Cleanup (#7)" This reverts commit d0b5baf. * Update examples * Change policy documents from json to data sources * Cleanup IAM policies to be China friendly * Use a bool for creating sns topic * Fix conditions + move data sources to data.tf
1 parent b124d74 commit 976339d

File tree

13 files changed

+357
-361
lines changed

13 files changed

+357
-361
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.48.0
21+
rev: v1.50.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

CHANGELOG.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ All notable changes to this project will be documented in this file.
55
<a name="unreleased"></a>
66
## [Unreleased]
77

8-
- Documentation updates
9-
- Use default KMS keys
10-
- Introduce Terratest
8+
- Add SNS Notifications to Backup Vault
9+
- Add continuous backup feature ([#8](https://github.com/umotif-public/terraform-aws-backup/issues/8))
10+
- Update README.md
11+
- Add github actions workflow for Terratest ([#6](https://github.com/umotif-public/terraform-aws-backup/issues/6))
12+
13+
14+
<a name="1.2.0"></a>
15+
## [1.2.0] - 2020-12-15
16+
17+
- Module testing ([#5](https://github.com/umotif-public/terraform-aws-backup/issues/5))
1118

1219

1320
<a name="1.1.0"></a>
@@ -30,6 +37,7 @@ All notable changes to this project will be documented in this file.
3037
- Initial commit
3138

3239

33-
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.1.0...HEAD
40+
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.2.0...HEAD
41+
[1.2.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.1.0...1.2.0
3442
[1.1.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.0.1...1.1.0
3543
[1.0.1]: https://github.com/umotif-public/terraform-aws-backup/compare/1.0.0...1.0.1

README.md

+57-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
[![Lint and Validate](https://github.com/umotif-public/terraform-aws-backup/workflows/Lint%20and%20Validate/badge.svg)](https://github.com/umotif-public/terraform-aws-backup/actions?query=workflow%3A%22Lint+and+Validate%22)
33
[![Terratest](https://github.com/umotif-public/terraform-aws-backup/workflows/Terratest/badge.svg)](https://github.com/umotif-public/terraform-aws-backup/actions?query=workflow%3ATerratest)
44

5-
65
# Terraform AWS Backup
76

87
Terraform module to provision [AWS Backup](https://aws.amazon.com/backup/) resources.
@@ -86,56 +85,84 @@ Module managed by:
8685

8786
| Name | Version |
8887
|------|---------|
89-
| terraform | >= 0.13 |
90-
| aws | >= 3.11 |
88+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
89+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.11 |
9190

9291
## Providers
9392

9493
| Name | Version |
9594
|------|---------|
96-
| aws | >= 3.11 |
95+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.11 |
9796

9897
## Modules
9998

100-
No Modules.
99+
No modules.
101100

102101
## Resources
103102

104-
| Name |
105-
|------|
106-
| [aws_backup_plan](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_plan) |
107-
| [aws_backup_selection](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_selection) |
108-
| [aws_backup_vault](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_vault) |
109-
| [aws_iam_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) |
110-
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) |
111-
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) |
103+
| Name | Type |
104+
|------|------|
105+
| [aws_backup_plan.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_plan) | resource |
106+
| [aws_backup_selection.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_selection) | resource |
107+
| [aws_backup_vault.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_vault) | resource |
108+
| [aws_backup_vault_notifications.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/backup_vault_notifications) | resource |
109+
| [aws_iam_policy.main_custom_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
110+
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
111+
| [aws_iam_role_policy_attachment.main_custom_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
112+
| [aws_iam_role_policy_attachment.main_role_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
113+
| [aws_sns_topic.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
114+
| [aws_sns_topic_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy) | resource |
115+
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
116+
| [aws_iam_policy_document.main_custom_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
117+
| [aws_iam_policy_document.sns_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
118+
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
119+
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
112120

113121
## Inputs
114122

115123
| Name | Description | Type | Default | Required |
116124
|------|-------------|------|---------|:--------:|
117-
| advanced\_backup\_settings | An object that specifies backup options for each resource type | `any` | `[]` | no |
118-
| iam\_role\_name | Name of IAM Role to associate to the Backup Plan | `string` | `null` | no |
119-
| plan\_name | The display name of a backup plan | `string` | n/a | yes |
120-
| rules | A list of rules mapping rule configurations for a backup plan | `any` | `[]` | no |
121-
| selection\_name | The display name of a resource selection document | `string` | `null` | no |
122-
| 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 |
123-
| selection\_tags | A list of selection tags map | `list(any)` | `[]` | no |
124-
| tags | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
125-
| vault\_kms\_key\_arn | The server-side encryption key that is used to protect your backups | `string` | `null` | no |
126-
| vault\_name | Name of the backup vault to create. If not given, AWS use default | `string` | `null` | no |
125+
| <a name="input_advanced_backup_settings"></a> [advanced\_backup\_settings](#input\_advanced\_backup\_settings) | An object that specifies backup options for each resource type | `any` | `[]` | no |
126+
| <a name="input_backup_vault_events"></a> [backup\_vault\_events](#input\_backup\_vault\_events) | An array of events that indicate the status of jobs to back up resources to the backup vault. | `list(string)` | <pre>[<br> "BACKUP_JOB_STARTED",<br> "BACKUP_JOB_COMPLETED",<br> "BACKUP_JOB_SUCCESSFUL",<br> "BACKUP_JOB_FAILED",<br> "BACKUP_JOB_EXPIRED",<br> "RESTORE_JOB_STARTED",<br> "RESTORE_JOB_COMPLETED",<br> "RESTORE_JOB_SUCCESSFUL",<br> "RESTORE_JOB_FAILED",<br> "COPY_JOB_STARTED",<br> "COPY_JOB_SUCCESSFUL",<br> "COPY_JOB_FAILED",<br> "RECOVERY_POINT_MODIFIED",<br> "BACKUP_PLAN_CREATED",<br> "BACKUP_PLAN_MODIFIED"<br>]</pre> | no |
127+
| <a name="input_create_sns_topic"></a> [create\_sns\_topic](#input\_create\_sns\_topic) | Create SNS Topic | `bool` | `true` | no |
128+
| <a name="input_enable_sns_notifications"></a> [enable\_sns\_notifications](#input\_enable\_sns\_notifications) | Enable Backup Vault Notifications | `bool` | `false` | no |
129+
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name of IAM Role to associate to the Backup Plan | `string` | `null` | no |
130+
| <a name="input_plan_name"></a> [plan\_name](#input\_plan\_name) | The display name of a backup plan | `string` | n/a | yes |
131+
| <a name="input_rule_completion_window"></a> [rule\_completion\_window](#input\_rule\_completion\_window) | The amount of time AWS Backup attempts a backup before canceling the job and returning an error | `number` | `null` | no |
132+
| <a name="input_rule_copy_action_destination_vault_arn"></a> [rule\_copy\_action\_destination\_vault\_arn](#input\_rule\_copy\_action\_destination\_vault\_arn) | An Amazon Resource Name (ARN) that uniquely identifies the destination backup vault for the copied backup. | `string` | `null` | no |
133+
| <a name="input_rule_copy_action_lifecycle"></a> [rule\_copy\_action\_lifecycle](#input\_rule\_copy\_action\_lifecycle) | The lifecycle defines when a protected resource is copied over to a backup vault and when it expires. | `map(any)` | `{}` | no |
134+
| <a name="input_rule_lifecycle_cold_storage_after"></a> [rule\_lifecycle\_cold\_storage\_after](#input\_rule\_lifecycle\_cold\_storage\_after) | Specifies the number of days after creation that a recovery point is moved to cold storage | `number` | `null` | no |
135+
| <a name="input_rule_lifecycle_delete_after"></a> [rule\_lifecycle\_delete\_after](#input\_rule\_lifecycle\_delete\_after) | Specifies the number of days after creation that a recovery point is deleted. Must be 90 days greater than `cold_storage_after` | `number` | `null` | no |
136+
| <a name="input_rule_name"></a> [rule\_name](#input\_rule\_name) | An display name for a backup rule | `string` | `null` | no |
137+
| <a name="input_rule_recovery_point_tags"></a> [rule\_recovery\_point\_tags](#input\_rule\_recovery\_point\_tags) | Metadata that you can assign to help organize the resources that you create | `map(string)` | `{}` | no |
138+
| <a name="input_rule_schedule"></a> [rule\_schedule](#input\_rule\_schedule) | A CRON expression specifying when AWS Backup initiates a backup job | `string` | `null` | no |
139+
| <a name="input_rule_start_window"></a> [rule\_start\_window](#input\_rule\_start\_window) | The amount of time in minutes before beginning a backup | `number` | `null` | no |
140+
| <a name="input_rules"></a> [rules](#input\_rules) | A list of rules mapping rule configurations for a backup plan | `any` | `[]` | no |
141+
| <a name="input_selection_name"></a> [selection\_name](#input\_selection\_name) | The display name of a resource selection document | `string` | `null` | no |
142+
| <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 |
143+
| <a name="input_selection_tag_key"></a> [selection\_tag\_key](#input\_selection\_tag\_key) | The key in a key-value pair | `string` | `null` | no |
144+
| <a name="input_selection_tag_type"></a> [selection\_tag\_type](#input\_selection\_tag\_type) | An operation, such as StringEquals, that is applied to a key-value pair used to filter resources in a selection | `string` | `null` | no |
145+
| <a name="input_selection_tag_value"></a> [selection\_tag\_value](#input\_selection\_tag\_value) | The value in a key-value pair | `string` | `null` | no |
146+
| <a name="input_selection_tags"></a> [selection\_tags](#input\_selection\_tags) | A list of selection tags map | `list(any)` | `[]` | no |
147+
| <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 |
148+
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
149+
| <a name="input_vault_kms_key_arn"></a> [vault\_kms\_key\_arn](#input\_vault\_kms\_key\_arn) | The server-side encryption key that is used to protect your backups | `string` | `null` | no |
150+
| <a name="input_vault_name"></a> [vault\_name](#input\_vault\_name) | Name of the backup vault to create. If not given, AWS use default | `string` | `null` | no |
151+
| <a name="input_vault_sns_kms_key_arn"></a> [vault\_sns\_kms\_key\_arn](#input\_vault\_sns\_kms\_key\_arn) | The server-side encryption key that is used to protect SNS messages for backups | `string` | `null` | no |
127152

128153
## Outputs
129154

130155
| Name | Description |
131156
|------|-------------|
132-
| backup\_plan\_arn | The Amazon Resource Name (ARN) that identifies the backup plan |
133-
| backup\_plan\_id | The name of the backup plan |
134-
| backup\_plan\_version | Unique, randomly generated, Unicode, UTF-8 encoded string that serves as the version ID of the backup plan. |
135-
| backup\_selection\_id | The identifier of the backup selection |
136-
| backup\_vault\_arn | The Amazon Resource Name (ARN) that identifies the AWS Backup Vault |
137-
| backup\_vault\_id | The name of the AWS Backup Vault |
138-
| backup\_vault\_recovery\_points | The number of recovery points that are stored in a backup vault |
157+
| <a name="output_backup_plan_arn"></a> [backup\_plan\_arn](#output\_backup\_plan\_arn) | The Amazon Resource Name (ARN) that identifies the backup plan |
158+
| <a name="output_backup_plan_id"></a> [backup\_plan\_id](#output\_backup\_plan\_id) | The name of the backup plan |
159+
| <a name="output_backup_plan_version"></a> [backup\_plan\_version](#output\_backup\_plan\_version) | Unique, randomly generated, Unicode, UTF-8 encoded string that serves as the version ID of the backup plan. |
160+
| <a name="output_backup_selection_id"></a> [backup\_selection\_id](#output\_backup\_selection\_id) | The identifier of the backup selection |
161+
| <a name="output_backup_sns_topic_arn"></a> [backup\_sns\_topic\_arn](#output\_backup\_sns\_topic\_arn) | The Amazon Resource Name (ARN) that specifies the topic for a backup vault’s events |
162+
| <a name="output_backup_vault_arn"></a> [backup\_vault\_arn](#output\_backup\_vault\_arn) | The Amazon Resource Name (ARN) that identifies the AWS Backup Vault |
163+
| <a name="output_backup_vault_events"></a> [backup\_vault\_events](#output\_backup\_vault\_events) | An array of events that indicate the status of jobs to back up resources to the backup vault. |
164+
| <a name="output_backup_vault_id"></a> [backup\_vault\_id](#output\_backup\_vault\_id) | The name of the AWS Backup Vault |
165+
| <a name="output_backup_vault_recovery_points"></a> [backup\_vault\_recovery\_points](#output\_backup\_vault\_recovery\_points) | The number of recovery points that are stored in a backup vault |
139166
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
140167

141168
## License

data.tf

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
data "aws_region" "current" {}
2+
data "aws_partition" "current" {}
3+
4+
data "aws_iam_policy_document" "sns_policy" {
5+
count = var.enable_sns_notifications ? 1 : 0
6+
7+
statement {
8+
sid = "AllowSNSPublish"
9+
10+
actions = [
11+
"SNS:Publish",
12+
]
13+
14+
effect = "Allow"
15+
16+
principals {
17+
type = "Service"
18+
identifiers = [
19+
"backup.amazonaws.com"
20+
]
21+
}
22+
23+
resources = var.create_sns_topic ? [aws_sns_topic.main[0].arn] : [var.sns_topic_arn]
24+
}
25+
}
26+
27+
data "aws_iam_policy_document" "main" {
28+
statement {
29+
sid = "AllowBackupService"
30+
31+
actions = [
32+
"sts:AssumeRole",
33+
]
34+
35+
effect = "Allow"
36+
37+
principals {
38+
type = "Service"
39+
identifiers = [
40+
"backup.amazonaws.com"
41+
]
42+
}
43+
}
44+
}
45+
46+
data "aws_iam_policy_document" "main_custom_policy" {
47+
statement {
48+
sid = "AllowTaggingResources"
49+
50+
actions = [
51+
"backup:TagResource",
52+
"backup:ListTags",
53+
"backup:UntagResource",
54+
"tag:GetResources"
55+
]
56+
57+
resources = ["*"]
58+
}
59+
}

examples/multiple-dbs/main.tf

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ data "aws_kms_key" "rds" {
3232
key_id = "alias/aws/rds"
3333
}
3434

35+
data "aws_kms_key" "sns_backup" {
36+
key_id = "alias/aws/sns"
37+
}
38+
3539
#############
3640
# RDS Aurora
3741
#############
@@ -120,16 +124,11 @@ module "backup" {
120124
module.aurora-postgresql.rds_cluster_arn
121125
]
122126

123-
selection_tags = [
124-
{
125-
type = "STRINGEQUALS"
126-
key = "Project"
127-
value = "Test"
128-
},
129-
{
130-
type = "STRINGEQUALS"
131-
key = "Environment"
132-
value = "test"
133-
}
127+
# Enable SNS Backup Notifications
128+
enable_sns_notifications = true
129+
vault_sns_kms_key_arn = data.aws_kms_key.sns_backup.arn
130+
backup_vault_events = [
131+
"BACKUP_JOB_FAILED",
132+
"BACKUP_JOB_EXPIRED",
134133
]
135134
}

examples/multiple-dbs/outputs.tf

+10
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ output "backup_plan_version" {
3131
output "backup_selection_id" {
3232
description = "The identifier of the backup selection"
3333
value = module.backup.backup_selection_id
34+
}
35+
36+
output "backup_sns_topic_arn" {
37+
description = "The Amazon Resource Name (ARN) that specifies the topic for a backup vault's events"
38+
value = module.backup.backup_sns_topic_arn
39+
}
40+
41+
output "backup_vault_events" {
42+
description = "An array of events that indicate the status of the jobs to back up resources to the backup vault."
43+
value = module.backup.backup_vault_events
3444
}

examples/one-db/main.tf

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ provider "aws" {
22
region = "eu-west-1"
33
}
44

5-
data "aws_caller_identity" "current" {}
6-
data "aws_region" "current" {}
7-
85
#####
96
# VPC and subnets
107
#####
@@ -91,16 +88,6 @@ module "backup" {
9188
selection_name = "${var.name_prefix}-backup-selection"
9289
selection_resources = [module.aurora.rds_cluster_arn]
9390

94-
selection_tags = [
95-
{
96-
type = "STRINGEQUALS"
97-
key = "Project"
98-
value = "Test"
99-
},
100-
{
101-
type = "STRINGEQUALS"
102-
key = "Environment"
103-
value = "test"
104-
}
105-
]
91+
# Enable SNS Backup Notifications
92+
enable_sns_notifications = true
10693
}

examples/one-db/outputs.tf

+10
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ output "backup_selection_id" {
3232
description = "The identifier of the backup selection"
3333
value = module.backup.backup_selection_id
3434
}
35+
36+
output "backup_sns_topic_arn" {
37+
description = "The Amazon Resource Name (ARN) that specifies the topic for a backup vault's events"
38+
value = module.backup.backup_sns_topic_arn
39+
}
40+
41+
output "backup_vault_events" {
42+
description = "An array of events that indicate the status of the jobs to back up resources to the backup vault."
43+
value = module.backup.backup_vault_events
44+
}

examples/vault/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ output "backup_plan_version" {
3131
output "backup_selection_id" {
3232
description = "The identifier of the backup selection"
3333
value = module.backup.backup_selection_id
34-
}
34+
}

0 commit comments

Comments
 (0)