Skip to content

Commit 5003b0d

Browse files
committed
Allow client to create new security group
1 parent 6f851d8 commit 5003b0d

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
lines changed

main.tf

+58-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ locals {
66
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false
77

88
ami = try(coalesce(var.ami, try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
9+
10+
security_group_name = try(coalesce(var.security_group_name, "${var.name}-sg"), "")
11+
sg_ingress_rules = try(lookup(var.security_group_rules, "ingress", {}), {})
12+
create_sg_ingress_rule = length(keys(local.sg_ingress_rules)) > 0 ? true : false
13+
sg_egress_rules = try(lookup(var.security_group_rules, "egress", {}), {})
14+
create_sg_egress_rule = length(keys(local.sg_egress_rules)) > 0 ? true : false
915
}
1016

1117
data "aws_ssm_parameter" "this" {
@@ -33,7 +39,7 @@ resource "aws_instance" "this" {
3339

3440
availability_zone = var.availability_zone
3541
subnet_id = var.subnet_id
36-
vpc_security_group_ids = var.vpc_security_group_ids
42+
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, "")], var.vpc_security_group_ids))
3743

3844
key_name = var.key_name
3945
monitoring = var.monitoring
@@ -211,7 +217,7 @@ resource "aws_instance" "ignore_ami" {
211217

212218
availability_zone = var.availability_zone
213219
subnet_id = var.subnet_id
214-
vpc_security_group_ids = var.vpc_security_group_ids
220+
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, "")], var.vpc_security_group_ids))
215221

216222
key_name = var.key_name
217223
monitoring = var.monitoring
@@ -395,7 +401,7 @@ resource "aws_spot_instance_request" "this" {
395401

396402
availability_zone = var.availability_zone
397403
subnet_id = var.subnet_id
398-
vpc_security_group_ids = var.vpc_security_group_ids
404+
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, "")], var.vpc_security_group_ids))
399405

400406
key_name = var.key_name
401407
monitoring = var.monitoring
@@ -620,3 +626,52 @@ resource "aws_eip" "this" {
620626

621627
tags = merge(var.tags, var.eip_tags)
622628
}
629+
630+
################################################################################
631+
# Security Group
632+
################################################################################
633+
634+
resource "aws_security_group" "this" {
635+
count = local.create && var.create_security_group ? 1 : 0
636+
637+
name = var.security_group_use_name_prefix ? null : local.security_group_name
638+
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
639+
vpc_id = var.vpc_id
640+
description = coalesce(var.security_group_description, "Control traffic to/from EC2 instance ${var.name}")
641+
642+
tags = merge(var.tags, var.security_group_tags, { Name = local.security_group_name })
643+
644+
lifecycle {
645+
create_before_destroy = true
646+
}
647+
}
648+
649+
resource "aws_vpc_security_group_egress_rule" "this" {
650+
for_each = { for k, v in local.sg_egress_rules : k => v if local.create && var.create_security_group && local.create_sg_egress_rule }
651+
652+
security_group_id = aws_security_group.this[0].id
653+
cidr_ipv4 = each.value.cidr_ipv4
654+
cidr_ipv6 = each.value.cidr_ipv6
655+
description = each.value.description
656+
from_port = each.value.from_port
657+
to_port = each.value.to_port
658+
ip_protocol = each.value.ip_protocol
659+
prefix_list_id = each.value.prefix_list_id
660+
referenced_security_group_id = each.value.referenced_security_group_id
661+
tags = merge(try(each.value.tags, {}), var.security_group_tags, { Name = local.security_group_name })
662+
}
663+
664+
resource "aws_vpc_security_group_ingress_rule" "this" {
665+
for_each = { for k, v in local.sg_ingress_rules : k => v if local.create && var.create_security_group && local.create_sg_ingress_rule }
666+
667+
security_group_id = aws_security_group.this[0].id
668+
cidr_ipv4 = each.value.cidr_ipv4
669+
cidr_ipv6 = each.value.cidr_ipv6
670+
description = each.value.description
671+
from_port = each.value.from_port
672+
to_port = each.value.to_port
673+
ip_protocol = each.value.ip_protocol
674+
prefix_list_id = each.value.prefix_list_id
675+
referenced_security_group_id = each.value.referenced_security_group_id
676+
tags = merge(try(each.value.tags, {}), var.security_group_tags, { Name = local.security_group_name })
677+
}

outputs.tf

+16
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,19 @@ output "ephemeral_block_device" {
230230
null
231231
)
232232
}
233+
234+
################################################################################
235+
# Security Group
236+
################################################################################
237+
238+
output "security_group_id" {
239+
description = "The security group ID of the cluster"
240+
value = try(aws_security_group.this[0].id, null)
241+
}
242+
243+
output "egress_security_group_rule_ids" {
244+
description = "The egress security group rule IDs of the cluster"
245+
value = {
246+
for k, v in try(aws_vpc_security_group_egress_rule.this, {}) : k => v
247+
}
248+
}

variables.tf

+79
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,82 @@ variable "eip_tags" {
430430
type = map(string)
431431
default = {}
432432
}
433+
434+
################################################################################
435+
# Security Group
436+
################################################################################
437+
variable "create_security_group" {
438+
description = "Determines whether to create security group for EC2 instance"
439+
type = bool
440+
default = true
441+
}
442+
443+
variable "security_group_name" {
444+
description = "The security group name. Default value is (`var.name`)"
445+
type = string
446+
default = ""
447+
}
448+
449+
variable "security_group_use_name_prefix" {
450+
description = "Determines whether the security group name (`var.name`) is used as a prefix"
451+
type = bool
452+
default = true
453+
}
454+
455+
variable "security_group_description" {
456+
description = "The description of the security group."
457+
type = string
458+
default = null
459+
}
460+
461+
variable "vpc_id" {
462+
description = "ID of the VPC where to create security group"
463+
type = string
464+
default = ""
465+
}
466+
467+
variable "security_group_rules" {
468+
description = "Map of security group rules to add to the cluster security group created"
469+
type = object({
470+
ingress = optional(map(object({
471+
cidr_ipv4 = optional(string)
472+
cidr_ipv6 = optional(string)
473+
description = optional(string)
474+
from_port = optional(number)
475+
ip_protocol = optional(string)
476+
prefix_list_id = optional(string)
477+
referenced_security_group_id = optional(string)
478+
tags = optional(map(string))
479+
to_port = optional(number)
480+
})))
481+
egress = optional(map(object({
482+
cidr_ipv4 = optional(string)
483+
cidr_ipv6 = optional(string)
484+
description = optional(string)
485+
from_port = optional(number)
486+
ip_protocol = optional(string)
487+
prefix_list_id = optional(string)
488+
referenced_security_group_id = optional(string)
489+
tags = optional(map(string))
490+
to_port = optional(number)
491+
})))
492+
})
493+
default = {
494+
ingress = {}
495+
egress = {
496+
all_https = {
497+
cidr_ipv4 = "0.0.0.0/0"
498+
description = "Allow all outbound HTTPS traffic"
499+
from_port = 443
500+
ip_protocol = "tcp"
501+
to_port = 443
502+
}
503+
}
504+
}
505+
}
506+
507+
variable "security_group_tags" {
508+
description = "Additional tags for the security group"
509+
type = map(string)
510+
default = {}
511+
}

0 commit comments

Comments
 (0)