Skip to content

Commit dba04fc

Browse files
committed
Upgrade to new Security Group standards (use security_group_inputs.tf export).
1 parent cd3cddf commit dba04fc

File tree

7 files changed

+131
-23
lines changed

7 files changed

+131
-23
lines changed

README.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ usage: |-
9090
kafka_version = "2.4.1"
9191
number_of_broker_nodes = 2 # this has to be a multiple of the # of subnet_ids
9292
broker_instance_type = "kafka.m5.large"
93-
93+
9494
# security groups to put on the cluster itself
95-
broker_node_security_groups = ["sg-XXXXXXXXX", "sg-YYYYYYYY"]
95+
associated_security_group_ids = ["sg-XXXXXXXXX", "sg-YYYYYYYY"]
9696
# security groups to give access to the cluster
97-
security_groups = ["sg-XXXXXXXXX", "sg-YYYYYYYY"]
97+
associated_security_group_ids = ["sg-XXXXXXXXX", "sg-YYYYYYYY"]
9898
}
9999
```
100100

main.tf

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,18 @@ module "broker_security_group" {
6666

6767
attributes = ["broker"]
6868

69-
security_group_description = "Allow inbound MSK-related traffic from Security Groups and CIDRs. Allow all outbound traffic"
69+
enabled = local.enabled && var.create_security_group
70+
security_group_name = var.security_group_name
71+
create_before_destroy = var.security_group_create_before_destroy
72+
security_group_create_timeout = var.security_group_create_timeout
73+
security_group_delete_timeout = var.security_group_delete_timeout
74+
75+
security_group_description = coalesce(var.security_group_description, "Allow inbound MSK-related traffic from Security Groups and CIDRs. Allow all outbound traffic")
7076
allow_all_egress = true
77+
rules = var.additional_security_group_rules
7178
rule_matrix = [
7279
{
73-
source_security_group_ids = var.security_groups
80+
source_security_group_ids = local.allowed_security_group_ids
7481
cidr_blocks = var.allowed_cidr_blocks
7582
rules = [
7683
for protocol_key, protocol in local.protocols : {
@@ -110,7 +117,7 @@ resource "aws_msk_cluster" "default" {
110117
instance_type = var.broker_instance_type
111118
ebs_volume_size = var.broker_volume_size
112119
client_subnets = var.subnet_ids
113-
security_groups = concat(var.broker_node_security_groups, [module.broker_security_group.id])
120+
security_groups = concat(var.associated_security_group_ids, [module.broker_security_group.id])
114121
}
115122

116123
configuration_info {

security_group_inputs.tf

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# security_group_inputs Version: 1
2+
#
3+
# Copy this file from https://github.com/cloudposse/terraform-aws-security-group/blob/master/exports/security_group_inputs.tf
4+
# and EDIT IT TO SUIT YOUR PROJECT. Update the version number above if you update this file from a later version.
5+
#
6+
# KEEP this top comment block, but REMOVE COMMENTS below that are intended
7+
# for the initial implementor and not maintainers or end users.
8+
#
9+
# This file provides the standard inputs that all Cloud Posse Open Source
10+
# Terraform module that create AWS Security Groups should implement.
11+
# This file does NOT provide implementation of the inputs, as that
12+
# of course varies with each module.
13+
#
14+
# This file documents, but does not declare, the standard outputs modules should create,
15+
# again because the implementation will vary with modules.
16+
#
17+
# Unlike null-label context.tf, this file cannot be automatically updated
18+
# because of the tight integration with the module using it.
19+
#
20+
21+
22+
variable "create_security_group" {
23+
type = bool
24+
default = true
25+
description = "Set `true` to create and configure a new security group. If false, `associated_security_group_ids` must be provided."
26+
}
27+
28+
variable "associated_security_group_ids" {
29+
type = list(string)
30+
default = []
31+
description = <<-EOT
32+
A list of IDs of Security Groups to associate the created resource with, in addition to the created security group.
33+
These security groups will not be modified and, if `create_security_group` is `false`, must have rules providing the desired access.
34+
EOT
35+
}
36+
37+
variable "allowed_security_group_ids" {
38+
type = list(string)
39+
default = []
40+
description = <<-EOT
41+
A list of IDs of Security Groups to allow access to the security group created by this module.
42+
EOT
43+
}
44+
45+
locals {
46+
allowed_security_group_ids = concat(var.security_groups, var.allowed_security_group_ids)
47+
}
48+
49+
variable "security_group_name" {
50+
type = list(string)
51+
default = []
52+
description = <<-EOT
53+
The name to assign to the created security group. Must be unique within the VPC.
54+
If not provided, will be derived from the `null-label.context` passed in.
55+
If `create_before_destroy` is true, will be used as a name prefix.
56+
EOT
57+
}
58+
59+
variable "security_group_description" {
60+
type = string
61+
default = null
62+
description = <<-EOT
63+
The description to assign to the created Security Group.
64+
Warning: Changing the description causes the security group to be replaced.
65+
EOT
66+
}
67+
68+
variable "security_group_create_before_destroy" {
69+
type = bool
70+
71+
default = false
72+
description = <<-EOT
73+
Set `true` to enable Terraform `create_before_destroy` behavior on the created security group.
74+
We recommend setting this `true` on new security groups, but default it to `false` because `true`
75+
will cause existing security groups to be replaced, possibly requiring the cluster to be deleted and recreated.
76+
Note that changing this value will always cause the security group to be replaced.
77+
EOT
78+
}
79+
80+
variable "security_group_create_timeout" {
81+
type = string
82+
default = "10m"
83+
description = "How long to wait for the security group to be created."
84+
}
85+
86+
variable "security_group_delete_timeout" {
87+
type = string
88+
default = "15m"
89+
description = <<-EOT
90+
How long to retry on `DependencyViolation` errors during security group deletion from
91+
lingering ENIs left by certain AWS services such as Elastic Load Balancing.
92+
EOT
93+
}
94+
95+
variable "additional_security_group_rules" {
96+
type = list(any)
97+
default = []
98+
description = <<-EOT
99+
A list of Security Group rule objects to add to the created security group, in addition to the ones
100+
this module normally creates. (To suppress the module's rules, set `create_security_group` to false
101+
and supply your own security group via `associated_security_group_ids`.)
102+
The keys and values of the objects are fully compatible with the `aws_security_group_rule` resource, except
103+
for `security_group_id` which will be ignored, and the optional "key" which, if provided, must be unique and known at "plan" time.
104+
To get more info see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule .
105+
EOT
106+
}

test/src/examples_complete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ func TestExamplesComplete(t *testing.T) {
3434
outputSecurityGroupName := terraform.Output(t, terraformOptions, "security_group_name")
3535

3636
// Verify we're getting back the outputs we expect
37-
assert.Regexp(t, "^eg-ue2-test-msk-test-[0-9a-fA-F]+$", outputSecurityGroupName)
37+
assert.Regexp(t, "^eg-ue2-test-msk-test-[0-9a-fA-F]+broker$", outputSecurityGroupName)
3838
}

variables-deprecated.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable "security_groups" {
2+
type = list(string)
3+
default = []
4+
description = <<-EOT
5+
DEPRECATED: Use `allowed_security_group_ids` instead.
6+
List of security group IDs to be allowed to connect to the cluster
7+
EOT
8+
}

variables.tf

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,9 @@ variable "zone_id" {
3535
default = null
3636
}
3737

38-
variable "security_groups" {
39-
type = list(string)
40-
default = []
41-
description = "List of security group IDs to be allowed to connect to the cluster"
42-
}
43-
44-
variable "broker_node_security_groups" {
45-
type = list(string)
46-
default = []
47-
description = "List of broker node security group IDs to be associated with the elastic network interfaces to control who can communicate with the cluster"
48-
}
49-
38+
# Intentionally not deprecated via security_group_inputs.tf since it cannot effectively be replaced via var.additional_security_group_rules.
39+
# This is because the logic to create these rules exists within this module, and should not be passed in by the consumer
40+
# of this module.
5041
variable "allowed_cidr_blocks" {
5142
type = list(string)
5243
default = []

versions.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 3.0"
88
}
9-
random = {
10-
source = "hashicorp/random"
11-
version = ">= 2.2"
12-
}
139
}
1410
}

0 commit comments

Comments
 (0)