Skip to content

Commit 92a91c8

Browse files
Added force destroy feature + new restore policy (#13)
* Updated Documentation + terratest version * Refactored examples to be 4.0 compatible * New feature to allow force destroy for backup vault * Added restore policy * Update documentation + upgrade go modules * Update github actions workflow
1 parent a02a7c3 commit 92a91c8

File tree

12 files changed

+1144
-214
lines changed

12 files changed

+1144
-214
lines changed

.github/workflows/terraform-checks.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
name: Lint and Validate
22

33
on:
4-
pull_request:
5-
branches:
6-
- main
4+
pull_request_review:
5+
types: [submitted, edited]
76
workflow_dispatch:
87

98
env:
10-
TF_VERSION: 0.13.0
9+
TF_VERSION: 1.1.7
1110

1211
jobs:
1312
fmt:

.github/workflows/test.yaml

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
name: Terratest
22

33
on:
4-
pull_request:
5-
branches:
6-
- main
4+
pull_request_review:
5+
types: [submitted, edited]
76
workflow_dispatch:
87

98
env:
10-
TF_VERSION: 0.13.0
11-
GO_VERSION: 1.16.5
9+
TF_VERSION: 1.1.7
10+
GO_VERSION: 1.19
1211

1312
jobs:
1413
terratest:
@@ -28,20 +27,12 @@ jobs:
2827
- name: Install dependencies
2928
run: |
3029
go mod download
31-
go get -u golang.org/x/lint/golint
3230
go get github.com/gruntwork-io/terratest/modules/[email protected]
3331
working-directory: ${{ env.working-directory }}
34-
- name: Run checks
35-
run: |
36-
go vet .
37-
golint .
38-
working-directory: ${{ env.working-directory }}
3932

4033
- name: Configure AWS credentials
4134
uses: aws-actions/configure-aws-credentials@v1
4235
with:
43-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
44-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4536
aws-region: eu-west-1
4637
role-skip-session-tagging: true
4738
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}

.pre-commit-config.yaml

+3-3
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.0.1
3+
rev: v4.3.0
44
hooks:
55
- id: check-added-large-files
66
args: ['--maxkb=500']
@@ -17,8 +17,8 @@ repos:
1717
- id: detect-aws-credentials
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
20-
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.50.0
20+
- repo: https://github.com/antonbabenko/pre-commit-terraform
21+
rev: v1.74.1
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

CHANGELOG.md

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

8-
- Migrate to main branch + manual trigger to pipeline
8+
9+
10+
<a name="1.4.0"></a>
11+
## [1.4.0] - 2021-06-28
12+
13+
- Migrate to main branch + manual trigger to pipeline ([#12](https://github.com/umotif-public/terraform-aws-backup/issues/12))
914

1015

1116
<a name="1.3.1"></a>
@@ -51,7 +56,8 @@ All notable changes to this project will be documented in this file.
5156
- Initial commit
5257

5358

54-
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.3.1...HEAD
59+
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.4.0...HEAD
60+
[1.4.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.3.1...1.4.0
5561
[1.3.1]: https://github.com/umotif-public/terraform-aws-backup/compare/1.3.0...1.3.1
5662
[1.3.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.2.0...1.3.0
5763
[1.2.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.1.0...1.2.0

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Terraform module to provision [AWS Backup](https://aws.amazon.com/backup/) resou
88

99
## Terraform versions
1010

11-
Terraform 0.13+. Pin module version to `~> v1.4`. Submit pull-requests to `main` branch. Prior versions on `master` branch will need `~> v1.3`.
11+
Terraform 1.0+. Pin module version to `~> v1.5`. Submit pull-requests to `main` branch. Prior versions on `master` branch will need `~> v1.3`.
1212

1313
## Usage
1414

@@ -17,7 +17,7 @@ Terraform 0.13+. Pin module version to `~> v1.4`. Submit pull-requests to `main`
1717
```hcl
1818
module "backup" {
1919
source = "umotif-public/backup/aws"
20-
version = "~> 1.4"
20+
version = "~> 1.5"
2121
2222
vault_name = "test-rds-aurora"
2323
vault_kms_key_arn = "arn:aws:kms:eu-west-1:1111111111:key/07a8a813-fcc9-4d7f-a982648d9c25"
@@ -66,7 +66,7 @@ module "backup" {
6666

6767
## Assumptions
6868

69-
Module is to be used with Terraform > 0.13.
69+
Module is to be used with Terraform > 1.0.
7070

7171
## Examples
7272

@@ -79,22 +79,21 @@ Module is to be used with Terraform > 0.13.
7979

8080
Module managed by:
8181

82-
* [Marcin Cuber](https://github.com/marcincuber) ([LinkedIn](https://www.linkedin.com/in/marcincuber/))
83-
* [Abdul Wahid](https://github.com/Ohid25) ([LinkedIn](https://www.linkedin.com/in/abdul-wahid/))
82+
* Module managed by [uMotif](https://github.com/umotif-public/).
8483

8584
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
8685
## Requirements
8786

8887
| Name | Version |
8988
|------|---------|
90-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
91-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.11 |
89+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.11 |
90+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
9291

9392
## Providers
9493

9594
| Name | Version |
9695
|------|---------|
97-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.11 |
96+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 |
9897

9998
## Modules
10099

@@ -111,7 +110,8 @@ No modules.
111110
| [aws_iam_policy.main_custom_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
112111
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
113112
| [aws_iam_role_policy_attachment.main_custom_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
114-
| [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_iam_role_policy_attachment.main_role_backup_policy_attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
114+
| [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 |
115115
| [aws_sns_topic.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
116116
| [aws_sns_topic_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy) | resource |
117117
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -148,6 +148,7 @@ No modules.
148148
| <a name="input_selection_tags"></a> [selection\_tags](#input\_selection\_tags) | A list of selection tags map | `list(any)` | `[]` | no |
149149
| <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 |
150150
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
151+
| <a name="input_vault_force_destroy"></a> [vault\_force\_destroy](#input\_vault\_force\_destroy) | A boolean that indicates that all recovery points stored in the vault are deleted so that the vault can be destroyed without error. | `bool` | `false` | no |
151152
| <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 |
152153
| <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 |
153154
| <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 |

examples/multiple-dbs/main.tf

+15-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ data "aws_vpc" "default" {
1212
default = true
1313
}
1414

15-
data "aws_subnet_ids" "all" {
16-
vpc_id = data.aws_vpc.default.id
15+
data "aws_subnets" "all" {
16+
filter {
17+
name = "vpc-id"
18+
values = [data.aws_vpc.default.id]
19+
}
1720
}
1821

1922
data "aws_subnet" "public" {
20-
for_each = data.aws_subnet_ids.all.ids
23+
for_each = toset(data.aws_subnets.all.ids)
2124
id = each.value
2225
}
2326

@@ -40,14 +43,15 @@ data "aws_kms_key" "sns_backup" {
4043
# RDS Aurora
4144
#############
4245
module "aurora-mysql" {
43-
source = "umotif-public/rds-aurora/aws"
46+
source = "umotif-public/rds-aurora/aws"
47+
version = "~> 3"
4448

4549
name_prefix = "${var.name_prefix}-aurora-mysql"
4650
database_name = "${var.name_prefix}mysqldb"
4751
engine = "aurora-mysql"
4852

4953
vpc_id = data.aws_vpc.default.id
50-
subnets = data.aws_subnet_ids.all.ids
54+
subnets = data.aws_subnets.all.ids
5155

5256
kms_key_id = data.aws_kms_key.rds.arn
5357

@@ -61,7 +65,8 @@ module "aurora-mysql" {
6165
}
6266

6367
module "aurora-postgresql" {
64-
source = "umotif-public/rds-aurora/aws"
68+
source = "umotif-public/rds-aurora/aws"
69+
version = "~> 3"
6570

6671
name_prefix = "${var.name_prefix}-postgresql"
6772
database_name = "${var.name_prefix}postgresqldb"
@@ -70,7 +75,7 @@ module "aurora-postgresql" {
7075
engine_parameter_family = "aurora-postgresql11"
7176

7277
vpc_id = data.aws_vpc.default.id
73-
subnets = data.aws_subnet_ids.all.ids
78+
subnets = data.aws_subnets.all.ids
7479

7580
kms_key_id = data.aws_kms_key.rds.arn
7681

@@ -90,8 +95,9 @@ module "backup" {
9095
source = "../.."
9196

9297
# Create a Vault
93-
vault_name = "${var.name_prefix}-rds-aurora"
94-
vault_kms_key_arn = data.aws_kms_key.backup.arn
98+
vault_name = "${var.name_prefix}-rds-aurora"
99+
vault_kms_key_arn = data.aws_kms_key.backup.arn
100+
vault_force_destroy = true
95101

96102
tags = {
97103
Environment = "test"

examples/one-db/main.tf

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ data "aws_vpc" "default" {
99
default = true
1010
}
1111

12-
data "aws_subnet_ids" "all" {
13-
vpc_id = data.aws_vpc.default.id
12+
data "aws_subnets" "all" {
13+
filter {
14+
name = "vpc-id"
15+
values = [data.aws_vpc.default.id]
16+
}
1417
}
1518

1619
data "aws_subnet" "public" {
17-
for_each = data.aws_subnet_ids.all.ids
20+
for_each = toset(data.aws_subnets.all.ids)
1821
id = each.value
1922
}
2023

@@ -33,14 +36,15 @@ data "aws_kms_key" "rds" {
3336
# RDS Aurora
3437
#############
3538
module "aurora" {
36-
source = "umotif-public/rds-aurora/aws"
39+
source = "umotif-public/rds-aurora/aws"
40+
version = "~> 3"
3741

3842
name_prefix = "${var.name_prefix}-aurora-mysql"
3943
database_name = "${var.name_prefix}mysqldb"
4044
engine = "aurora-mysql"
4145

4246
vpc_id = data.aws_vpc.default.id
43-
subnets = data.aws_subnet_ids.all.ids
47+
subnets = data.aws_subnets.all.ids
4448

4549
kms_key_id = data.aws_kms_key.rds.arn
4650

@@ -60,9 +64,9 @@ module "backup" {
6064
source = "../.."
6165

6266
# Create a Vault
63-
vault_name = "${var.name_prefix}-rds-aurora"
64-
vault_kms_key_arn = data.aws_kms_key.backup.arn
65-
67+
vault_name = "${var.name_prefix}-rds-aurora"
68+
vault_kms_key_arn = data.aws_kms_key.backup.arn
69+
vault_force_destroy = true
6670
tags = {
6771
Environment = "test"
6872
}
@@ -78,10 +82,12 @@ module "backup" {
7882
Project = "test"
7983
Region = "eu-west-1"
8084
}
85+
schedule = "cron(0 2 ? * MON-FRI *)"
8186

8287
lifecycle = {
8388
delete_after = 30
8489
}
90+
8591
}
8692
]
8793

main.tf

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
resource "aws_backup_vault" "main" {
55
count = var.vault_name != null ? 1 : 0
66

7-
name = var.vault_name
8-
kms_key_arn = var.vault_kms_key_arn
9-
tags = var.tags
7+
name = var.vault_name
8+
kms_key_arn = var.vault_kms_key_arn
9+
force_destroy = var.vault_force_destroy
10+
tags = var.tags
1011
}
1112

1213
#####
@@ -117,18 +118,24 @@ resource "aws_iam_role" "main" {
117118
tags = var.tags
118119
}
119120

120-
resource "aws_iam_role_policy_attachment" "main_role_policy_attach" {
121+
resource "aws_iam_role_policy_attachment" "main_role_backup_policy_attach" {
121122
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
122123
role = aws_iam_role.main.name
123124
}
124125

126+
resource "aws_iam_role_policy_attachment" "main_role_restore_policy_attach" {
127+
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForRestores"
128+
role = aws_iam_role.main.name
129+
}
130+
125131
resource "aws_iam_policy" "main_custom_policy" {
126132
description = "AWS Backup Tag policy"
133+
policy = data.aws_iam_policy_document.main_custom_policy.json
127134

128-
policy = data.aws_iam_policy_document.main_custom_policy.json
135+
tags = var.tags
129136
}
130137

131138
resource "aws_iam_role_policy_attachment" "main_custom_policy_attach" {
132139
policy_arn = aws_iam_policy.main_custom_policy.arn
133140
role = aws_iam_role.main.name
134-
}
141+
}

test/go.mod

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@ module github.com/umotif-public/terraform-aws-backup/test
33
go 1.15
44

55
require (
6-
github.com/gruntwork-io/terratest v0.31.0
7-
github.com/jinzhu/copier v0.1.0 // indirect
8-
github.com/kr/text v0.2.0 // indirect
9-
github.com/stretchr/testify v1.6.1
10-
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c // indirect
11-
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
12-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
13-
golang.org/x/tools v0.1.4 // indirect
6+
cloud.google.com/go v0.103.0 // indirect
7+
cloud.google.com/go/compute v1.8.0 // indirect
8+
cloud.google.com/go/storage v1.25.0 // indirect
9+
github.com/aws/aws-sdk-go v1.44.79 // indirect
10+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
11+
github.com/golang/snappy v0.0.4 // indirect
12+
github.com/googleapis/gax-go/v2 v2.5.1 // indirect
13+
github.com/gruntwork-io/terratest v0.40.20
14+
github.com/hashicorp/errwrap v1.1.0 // indirect
15+
github.com/hashicorp/go-getter v1.6.2 // indirect
16+
github.com/hashicorp/go-multierror v1.1.1 // indirect
17+
github.com/hashicorp/go-version v1.6.0 // indirect
18+
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
19+
github.com/hashicorp/terraform-json v0.14.0 // indirect
20+
github.com/jinzhu/copier v0.3.5 // indirect
21+
github.com/klauspost/compress v1.15.9 // indirect
22+
github.com/mattn/go-zglob v0.0.3 // indirect
23+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
24+
github.com/stretchr/testify v1.8.0
25+
github.com/tmccombs/hcl2json v0.3.4 // indirect
26+
github.com/ulikunitz/xz v0.5.10 // indirect
27+
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
28+
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
29+
golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7 // indirect
30+
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect
31+
google.golang.org/api v0.93.0 // indirect
32+
google.golang.org/genproto v0.0.0-20220817144833-d7fd3f11b9b1 // indirect
1433
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
15-
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
1634
)

0 commit comments

Comments
 (0)