Skip to content

Commit 3e4a48f

Browse files
author
Abdul Wahid
authored
Module testing (#5)
* Introduce Terratest 1. Refactor KMS module to use `data` block for policy 2. Refactor VPC and subnets to use default vpc 3. Variablise naming of each resource 4. Add initial set of tests for Vault * Use default KMS keys * Documentation updates * Test Vault creation only * Update pre-commit-config * Module fix * Add new example and update test * golint * Adjustments to README * Use 'Equal' instead of 'Contains'
1 parent b180323 commit 3e4a48f

17 files changed

+908
-303
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.44.0
21+
rev: v1.45.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

CHANGELOG.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
<a name="unreleased"></a>
6+
## [Unreleased]
7+
8+
- Documentation updates
9+
- Use default KMS keys
10+
- Introduce Terratest
11+
12+
13+
<a name="1.1.0"></a>
14+
## [1.1.0] - 2020-11-18
15+
16+
- Allow for multiple selection conditions for a backup plan ([#4](https://github.com/umotif-public/terraform-aws-backup/issues/4))
17+
- Update example in README ([#3](https://github.com/umotif-public/terraform-aws-backup/issues/3))
18+
19+
20+
<a name="1.0.1"></a>
21+
## [1.0.1] - 2020-11-13
22+
23+
- target_vault_name reference a resource not a variable ([#2](https://github.com/umotif-public/terraform-aws-backup/issues/2))
24+
25+
526
<a name="1.0.0"></a>
627
## 1.0.0 - 2020-11-09
728

829
- Complete AWS Backup module ([#1](https://github.com/umotif-public/terraform-aws-backup/issues/1))
930
- Initial commit
1031

1132

12-
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.0.0...HEAD
33+
[Unreleased]: https://github.com/umotif-public/terraform-aws-backup/compare/1.1.0...HEAD
34+
[1.1.0]: https://github.com/umotif-public/terraform-aws-backup/compare/1.0.1...1.1.0
35+
[1.0.1]: https://github.com/umotif-public/terraform-aws-backup/compare/1.0.0...1.0.1

README.md

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# terraform-aws-backup
2-
Terraform module to provision AWS Backup resources
1+
# Terraform AWS Backup
2+
3+
Terraform module to provision [AWS Backup](https://aws.amazon.com/backup/) resources.
34

45
## Terraform versions
56

@@ -10,7 +11,7 @@ Terraform 0.13. Pin module version to `~> v1.0`. Submit pull-requests to `master
1011
```hcl
1112
module "backup" {
1213
source = "umotif-public/backup/aws"
13-
version = "~> 1.0.0"
14+
version = "~> 1.0"
1415
1516
vault_name = "test-rds-aurora"
1617
vault_kms_key_arn = "arn:aws:kms:eu-west-1:1111111111:key/07a8a813-fcc9-4d7f-a982648d9c25"
@@ -40,7 +41,7 @@ module "backup" {
4041
]
4142
4243
selection_name = "test-backup-selection"
43-
selection_resources = ["arn:aws:rds:eu-west-1:1111111111:cluster:example-dataabase-1"]
44+
selection_resources = ["arn:aws:rds:eu-west-1:1111111111:cluster:example-database-1"]
4445
4546
selection_tags = [
4647
{
@@ -66,12 +67,14 @@ Module is to be used with Terraform > 0.13.
6667
* [Backup with Aurora MySQL](https://github.com/umotif-public/terraform-aws-backup/tree/master/examples/one-db)
6768
* [Backup with Aurora MySQL and Aurora PostgreSQL](https://github.com/umotif-public/terraform-aws-backup/tree/master/examples/multiple-dbs)
6869
* [Backup with an externally created Vault](https://github.com/umotif-public/terraform-aws-backup/tree/master/examples/external-vault)
70+
* [Backup with Vault only](https://github.com/umotif-public/terraform-aws-backup/tree/master/examples/vault)
6971

7072
## Authors
7173

7274
Module managed by:
73-
* [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](https://www.linkedin.com/in/marcincuber/)
74-
* [Abdul Wahid](https://github.com/Ohid25) [LinkedIn](https://www.linkedin.com/in/abdul-wahid/)
75+
76+
* [Marcin Cuber](https://github.com/marcincuber) ([LinkedIn](https://www.linkedin.com/in/marcincuber/))
77+
* [Abdul Wahid](https://github.com/Ohid25) ([LinkedIn](https://www.linkedin.com/in/abdul-wahid/))
7578

7679
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7780
## Requirements
@@ -96,7 +99,7 @@ Module managed by:
9699
| plan\_name | The display name of a backup plan | `string` | n/a | yes |
97100
| rule\_completion\_window | The amount of time AWS Backup attempts a backup before canceling the job and returning an error | `number` | `null` | no |
98101
| 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 |
99-
| rule\_copy\_action\_lifecycle | The lifecycle defines when a protected resource is copied over to a backup vault and when it expires. | `map` | `{}` | no |
102+
| 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 |
100103
| rule\_lifecycle\_cold\_storage\_after | Specifies the number of days after creation that a recovery point is moved to cold storage | `number` | `null` | no |
101104
| 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 |
102105
| rule\_name | An display name for a backup rule | `string` | `null` | no |
@@ -109,7 +112,7 @@ Module managed by:
109112
| selection\_tag\_key | The key in a key-value pair | `string` | `null` | no |
110113
| 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 |
111114
| selection\_tag\_value | The value in a key-value pair | `string` | `null` | no |
112-
| selection\_tags | A list of selection tags map | `list` | `[]` | no |
115+
| selection\_tags | A list of selection tags map | `list(any)` | `[]` | no |
113116
| tags | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
114117
| vault\_kms\_key\_arn | The server-side encryption key that is used to protect your backups | `string` | `null` | no |
115118
| vault\_name | Name of the backup vault to create. If not given, AWS use default | `string` | `null` | no |
@@ -132,13 +135,28 @@ Module managed by:
132135

133136
See LICENSE for full details.
134137

135-
## Pre-commit hooks
138+
## Pre-commit hooks & Golang for Terratest
136139

137140
### Install dependencies
138141

139142
* [`pre-commit`](https://pre-commit.com/#install)
140143
* [`terraform-docs`](https://github.com/segmentio/terraform-docs) required for `terraform_docs` hooks.
141144
* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook.
145+
* [`golang`](https://formulae.brew.sh/formula/go) required for running tests.
146+
147+
#### Terratest
148+
149+
We are using [Terratest](https://terratest.gruntwork.io/) to run tests on this module.
150+
151+
```bash
152+
brew install go
153+
# Change to test directory
154+
cd test
155+
# Get dependencies
156+
go mod download
157+
# Run tests
158+
go test -v -timeout 30m
159+
```
142160

143161
#### MacOS
144162

examples/external-vault/main.tf

+8-51
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,16 @@ data "aws_region" "current" {}
88
######
99
# KMS
1010
######
11-
module "kms-backup" {
12-
source = "umotif-public/kms/aws"
13-
version = "~> 1.0"
14-
15-
alias_name = "backup-kms-test-key"
16-
deletion_window_in_days = 7
17-
enable_key_rotation = true
18-
policy = jsonencode(
19-
{
20-
"Version" : "2012-10-17",
21-
"Statement" : [
22-
{
23-
"Sid" : "Enable IAM User Permissions",
24-
"Effect" : "Allow",
25-
"Principal" : {
26-
"AWS" : [
27-
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
28-
data.aws_caller_identity.current.arn
29-
]
30-
},
31-
"Action" : "kms:*",
32-
"Resource" : "*"
33-
},
34-
{
35-
"Sid" : "Allow use of the key",
36-
"Effect" : "Allow",
37-
"Principal" : {
38-
"Service" : ["backup.amazonaws.com"]
39-
},
40-
"Action" : [
41-
"kms:Encrypt",
42-
"kms:Decrypt",
43-
"kms:ReEncrypt*",
44-
"kms:GenerateDataKey*",
45-
"kms:DescribeKey"
46-
],
47-
"Resource" : "*"
48-
}
49-
]
50-
}
51-
)
52-
53-
tags = {
54-
Environment = "test"
55-
}
11+
data "aws_kms_key" "backup" {
12+
key_id = "alias/aws/backup"
5613
}
5714

5815
#########
5916
# Backup
6017
#########
6118
resource "aws_backup_vault" "external_vault" {
62-
name = "external-test"
63-
kms_key_arn = module.kms-backup.key_arn
19+
name = "${var.name_prefix}-external"
20+
kms_key_arn = data.aws_kms_key.backup.arn
6421
tags = {
6522
Enviroment = "test"
6623
Vault = "external"
@@ -71,11 +28,11 @@ module "backup" {
7128
source = "../.."
7229

7330
# Create a backup plan
74-
plan_name = "test-backup-plan"
31+
plan_name = "${var.name_prefix}-backup-plan"
7532

7633
rules = [
7734
{
78-
name = "test-backup-rule"
35+
name = "${var.name_prefix}-backup-rule"
7936
target_vault_name = aws_backup_vault.external_vault.name
8037
schedule = "cron(0 12 * * ? *)"
8138
start_window = "65"
@@ -87,12 +44,12 @@ module "backup" {
8744

8845
lifecycle = {
8946
cold_storage_after = 0
90-
delete_after = 90
47+
delete_after = 95
9148
}
9249
}
9350
]
9451

95-
selection_name = "test-backup-selection"
52+
selection_name = "${var.name_prefix}-backup-selection"
9653

9754
selection_tags = [
9855
{

examples/external-vault/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+
}

0 commit comments

Comments
 (0)