Skip to content

Commit 5f9e32d

Browse files
authored
feat: allow configuring of additional security group rules (#38)
## what - This PR makes it possible to optionally add additional security group rules to the main security group - We add the following: - A new tf resource `aws_security_group_rule.additonal`, which loops over... - A new tf variable `var.additional_security_group_rules` of type map - Additions to the README.md ## why - By allowing the configuration of sg rules directly in this module, we do not require the module user to create additional security groups outside this module. This is especially useful for those users that consume this module with terragrunt, who may not have the ability to easily create additional security groups ## references - N/A, but as a user of downstream module [terraform-aws-tailscale](https://github.com/masterpointio/terraform-aws-tailscale), I would be delighted with this addition. If accepted, I will follow-up with a PR in that module as well <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for defining additional security group rules via a new input variable. - **Documentation** - Updated documentation to include details about the new resource and input variable for additional security group rules. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 2a72ece commit 5f9e32d

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Use [the awesome `gossm` project](https://github.com/gjbae1212/gossm).
115115
| [aws_iam_role_policy_attachment.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
116116
| [aws_launch_template.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
117117
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
118+
| [aws_security_group_rule.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
118119
| [aws_security_group_rule.allow_all_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
119120
| [aws_ssm_document.session_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_document) | resource |
120121
| [null_resource.validate_instance_type](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
@@ -130,6 +131,7 @@ Use [the awesome `gossm` project](https://github.com/gjbae1212/gossm).
130131
| Name | Description | Type | Default | Required |
131132
|------|-------------|------|---------|:--------:|
132133
| <a name="input_additional_security_group_ids"></a> [additional\_security\_group\_ids](#input\_additional\_security\_group\_ids) | Security groups that will be attached to the app instances | `list(string)` | `[]` | no |
134+
| <a name="input_additional_security_group_rules"></a> [additional\_security\_group\_rules](#input\_additional\_security\_group\_rules) | Additional security group rules that will be attached to the primary security group | <pre>map(object({<br/> type = string<br/> from_port = number<br/> to_port = number<br/> protocol = string<br/><br/> description = optional(string)<br/> cidr_blocks = optional(list(string))<br/> ipv6_cidr_blocks = optional(list(string))<br/> prefix_list_ids = optional(list(string))<br/> self = optional(bool)<br/> }))</pre> | `{}` | no |
133135
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br/>This is for some rare cases where resources want additional configuration of tags<br/>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
134136
| <a name="input_ami"></a> [ami](#input\_ami) | The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2023 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2023 AMI. Pin to a specific AMI if you would like to avoid these updates. | `string` | `""` | no |
135137
| <a name="input_architecture"></a> [architecture](#input\_architecture) | The architecture of the AMI (e.g., x86\_64, arm64) | `string` | `"arm64"` | no |

main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ resource "aws_security_group_rule" "allow_all_egress" {
159159
security_group_id = aws_security_group.default.id
160160
}
161161

162+
resource "aws_security_group_rule" "additional" {
163+
for_each = var.additional_security_group_rules
164+
165+
type = lookup(each.value, "type")
166+
from_port = lookup(each.value, "from_port")
167+
to_port = lookup(each.value, "to_port")
168+
protocol = lookup(each.value, "protocol")
169+
170+
description = lookup(each.value, "description", null)
171+
cidr_blocks = lookup(each.value, "cidr_blocks", null)
172+
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
173+
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
174+
self = lookup(each.value, "self", null)
175+
176+
security_group_id = aws_security_group.default.id
177+
}
178+
162179
#######################
163180
## SECURITY LOGGING ##
164181
#####################

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ variable "additional_security_group_ids" {
6262
default = []
6363
}
6464

65+
variable "additional_security_group_rules" {
66+
description = "Additional security group rules that will be attached to the primary security group"
67+
type = map(object({
68+
type = string
69+
from_port = number
70+
to_port = number
71+
protocol = string
72+
73+
description = optional(string)
74+
cidr_blocks = optional(list(string))
75+
ipv6_cidr_blocks = optional(list(string))
76+
prefix_list_ids = optional(list(string))
77+
self = optional(bool)
78+
}))
79+
default = {}
80+
}
81+
6582
variable "monitoring_enabled" {
6683
description = "Enable detailed monitoring of instance"
6784
type = bool

0 commit comments

Comments
 (0)