Skip to content

Commit 14008fc

Browse files
fixes: Changing task_exec_policy_arns or task_policy_arns cause recreations #167 (#178)
* fix: role uses count * update other policies * solves policy updates and fixes outputs * cleanup * cleanup * Auto Format Co-authored-by: cloudpossebot <11232728+cloudpossebot@users.noreply.github.com>
1 parent cb4f26e commit 14008fc

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ resource "aws_iam_role" "ecs_task" {
159159
}
160160

161161
resource "aws_iam_role_policy_attachment" "ecs_task" {
162-
count = local.create_task_role ? length(var.task_policy_arns) : 0
163-
policy_arn = var.task_policy_arns[count.index]
162+
for_each = local.create_task_role ? toset(var.task_policy_arns) : toset([])
163+
policy_arn = each.value
164164
role = join("", aws_iam_role.ecs_task.*.id)
165165
}
166166

@@ -279,15 +279,15 @@ data "aws_iam_policy_document" "ecs_exec" {
279279
}
280280

281281
resource "aws_iam_role_policy" "ecs_exec" {
282-
count = local.create_exec_role ? 1 : 0
283-
name = module.exec_label.id
284-
policy = join("", data.aws_iam_policy_document.ecs_exec.*.json)
285-
role = join("", aws_iam_role.ecs_exec.*.id)
282+
for_each = local.create_exec_role ? toset(["true"]) : toset([])
283+
name = module.exec_label.id
284+
policy = join("", data.aws_iam_policy_document.ecs_exec.*.json)
285+
role = join("", aws_iam_role.ecs_exec.*.id)
286286
}
287287

288288
resource "aws_iam_role_policy_attachment" "ecs_exec" {
289-
count = local.create_exec_role ? length(var.task_exec_policy_arns) : 0
290-
policy_arn = var.task_exec_policy_arns[count.index]
289+
for_each = local.create_exec_role ? toset(var.task_exec_policy_arns) : toset([])
290+
policy_arn = each.value
291291
role = join("", aws_iam_role.ecs_exec.*.id)
292292
}
293293

outputs.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
output "ecs_exec_role_policy_id" {
22
description = "The ECS service role policy ID, in the form of `role_name:role_policy_name`"
3-
value = join("", aws_iam_role_policy.ecs_exec.*.id)
3+
value = join("", [
4+
for k, v in aws_iam_role_policy.ecs_exec : v.id
5+
])
46
}
57

68
output "ecs_exec_role_policy_name" {
79
description = "ECS service role name"
8-
value = join("", aws_iam_role_policy.ecs_exec.*.name)
10+
value = join("", [
11+
for k, v in aws_iam_role_policy.ecs_exec : v.name
12+
])
913
}
1014

1115
output "service_name" {

0 commit comments

Comments
 (0)