Skip to content

Commit eca0887

Browse files
committed
feat: Use policy for tasks role instead of inline policy
1 parent b107c47 commit eca0887

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

modules/service/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,18 @@ module "ecs_service" {
201201
| [aws_ecs_task_set.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_set) | resource |
202202
| [aws_iam_policy.service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
203203
| [aws_iam_policy.task_exec](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
204+
| [aws_iam_policy.tasks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
204205
| [aws_iam_role.infrastructure_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
205206
| [aws_iam_role.service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
206207
| [aws_iam_role.task_exec](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
207208
| [aws_iam_role.tasks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
208-
| [aws_iam_role_policy.tasks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
209209
| [aws_iam_role_policy_attachment.infrastructure_iam_role_ebs_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
210210
| [aws_iam_role_policy_attachment.infrastructure_iam_role_vpc_lattice_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
211211
| [aws_iam_role_policy_attachment.service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
212212
| [aws_iam_role_policy_attachment.task_exec](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
213213
| [aws_iam_role_policy_attachment.task_exec_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
214214
| [aws_iam_role_policy_attachment.tasks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
215+
| [aws_iam_role_policy_attachment.tasks_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
215216
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
216217
| [aws_vpc_security_group_egress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_egress_rule) | resource |
217218
| [aws_vpc_security_group_ingress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_ingress_rule) | resource |
@@ -333,7 +334,7 @@ module "ecs_service" {
333334
| <a name="input_tasks_iam_role_name"></a> [tasks\_iam\_role\_name](#input\_tasks\_iam\_role\_name) | Name to use on IAM role created | `string` | `null` | no |
334335
| <a name="input_tasks_iam_role_path"></a> [tasks\_iam\_role\_path](#input\_tasks\_iam\_role\_path) | IAM role path | `string` | `null` | no |
335336
| <a name="input_tasks_iam_role_permissions_boundary"></a> [tasks\_iam\_role\_permissions\_boundary](#input\_tasks\_iam\_role\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the IAM role | `string` | `null` | no |
336-
| <a name="input_tasks_iam_role_policies"></a> [tasks\_iam\_role\_policies](#input\_tasks\_iam\_role\_policies) | Map of IAM role policy ARNs to attach to the IAM role | `map(string)` | `{}` | no |
337+
| <a name="input_tasks_iam_role_policies"></a> [tasks\_iam\_role\_policies](#input\_tasks\_iam\_role\_policies) | Map of additioanl IAM role policy ARNs to attach to the IAM role | `map(string)` | `{}` | no |
337338
| <a name="input_tasks_iam_role_statements"></a> [tasks\_iam\_role\_statements](#input\_tasks\_iam\_role\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | <pre>list(object({<br/> sid = optional(string)<br/> actions = optional(list(string))<br/> not_actions = optional(list(string))<br/> effect = optional(string)<br/> resources = optional(list(string))<br/> not_resources = optional(list(string))<br/> principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> not_principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> condition = optional(list(object({<br/> test = string<br/> values = list(string)<br/> variable = string<br/> })))<br/> }))</pre> | `null` | no |
338339
| <a name="input_tasks_iam_role_tags"></a> [tasks\_iam\_role\_tags](#input\_tasks\_iam\_role\_tags) | A map of additional tags to add to the IAM role created | `map(string)` | `{}` | no |
339340
| <a name="input_tasks_iam_role_use_name_prefix"></a> [tasks\_iam\_role\_use\_name\_prefix](#input\_tasks\_iam\_role\_use\_name\_prefix) | Determines whether the IAM role name (`tasks_iam_role_name`) is used as a prefix | `bool` | `true` | no |

modules/service/main.tf

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,13 +1096,6 @@ resource "aws_iam_role" "tasks" {
10961096
tags = merge(var.tags, var.tasks_iam_role_tags)
10971097
}
10981098

1099-
resource "aws_iam_role_policy_attachment" "tasks" {
1100-
for_each = { for k, v in var.tasks_iam_role_policies : k => v if local.create_tasks_iam_role }
1101-
1102-
role = aws_iam_role.tasks[0].name
1103-
policy_arn = each.value
1104-
}
1105-
11061099
data "aws_iam_policy_document" "tasks" {
11071100
count = local.create_tasks_iam_role && (var.tasks_iam_role_statements != null || var.enable_execute_command) ? 1 : 0
11081101

@@ -1163,13 +1156,29 @@ data "aws_iam_policy_document" "tasks" {
11631156
}
11641157
}
11651158

1166-
resource "aws_iam_role_policy" "tasks" {
1159+
resource "aws_iam_policy" "tasks" {
11671160
count = local.create_tasks_iam_role && (var.tasks_iam_role_statements != null || var.enable_execute_command) ? 1 : 0
11681161

11691162
name = var.tasks_iam_role_use_name_prefix ? null : local.tasks_iam_role_name
11701163
name_prefix = var.tasks_iam_role_use_name_prefix ? "${local.tasks_iam_role_name}-" : null
1164+
description = coalesce(var.tasks_iam_role_description, "Task role IAM policy")
11711165
policy = data.aws_iam_policy_document.tasks[0].json
1172-
role = aws_iam_role.tasks[0].id
1166+
path = var.tasks_iam_role_path
1167+
tags = merge(var.tags, var.tasks_iam_role_tags)
1168+
}
1169+
1170+
resource "aws_iam_role_policy_attachment" "tasks" {
1171+
count = local.create_tasks_iam_role && (length(var.tasks_iam_role_statements) > 0 || var.enable_execute_command) ? 1 : 0
1172+
1173+
role = aws_iam_role.tasks[0].name
1174+
policy_arn = aws_iam_policy.tasks[0].arn
1175+
}
1176+
1177+
resource "aws_iam_role_policy_attachment" "tasks_additional" {
1178+
for_each = { for k, v in var.tasks_iam_role_policies : k => v if local.create_tasks_iam_role }
1179+
1180+
role = aws_iam_role.tasks[0].name
1181+
policy_arn = each.value
11731182
}
11741183

11751184
################################################################################

modules/service/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ variable "tasks_iam_role_tags" {
727727
}
728728

729729
variable "tasks_iam_role_policies" {
730-
description = "Map of IAM role policy ARNs to attach to the IAM role"
730+
description = "Map of additioanl IAM role policy ARNs to attach to the IAM role"
731731
type = map(string)
732732
default = {}
733733
}

0 commit comments

Comments
 (0)