@@ -145,6 +145,8 @@ resource "aws_ecs_cluster_capacity_providers" "this" {
145145# ###############################################################################
146146
147147locals {
148+ managed_instances_enabled = anytrue ([for k , v in local . capacity_providers : v . managed_instances_provider != null ])
149+
148150 # TODO - embed the `autoscaling_capacity_providers` into a shape acceptable for
149151 # `var.capacity_providers` so that it can be merged with the new `capacity_providers`
150152 # for backward compatibility. Remove `autoscaling_capacity_providers` in the next major version.
@@ -322,7 +324,7 @@ resource "aws_ecs_capacity_provider" "this" {
322324 for_each = instance_launch_template. value . network_configuration != null ? [instance_launch_template . value . network_configuration ] : []
323325
324326 content {
325- security_groups = network_configuration. value . security_groups
327+ security_groups = local . create_security_group ? flatten ( concat (aws_security_group . this [ * ] . id , network_configuration . value . security_groups )) : network_configuration. value . security_groups
326328 subnets = network_configuration. value . subnets
327329 }
328330 }
@@ -510,8 +512,8 @@ resource "aws_iam_role_policy_attachment" "task_exec" {
510512# ###########################################################################################
511513
512514locals {
513- needs_infrastructure_iam_role = anytrue ([ for k , v in local . capacity_providers : v . managed_instances_provider != null ])
514- create_infrastructure_iam_role = var . create && var . create_infrastructure_iam_role && local . needs_infrastructure_iam_role
515+ create_infrastructure_iam_role = var . create && var . create_infrastructure_iam_role && local . managed_instances_enabled
516+
515517 infrastructure_iam_role_name = coalesce (var. infrastructure_iam_role_name , " ${ var . name } -infra" , " NotProvided" )
516518}
517519
@@ -560,7 +562,7 @@ resource "aws_iam_role_policy_attachment" "infrastructure_managed_instances" {
560562# ###############################################################################
561563
562564locals {
563- create_node_iam_instance_profile = var. create && var. create_node_iam_instance_profile
565+ create_node_iam_instance_profile = var. create && var. create_node_iam_instance_profile && local . managed_instances_enabled
564566
565567 node_iam_role_name = coalesce (var. node_iam_role_name , " ${ var . name } -node" )
566568}
@@ -739,3 +741,78 @@ resource "aws_iam_instance_profile" "this" {
739741 create_before_destroy = true
740742 }
741743}
744+
745+ # ###############################################################################
746+ # Security Group
747+ # ###############################################################################
748+
749+ locals {
750+ create_security_group = var. create && var. create_security_group && local. managed_instances_enabled
751+
752+ security_group_name = coalesce (var. security_group_name , var. name , " NotProvided" )
753+ }
754+
755+ resource "aws_security_group" "this" {
756+ count = local. create_security_group ? 1 : 0
757+
758+ region = var. region
759+
760+ name = var. security_group_use_name_prefix ? null : local. security_group_name
761+ name_prefix = var. security_group_use_name_prefix ? " ${ local . security_group_name } -" : null
762+ description = var. security_group_description
763+ vpc_id = var. vpc_id
764+
765+ tags = merge (
766+ var. tags ,
767+ { Name = local.security_group_name },
768+ var. security_group_tags
769+ )
770+
771+ lifecycle {
772+ create_before_destroy = true
773+ }
774+ }
775+
776+ resource "aws_vpc_security_group_ingress_rule" "this" {
777+ for_each = { for k , v in var . security_group_ingress_rules : k => v if var . security_group_ingress_rules != null && local . create_security_group }
778+
779+ region = var. region
780+
781+ cidr_ipv4 = each. value . cidr_ipv4
782+ cidr_ipv6 = each. value . cidr_ipv6
783+ description = each. value . description
784+ from_port = each. value . from_port
785+ ip_protocol = each. value . ip_protocol
786+ prefix_list_id = each. value . prefix_list_id
787+ referenced_security_group_id = each. value . referenced_security_group_id == " self" ? aws_security_group. this [0 ]. id : each. value . referenced_security_group_id
788+ security_group_id = aws_security_group. this [0 ]. id
789+ tags = merge (
790+ var. tags ,
791+ var. security_group_tags ,
792+ { " Name" = coalesce (each. value . name , " ${ local . security_group_name } -${ each . key } " ) },
793+ each. value . tags
794+ )
795+ to_port = try (coalesce (each. value . to_port , each. value . from_port ), null )
796+ }
797+
798+ resource "aws_vpc_security_group_egress_rule" "this" {
799+ for_each = { for k , v in var . security_group_egress_rules : k => v if var . security_group_egress_rules != null && local . create_security_group }
800+
801+ region = var. region
802+
803+ cidr_ipv4 = each. value . cidr_ipv4
804+ cidr_ipv6 = each. value . cidr_ipv6
805+ description = each. value . description
806+ from_port = try (coalesce (each. value . from_port , each. value . to_port ), null )
807+ ip_protocol = each. value . ip_protocol
808+ prefix_list_id = each. value . prefix_list_id
809+ referenced_security_group_id = each. value . referenced_security_group_id == " self" ? aws_security_group. this [0 ]. id : each. value . referenced_security_group_id
810+ security_group_id = aws_security_group. this [0 ]. id
811+ tags = merge (
812+ var. tags ,
813+ var. security_group_tags ,
814+ { " Name" = coalesce (each. value . name , " ${ local . security_group_name } -${ each . key } " ) },
815+ each. value . tags
816+ )
817+ to_port = each. value . to_port
818+ }
0 commit comments