Skip to content

Commit 48f5647

Browse files
authored
fix external task definition must exist before first run (#204)
* fix external task definition must exist before first run * update readme * fix lint warnings
1 parent 423cecd commit 48f5647

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Available targets:
366366
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
367367
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
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 |
369-
| <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 |
369+
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | A `list(string)` of zero or one ARNs of task definitions, to reuse<br>reuse an existing task definition family and revision for the ecs<br>service instead of creating one<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | 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.<br>Changes to the list will have ripple effects, so use `task_exec_policy_arns_map` if possible. | `list(string)` | `[]` | no |
371371
| <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.<br>The names are arbitrary, but must be known at plan time. The purpose of the name<br>is so that changes to one ARN do not cause a ripple effect on the other ARNs.<br>If you cannot provide unique names known at plan time, use `task_exec_policy_arns` instead. | `map(string)` | `{}` | no |
372372
| <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 |

docs/terraform.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
120120
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
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 |
122-
| <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 |
122+
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | A `list(string)` of zero or one ARNs of task definitions, to reuse<br>reuse an existing task definition family and revision for the ecs<br>service instead of creating one<br>DEPRECATED: you can also pass a `string` with the ARN, but that<br>string must be known a "plan" time. | `any` | `[]` | 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.<br>Changes to the list will have ripple effects, so use `task_exec_policy_arns_map` if possible. | `list(string)` | `[]` | no |
124124
| <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.<br>The names are arbitrary, but must be known at plan time. The purpose of the name<br>is so that changes to one ARN do not cause a ripple effect on the other ARNs.<br>If you cannot provide unique names known at plan time, use `task_exec_policy_arns` instead. | `map(string)` | `{}` | no |
125125
| <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 |

main.tf

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ locals {
77
create_exec_role = local.enabled && length(var.task_exec_role_arn) == 0
88
enable_ecs_service_role = module.this.enabled && var.network_mode != "awsvpc" && length(var.ecs_load_balancers) >= 1
99
create_security_group = local.enabled && var.network_mode == "awsvpc" && var.security_group_enabled
10+
create_task_definition = local.enabled && length(var.task_definition) == 0
1011

1112
volumes = concat(var.docker_volumes, var.efs_volumes, var.fsx_volumes, var.bind_mount_volumes)
1213

@@ -46,7 +47,7 @@ module "exec_label" {
4647
}
4748

4849
resource "aws_ecs_task_definition" "default" {
49-
count = local.enabled && var.task_definition == null ? 1 : 0
50+
count = local.create_task_definition ? 1 : 0
5051
family = module.this.id
5152
container_definitions = var.container_definition_json
5253
requires_compatibilities = [var.launch_type]
@@ -360,7 +361,7 @@ resource "aws_security_group_rule" "nlb" {
360361
resource "aws_ecs_service" "ignore_changes_task_definition" {
361362
count = local.ecs_service_enabled && var.ignore_changes_task_definition && !var.ignore_changes_desired_count ? 1 : 0
362363
name = module.this.id
363-
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
364+
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
364365
desired_count = var.desired_count
365366
deployment_maximum_percent = var.deployment_maximum_percent
366367
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
@@ -459,7 +460,7 @@ resource "aws_ecs_service" "ignore_changes_task_definition" {
459460
resource "aws_ecs_service" "ignore_changes_task_definition_and_desired_count" {
460461
count = local.ecs_service_enabled && var.ignore_changes_task_definition && var.ignore_changes_desired_count ? 1 : 0
461462
name = module.this.id
462-
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
463+
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
463464
desired_count = var.desired_count
464465
deployment_maximum_percent = var.deployment_maximum_percent
465466
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
@@ -558,7 +559,7 @@ resource "aws_ecs_service" "ignore_changes_task_definition_and_desired_count" {
558559
resource "aws_ecs_service" "ignore_changes_desired_count" {
559560
count = local.ecs_service_enabled && !var.ignore_changes_task_definition && var.ignore_changes_desired_count ? 1 : 0
560561
name = module.this.id
561-
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
562+
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
562563
desired_count = var.desired_count
563564
deployment_maximum_percent = var.deployment_maximum_percent
564565
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
@@ -657,7 +658,7 @@ resource "aws_ecs_service" "ignore_changes_desired_count" {
657658
resource "aws_ecs_service" "default" {
658659
count = local.ecs_service_enabled && !var.ignore_changes_task_definition && !var.ignore_changes_desired_count ? 1 : 0
659660
name = module.this.id
660-
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
661+
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
661662
desired_count = var.desired_count
662663
deployment_maximum_percent = var.deployment_maximum_percent
663664
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent

variables.tf

+9-3
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,15 @@ variable "wait_for_steady_state" {
439439
}
440440

441441
variable "task_definition" {
442-
type = string
443-
description = "Reuse an existing task definition family and revision for the ecs service instead of creating one"
444-
default = null
442+
type = any
443+
description = <<-EOT
444+
A `list(string)` of zero or one ARNs of task definitions, to reuse
445+
reuse an existing task definition family and revision for the ecs
446+
service instead of creating one
447+
DEPRECATED: you can also pass a `string` with the ARN, but that
448+
string must be known a "plan" time.
449+
EOT
450+
default = []
445451
}
446452

447453
variable "force_new_deployment" {

0 commit comments

Comments
 (0)