Skip to content

Commit

Permalink
Reuse existing task definition family and revision (#108)
Browse files Browse the repository at this point in the history
* Reuse existing task definition arn

* Reuse task definition if provided

* Update variables.tf

* Update main.tf

* Update variables.tf

* Auto Format

* Auto Format

* Update main.tf

Co-authored-by: cloudpossebot <[email protected]>
  • Loading branch information
nitrocode and cloudpossebot authored Feb 25, 2021
1 parent a0ed5cd commit afb8060
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ Available targets:
| subnet\_ids | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
| 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 |
| task\_definition | Reuse an existing task definition family and revision for the ecs service instead of creating one | `string` | `null` | no |
| task\_exec\_policy\_arns | A list of IAM Policy ARNs to attach to the generated task execution role. | `list(string)` | `[]` | no |
| task\_exec\_role\_arn | The ARN of IAM role that allows the ECS/Fargate agent to make calls to the ECS API on your behalf | `string` | `""` | no |
| 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 |
Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
| subnet\_ids | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
| 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 |
| task\_definition | Reuse an existing task definition family and revision for the ecs service instead of creating one | `string` | `null` | no |
| task\_exec\_policy\_arns | A list of IAM Policy ARNs to attach to the generated task execution role. | `list(string)` | `[]` | no |
| task\_exec\_role\_arn | The ARN of IAM role that allows the ECS/Fargate agent to make calls to the ECS API on your behalf | `string` | `""` | no |
| 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 |
Expand Down
6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module "exec_label" {
}

resource "aws_ecs_task_definition" "default" {
count = local.enabled ? 1 : 0
count = local.enabled && var.task_definition == null ? 1 : 0
family = module.this.id
container_definitions = var.container_definition_json
requires_compatibilities = [var.launch_type]
Expand Down Expand Up @@ -287,7 +287,7 @@ resource "aws_security_group_rule" "nlb" {
resource "aws_ecs_service" "ignore_changes_task_definition" {
count = local.enabled && var.ignore_changes_task_definition ? 1 : 0
name = module.this.id
task_definition = "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}"
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
Expand Down Expand Up @@ -369,7 +369,7 @@ resource "aws_ecs_service" "ignore_changes_task_definition" {
resource "aws_ecs_service" "default" {
count = local.enabled && var.ignore_changes_task_definition == false ? 1 : 0
name = module.this.id
task_definition = "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}"
task_definition = coalesce(var.task_definition, "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}")
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,9 @@ variable "use_old_arn" {
description = "A flag to enable/disable tagging the ecs resources that require the new arn format"
default = false
}

variable "task_definition" {
type = string
description = "Reuse an existing task definition family and revision for the ecs service instead of creating one"
default = null
}

0 comments on commit afb8060

Please sign in to comment.