Skip to content

Commit 565470f

Browse files
committed
Adjusting protection groups, and adding test
1 parent be0aecb commit 565470f

File tree

4 files changed

+101
-41
lines changed

4 files changed

+101
-41
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ No modules.
4343

4444
| Name | Description | Type | Default | Required |
4545
|------|-------------|------|---------|:--------:|
46-
| <a name="input_aggregation"></a> [aggregation](#input\_aggregation) | Defines how AWS Shield combines resource data for the group in order to detect, mitigate, and report events. | `string` | n/a | yes |
4746
| <a name="input_name"></a> [name](#input\_name) | A friendly name for the Protection you are creating. | `string` | n/a | yes |
48-
| <a name="input_pattern"></a> [pattern](#input\_pattern) | The criteria to use to choose the protected resources for inclusion in the group. | `string` | n/a | yes |
49-
| <a name="input_protection_group_id"></a> [protection\_group\_id](#input\_protection\_group\_id) | The name of the protection group. | `string` | n/a | yes |
47+
| <a name="input_protection_group_config"></a> [protection\_group\_config](#input\_protection\_group\_config) | `id` - The name of the protection group, or protection\_group\_id<br> `aggregation` - Defines how AWS Shield combines resource data for the group in order to detect, mitigate, and report events.<br> `pattern` - The criteria to use to choose the protected resources for inclusion in the group.<br> `resource_type` - (Optional) The resource type to include in the protection group. You must set this only when you set pattern to `BY_RESOURCE_TYPE`. | <pre>list(object({<br> id = string<br> aggregation = string<br> pattern = string<br> resource_type = optional(string)<br> }))</pre> | n/a | yes |
5048
| <a name="input_resource_arn"></a> [resource\_arn](#input\_resource\_arn) | The ARN (Amazon Resource Name) of the resource to be protected. | `string` | n/a | yes |
5149
| <a name="input_health_check_configuration"></a> [health\_check\_configuration](#input\_health\_check\_configuration) | Amazon Route53 Health Check Configuration to be associated to AWS Shield Advanced Protection. | `map(any)` | `null` | no |
52-
| <a name="input_resource_type"></a> [resource\_type](#input\_resource\_type) | The resource type to include in the protection group. This is required if `pattern` is set to BY\_RESOURCE\_TYPE. Otherwise this must be not set. Defaults to `null` | `string` | `null` | no |
53-
| <a name="input_tags"></a> [tags](#input\_tags) | Key-value map of resource tags. Defaults to `{}` | `map(string)` | `{}` | no |
50+
| <a name="input_tags"></a> [tags](#input\_tags) | Key-value map of resource tags to apply to all taggable resources created by the module. If configured with a provider `default_tags` configuration block present, tags with matching keys will overwrite those defined at the provider-level. Defaults to `{}`. | `map(string)` | `{}` | no |
5451

5552
### Outputs
5653

main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ resource "aws_shield_protection" "this" {
1717
}
1818

1919
resource "aws_shield_protection_group" "this" {
20-
protection_group_id = var.protection_group_id
21-
aggregation = var.aggregation
22-
pattern = var.pattern
23-
members = [var.resource_arn]
24-
resource_type = var.resource_type
20+
for_each = var.protection_group_config != null ? { for config in var.protection_group_config : config.id => config } : {}
21+
22+
protection_group_id = each.value.id
23+
aggregation = each.value.aggregation
24+
pattern = each.value.pattern
25+
resource_type = each.value.resource_type
26+
members = try([var.resource_arn], [])
2527
tags = merge(
2628
local.tags,
2729
var.tags

test/complete/main.tf

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,62 @@ resource "aws_eip" "example" {
1111
domain = "vpc"
1212
}
1313

14+
15+
1416
module "shield_advanced" {
1517
source = "../.."
1618

17-
name = "Example protection"
18-
resource_arn = "${local.arn_prefix}/${aws_eip.example.id}"
19-
protection_group_id = "example"
20-
aggregation = "MEAN"
21-
pattern = "ARBITRARY"
19+
name = "Example protection"
20+
resource_arn = "${local.arn_prefix}/${aws_eip.example.id}"
21+
22+
protection_group_config = [
23+
{
24+
id = "Arbitrary Resource"
25+
aggregation = "MEAN"
26+
pattern = "ARBITRARY"
27+
members = "${local.arn_prefix}/${aws_eip.example.id}"
28+
},
29+
{
30+
id = "All Resources"
31+
aggregation = "MEAN"
32+
pattern = "ALL"
33+
},
34+
{
35+
id = "CloudFront Resource"
36+
aggregation = "SUM"
37+
pattern = "BY_RESOURCE_TYPE"
38+
resource_type = "CLOUDFRONT_DISTRIBUTION"
39+
},
40+
{
41+
id = "Route53 Resource"
42+
aggregation = "MAX"
43+
pattern = "BY_RESOURCE_TYPE"
44+
resource_type = "ROUTE_53_HOSTED_ZONE"
45+
},
46+
{
47+
id = "GlobalAccelerator Resource"
48+
aggregation = "SUM"
49+
pattern = "BY_RESOURCE_TYPE"
50+
resource_type = "GLOBAL_ACCELERATOR"
51+
},
52+
{
53+
id = "ALB Resource"
54+
aggregation = "MEAN"
55+
pattern = "BY_RESOURCE_TYPE"
56+
resource_type = "APPLICATION_LOAD_BALANCER"
57+
},
58+
{
59+
id = "CLB Resource"
60+
aggregation = "MEAN"
61+
pattern = "BY_RESOURCE_TYPE"
62+
resource_type = "CLASSIC_LOAD_BALANCER"
63+
},
64+
{
65+
id = "ElasticIP Resource"
66+
aggregation = "SUM"
67+
pattern = "BY_RESOURCE_TYPE"
68+
resource_type = "ELASTIC_IP_ALLOCATION"
69+
},
70+
]
71+
2272
}

variables.tf

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,54 @@ variable "resource_arn" {
88
type = string
99
}
1010

11-
variable "protection_group_id" {
12-
description = "The name of the protection group."
13-
type = string
14-
}
15-
16-
variable "aggregation" {
17-
description = "Defines how AWS Shield combines resource data for the group in order to detect, mitigate, and report events."
18-
type = string
11+
variable "protection_group_config" {
12+
description = <<EOF
13+
`id` - The name of the protection group, or protection_group_id
14+
`aggregation` - Defines how AWS Shield combines resource data for the group in order to detect, mitigate, and report events.
15+
`pattern` - The criteria to use to choose the protected resources for inclusion in the group.
16+
`resource_type` - (Optional) The resource type to include in the protection group. You must set this only when you set pattern to `BY_RESOURCE_TYPE`.
17+
EOF
18+
type = list(object({
19+
id = string
20+
aggregation = string
21+
pattern = string
22+
resource_type = optional(string)
23+
}))
1924
validation {
20-
condition = contains([
21-
"SUM",
22-
"MEAN",
23-
"MAX",
24-
], var.aggregation)
25-
error_message = "Valid values for `var.aggregation` are `SUM | MEAN | MAX`."
25+
condition = alltrue([
26+
for config in var.protection_group_config : contains([
27+
"SUM",
28+
"MEAN",
29+
"MAX",
30+
], config.aggregation) && contains([
31+
"ARBITRARY",
32+
"ALL",
33+
"BY_RESOURCE_TYPE",
34+
], config.pattern) && contains([
35+
"APPLICATION_LOAD_BALANCER",
36+
"CLASSIC_LOAD_BALANCER",
37+
"CLOUDFRONT_DISTRIBUTION",
38+
"ELASTIC_IP_ALLOCATION",
39+
"GLOBAL_ACCELERATOR",
40+
"ROUTE_53_HOSTED_ZONE",
41+
], config.resource_type) if config.resource_type != null
42+
])
43+
error_message = <<EOF
44+
Valid values for `aggregation` are `SUM | MEAN | MAX`.
45+
Valid values for `pattern` are `ARBITRARY | ALL | BY_RESOURCE_TYPE`. You must declare `members` parameter when using the `ARBITRARY` pattern, using a list with the content of the `var.resource_arn`.
46+
Valid values for `resource_type` are `APPLICATION_LOAD_BALANCER | CLASSIC_LOAD_BALANCER | CLOUDFRONT_DISTRIBUTION | ELASTIC_IP_ALLOCATION | GLOBAL_ACCELERATOR | ROUTE_53_HOSTED_ZONE`.
47+
EOF
2648
}
2749
}
2850

29-
variable "pattern" {
30-
description = "The criteria to use to choose the protected resources for inclusion in the group."
31-
type = string
32-
}
33-
34-
variable "resource_type" {
35-
description = "The resource type to include in the protection group. This is required if `pattern` is set to BY_RESOURCE_TYPE. Otherwise this must be not set. Defaults to `null`"
36-
type = string
37-
default = null
38-
}
39-
4051
variable "health_check_configuration" {
4152
description = "Amazon Route53 Health Check Configuration to be associated to AWS Shield Advanced Protection."
4253
type = map(any)
4354
default = null
4455
}
4556

4657
variable "tags" {
47-
description = "Key-value map of resource tags. Defaults to `{}`"
58+
description = "Key-value map of resource tags to apply to all taggable resources created by the module. If configured with a provider `default_tags` configuration block present, tags with matching keys will overwrite those defined at the provider-level. Defaults to `{}`."
4859
type = map(string)
4960
default = {}
5061
}

0 commit comments

Comments
 (0)