Skip to content

Commit 4e1473d

Browse files
fix: Correct logic for local needs_infrastructure_iam_role check (#224)
* Fix need infrastructure role check. * try adjusting logic. * explicit dep * forgot this default * update example.
1 parent deda673 commit 4e1473d

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

examples/ec2-autoscaling/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ module "ecs_service" {
9595
}
9696
}
9797

98-
create_infrastructure_iam_role = true
9998
volume_configuration = {
10099
ebs-volume = {
101100
managed_ebs_volume = {

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ module "service" {
100100
iam_role_statements = lookup(each.value, "iam_role_statements", {})
101101

102102
# ECS infrastructure IAM role
103-
create_infrastructure_iam_role = try(each.value.create_infrastructure_iam_role, false)
103+
create_infrastructure_iam_role = try(each.value.create_infrastructure_iam_role, true)
104104
infrastructure_iam_role_arn = try(each.value.infrastructure_iam_role_arn, null)
105105
infrastructure_iam_role_name = try(each.value.infrastructure_iam_role_name, null)
106106
infrastructure_iam_role_use_name_prefix = try(each.value.infrastructure_iam_role_use_name_prefix, true)

modules/service/main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ resource "aws_ecs_service" "this" {
225225
for_each = try([volume_configuration.value.managed_ebs_volume], [])
226226

227227
content {
228-
role_arn = try(aws_iam_role.infrastructure_iam_role[0].arn, var.infrastructure_iam_role_arn)
228+
role_arn = local.infrastructure_iam_role_arn
229229
encrypted = try(managed_ebs_volume.value.encrypted, null)
230230
file_system_type = try(managed_ebs_volume.value.file_system_type, null)
231231
iops = try(managed_ebs_volume.value.iops, null)
@@ -254,7 +254,8 @@ resource "aws_ecs_service" "this" {
254254

255255
depends_on = [
256256
aws_iam_role_policy_attachment.service,
257-
aws_iam_role_policy_attachment.infrastructure_iam_role_ebs_policy
257+
aws_iam_role_policy_attachment.infrastructure_iam_role_ebs_policy,
258+
aws_iam_role.infrastructure_iam_role,
258259
]
259260

260261
lifecycle {
@@ -1505,8 +1506,9 @@ resource "aws_security_group_rule" "this" {
15051506
############################################################################################
15061507

15071508
locals {
1508-
needs_infrastructure_iam_role = var.volume_configuration != null
1509+
needs_infrastructure_iam_role = length(var.volume_configuration) > 0
15091510
create_infrastructure_iam_role = var.create && var.create_infrastructure_iam_role && local.needs_infrastructure_iam_role
1511+
infrastructure_iam_role_arn = local.needs_infrastructure_iam_role ? try(aws_iam_role.infrastructure_iam_role[0].arn, var.infrastructure_iam_role_arn) : null
15101512
infrastructure_iam_role_name = try(coalesce(var.infrastructure_iam_role_name, var.name), "")
15111513
}
15121514

0 commit comments

Comments
 (0)