Skip to content

Commit c9f96f1

Browse files
Use map for task & task exec policy arns variables (#194)
* Add option to trigger a redeployment on apply (#193) * Add option to trigger a redeployment on apply * add triggers var to each variation of ecs_service * set redeployment trigger value in locals * Update example version and fixture * Update readme * Bump min tf version to 0.14 doc: rebuild readme * Run pr/auto-format/host locally * Modify task_policy_arns to use map - avoid Terraform for_each error related to dependency on resources known after apply - deprecate var task_policy_arns - replace with task_policy_arns_map * Use map instead of list for task_policy_arns * Replace task_exec_policy_arns with task_exec_policy_arns_map - deprecate task_exec_policy_arns - add test input for task_policy_arns * Rename test policy statement * Update readme * Auto Format * bump tf version for examples --------- Co-authored-by: cloudpossebot <[email protected]>
1 parent c228577 commit c9f96f1

File tree

9 files changed

+73
-11
lines changed

9 files changed

+73
-11
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99

1010
.build-harness
1111
build-harness
12+
13+
# Test output
14+
test.log
15+
.terraform.lock.hcl

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ Available targets:
368368
| <a name="input_task_cpu"></a> [task\_cpu](#input\_task\_cpu) | The number of CPU units used by the task. If using `FARGATE` launch type `task_cpu` must match [supported memory values](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | `number` | `256` | no |
369369
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | Reuse an existing task definition family and revision for the ecs service instead of creating one | `string` | `null` | no |
370370
| <a name="input_task_exec_policy_arns"></a> [task\_exec\_policy\_arns](#input\_task\_exec\_policy\_arns) | A list of IAM Policy ARNs to attach to the generated task execution role. | `list(string)` | `[]` | no |
371+
| <a name="input_task_exec_policy_arns_map"></a> [task\_exec\_policy\_arns\_map](#input\_task\_exec\_policy\_arns\_map) | A map of name to IAM Policy ARNs to attach to the generated task execution role. | `map(string)` | `{}` | no |
371372
| <a name="input_task_exec_role_arn"></a> [task\_exec\_role\_arn](#input\_task\_exec\_role\_arn) | A `list(string)` of zero or one ARNs of IAM roles that allows the<br>ECS/Fargate agent to make calls to the ECS API on your behalf.<br>If the list is empty, a role will be created for you.<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | no |
372373
| <a name="input_task_memory"></a> [task\_memory](#input\_task\_memory) | The amount of memory (in MiB) used by the task. If using Fargate launch type `task_memory` must match [supported cpu value](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | `number` | `512` | no |
373374
| <a name="input_task_placement_constraints"></a> [task\_placement\_constraints](#input\_task\_placement\_constraints) | A set of placement constraints rules that are taken into consideration during task placement.<br>Maximum number of placement\_constraints is 10. See [`placement_constraints`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition#placement-constraints-arguments) | <pre>list(object({<br> type = string<br> expression = string<br> }))</pre> | `[]` | no |
374375
| <a name="input_task_policy_arns"></a> [task\_policy\_arns](#input\_task\_policy\_arns) | A list of IAM Policy ARNs to attach to the generated task role. | `list(string)` | `[]` | no |
376+
| <a name="input_task_policy_arns_map"></a> [task\_policy\_arns\_map](#input\_task\_policy\_arns\_map) | A map of name to IAM Policy ARNs to attach to the generated task role. | `map(string)` | `{}` | no |
375377
| <a name="input_task_role_arn"></a> [task\_role\_arn](#input\_task\_role\_arn) | A `list(string)` of zero or one ARNs of IAM roles that allows<br>your Amazon ECS container task to make calls to other AWS services.<br>If the list is empty, a role will be created for you.<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | no |
376378
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
377379
| <a name="input_use_alb_security_group"></a> [use\_alb\_security\_group](#input\_use\_alb\_security\_group) | A flag to enable/disable allowing traffic from the ALB security group to the service security group | `bool` | `false` | no |

docs/terraform.md

+2
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@
121121
| <a name="input_task_cpu"></a> [task\_cpu](#input\_task\_cpu) | The number of CPU units used by the task. If using `FARGATE` launch type `task_cpu` must match [supported memory values](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | `number` | `256` | no |
122122
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | Reuse an existing task definition family and revision for the ecs service instead of creating one | `string` | `null` | no |
123123
| <a name="input_task_exec_policy_arns"></a> [task\_exec\_policy\_arns](#input\_task\_exec\_policy\_arns) | A list of IAM Policy ARNs to attach to the generated task execution role. | `list(string)` | `[]` | no |
124+
| <a name="input_task_exec_policy_arns_map"></a> [task\_exec\_policy\_arns\_map](#input\_task\_exec\_policy\_arns\_map) | A map of name to IAM Policy ARNs to attach to the generated task execution role. | `map(string)` | `{}` | no |
124125
| <a name="input_task_exec_role_arn"></a> [task\_exec\_role\_arn](#input\_task\_exec\_role\_arn) | A `list(string)` of zero or one ARNs of IAM roles that allows the<br>ECS/Fargate agent to make calls to the ECS API on your behalf.<br>If the list is empty, a role will be created for you.<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | no |
125126
| <a name="input_task_memory"></a> [task\_memory](#input\_task\_memory) | The amount of memory (in MiB) used by the task. If using Fargate launch type `task_memory` must match [supported cpu value](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | `number` | `512` | no |
126127
| <a name="input_task_placement_constraints"></a> [task\_placement\_constraints](#input\_task\_placement\_constraints) | A set of placement constraints rules that are taken into consideration during task placement.<br>Maximum number of placement\_constraints is 10. See [`placement_constraints`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition#placement-constraints-arguments) | <pre>list(object({<br> type = string<br> expression = string<br> }))</pre> | `[]` | no |
127128
| <a name="input_task_policy_arns"></a> [task\_policy\_arns](#input\_task\_policy\_arns) | A list of IAM Policy ARNs to attach to the generated task role. | `list(string)` | `[]` | no |
129+
| <a name="input_task_policy_arns_map"></a> [task\_policy\_arns\_map](#input\_task\_policy\_arns\_map) | A map of name to IAM Policy ARNs to attach to the generated task role. | `map(string)` | `{}` | no |
128130
| <a name="input_task_role_arn"></a> [task\_role\_arn](#input\_task\_role\_arn) | A `list(string)` of zero or one ARNs of IAM roles that allows<br>your Amazon ECS container task to make calls to other AWS services.<br>If the list is empty, a role will be created for you.<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | no |
129131
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
130132
| <a name="input_use_alb_security_group"></a> [use\_alb\_security\_group](#input\_use\_alb\_security\_group) | A flag to enable/disable allowing traffic from the ALB security group to the service security group | `bool` | `false` | no |

examples/complete/main.tf

+27
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ module "container_definition" {
4545
port_mappings = var.container_port_mappings
4646
}
4747

48+
module "test_policy" {
49+
source = "cloudposse/iam-policy/aws"
50+
version = "0.4.0"
51+
52+
name = "policy"
53+
attributes = ["test"]
54+
55+
iam_policy_enabled = true
56+
description = "Test policy"
57+
58+
iam_policy_statements = [
59+
{
60+
sid = "DummyStatement"
61+
effect = "Allow"
62+
actions = ["none:null"]
63+
resources = ["*"]
64+
conditions = []
65+
}
66+
]
67+
68+
context = module.this.context
69+
}
70+
4871
module "ecs_alb_service_task" {
4972
source = "../.."
5073
alb_security_group = module.vpc.vpc_default_security_group_id
@@ -65,6 +88,10 @@ module "ecs_alb_service_task" {
6588
task_memory = var.task_memory
6689
task_cpu = var.task_cpu
6790
ecs_service_enabled = var.ecs_service_enabled
91+
force_new_deployment = var.force_new_deployment
92+
redeploy_on_apply = var.redeploy_on_apply
93+
task_policy_arns = [module.test_policy.policy_arn]
94+
# task_policy_arns_map = { test = module.test_policy.policy_arn }
6895

6996
context = module.this.context
7097
}

examples/complete/variables.tf

+12
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,15 @@ variable "ecs_service_enabled" {
126126
description = "Whether or not to create the aws_ecs_service resource"
127127
default = true
128128
}
129+
130+
variable "force_new_deployment" {
131+
type = bool
132+
description = "Enable to force a new task deployment of the service."
133+
default = false
134+
}
135+
136+
variable "redeploy_on_apply" {
137+
type = bool
138+
description = "Updates the service to the latest task definition on each apply"
139+
default = false
140+
}

examples/complete/versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.14.0"
2+
required_version = ">= 1.0"
33

44
required_providers {
55
aws = {

main.tf

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ locals {
1313
redeployment_trigger = var.force_new_deployment && var.redeploy_on_apply ? {
1414
redeployment = timestamp()
1515
} : {}
16+
17+
task_policy_arns_map = length(var.task_policy_arns) > 0 ? { for i, a in var.task_policy_arns : i => a } : var.task_policy_arns_map
18+
19+
task_exec_policy_arns_map = length(var.task_exec_policy_arns) > 0 ? { for i, a in var.task_exec_policy_arns : i => a } : var.task_exec_policy_arns_map
1620
}
1721

1822
module "task_label" {
@@ -163,7 +167,7 @@ resource "aws_iam_role" "ecs_task" {
163167
}
164168

165169
resource "aws_iam_role_policy_attachment" "ecs_task" {
166-
for_each = local.create_task_role ? toset(var.task_policy_arns) : toset([])
170+
for_each = local.create_task_role ? local.task_policy_arns_map : {}
167171
policy_arn = each.value
168172
role = join("", aws_iam_role.ecs_task.*.id)
169173
}
@@ -290,7 +294,7 @@ resource "aws_iam_role_policy" "ecs_exec" {
290294
}
291295

292296
resource "aws_iam_role_policy_attachment" "ecs_exec" {
293-
for_each = local.create_exec_role ? toset(var.task_exec_policy_arns) : toset([])
297+
for_each = local.create_exec_role ? local.task_exec_policy_arns_map : {}
294298
policy_arn = each.value
295299
role = join("", aws_iam_role.ecs_exec.*.id)
296300
}

variables-deprecated.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "task_policy_arns" {
2+
type = list(string)
3+
description = "A list of IAM Policy ARNs to attach to the generated task role."
4+
default = []
5+
}
6+
7+
variable "task_exec_policy_arns" {
8+
type = list(string)
9+
description = "A list of IAM Policy ARNs to attach to the generated task execution role."
10+
default = []
11+
}

variables.tf

+8-8
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ variable "task_exec_role_arn" {
193193
default = []
194194
}
195195

196-
variable "task_exec_policy_arns" {
197-
type = list(string)
198-
description = "A list of IAM Policy ARNs to attach to the generated task execution role."
199-
default = []
196+
variable "task_exec_policy_arns_map" {
197+
type = map(string)
198+
description = "A map of name to IAM Policy ARNs to attach to the generated task execution role."
199+
default = {}
200200
}
201201

202202
variable "task_role_arn" {
@@ -211,10 +211,10 @@ variable "task_role_arn" {
211211
default = []
212212
}
213213

214-
variable "task_policy_arns" {
215-
type = list(string)
216-
description = "A list of IAM Policy ARNs to attach to the generated task role."
217-
default = []
214+
variable "task_policy_arns_map" {
215+
type = map(string)
216+
description = "A map of name to IAM Policy ARNs to attach to the generated task role."
217+
default = {}
218218
}
219219

220220
variable "service_role_arn" {

0 commit comments

Comments
 (0)