From f177a497bee075ad303d356e1f392a67185f7333 Mon Sep 17 00:00:00 2001 From: Ivan Sukhomlyn Date: Tue, 20 Feb 2024 14:14:40 +0200 Subject: [PATCH] feat: Replacing hack with track_latest task definition attribute --- README.md | 2 +- examples/complete/README.md | 4 ++-- examples/complete/versions.tf | 2 +- examples/ec2-autoscaling/README.md | 4 ++-- examples/ec2-autoscaling/versions.tf | 2 +- examples/fargate/README.md | 4 ++-- examples/fargate/versions.tf | 2 +- main.tf | 3 ++- modules/cluster/README.md | 4 ++-- modules/cluster/versions.tf | 2 +- modules/container-definition/README.md | 4 ++-- modules/container-definition/versions.tf | 2 +- modules/service/README.md | 6 +++--- modules/service/main.tf | 22 +++------------------- modules/service/variables.tf | 6 ++++++ modules/service/versions.tf | 2 +- versions.tf | 2 +- wrappers/service/main.tf | 1 + 18 files changed, 33 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 3a3d87e..da4925e 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ module "ecs" { | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers diff --git a/examples/complete/README.md b/examples/complete/README.md index 35429b4..a3de37c 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -27,13 +27,13 @@ Note that this example may create resources which will incur monetary charges on | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf index 63e9319..5f6e023 100644 --- a/examples/complete/versions.tf +++ b/examples/complete/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/examples/ec2-autoscaling/README.md b/examples/ec2-autoscaling/README.md index d2206f8..d5288ec 100644 --- a/examples/ec2-autoscaling/README.md +++ b/examples/ec2-autoscaling/README.md @@ -27,13 +27,13 @@ Note that this example may create resources which will incur monetary charges on | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules diff --git a/examples/ec2-autoscaling/versions.tf b/examples/ec2-autoscaling/versions.tf index 63e9319..5f6e023 100644 --- a/examples/ec2-autoscaling/versions.tf +++ b/examples/ec2-autoscaling/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/examples/fargate/README.md b/examples/fargate/README.md index 5cf6f30..fed0c45 100644 --- a/examples/fargate/README.md +++ b/examples/fargate/README.md @@ -27,13 +27,13 @@ Note that this example may create resources which will incur monetary charges on | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules diff --git a/examples/fargate/versions.tf b/examples/fargate/versions.tf index 63e9319..5f6e023 100644 --- a/examples/fargate/versions.tf +++ b/examples/fargate/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/main.tf b/main.tf index f182907..c238b76 100644 --- a/main.tf +++ b/main.tf @@ -98,7 +98,8 @@ module "service" { # Task definition create_task_definition = try(each.value.create_task_definition, true) - task_definition_arn = lookup(each.value, "task_definition_arn", null) + task_definition_arn = try(each.value.task_definition_arn, null) + task_definition_track_latest = try(each.value.task_definition_track_latest, true) container_definitions = try(each.value.container_definitions, {}) container_definition_defaults = try(each.value.container_definition_defaults, {}) cpu = try(each.value.cpu, 1024) diff --git a/modules/cluster/README.md b/modules/cluster/README.md index f8c2cea..a3fa14a 100644 --- a/modules/cluster/README.md +++ b/modules/cluster/README.md @@ -137,13 +137,13 @@ module "ecs_cluster" { | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules diff --git a/modules/cluster/versions.tf b/modules/cluster/versions.tf index 63e9319..5f6e023 100644 --- a/modules/cluster/versions.tf +++ b/modules/cluster/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/modules/container-definition/README.md b/modules/container-definition/README.md index 5426afd..03154b8 100644 --- a/modules/container-definition/README.md +++ b/modules/container-definition/README.md @@ -116,13 +116,13 @@ module "example_ecs_container_definition" { | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules diff --git a/modules/container-definition/versions.tf b/modules/container-definition/versions.tf index 63e9319..5f6e023 100644 --- a/modules/container-definition/versions.tf +++ b/modules/container-definition/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/modules/service/README.md b/modules/service/README.md index 1a93616..259e7d1 100644 --- a/modules/service/README.md +++ b/modules/service/README.md @@ -167,13 +167,13 @@ module "ecs_service" { | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [aws](#requirement\_aws) | >= 5.34 | +| [aws](#requirement\_aws) | >= 5.37 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.34 | +| [aws](#provider\_aws) | >= 5.37 | ## Modules @@ -206,7 +206,6 @@ module "ecs_service" { | [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | | [aws_security_group_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | -| [aws_ecs_task_definition.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecs_task_definition) | data source | | [aws_iam_policy_document.service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.service_assume](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.task_exec](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | @@ -293,6 +292,7 @@ module "ecs_service" { | [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no | | [task\_definition\_arn](#input\_task\_definition\_arn) | Existing task definition ARN. Required when `create_task_definition` is `false` | `string` | `null` | no | | [task\_definition\_placement\_constraints](#input\_task\_definition\_placement\_constraints) | Configuration block for rules that are taken into consideration during task placement (up to max of 10). This is set at the task definition, see `placement_constraints` for setting at the service | `any` | `{}` | no | +| [task\_definition\_track\_latest](#input\_task\_definition\_track\_latest) | Whether to track the latest task definition revision in case of external changes | `bool` | `true` | no | | [task\_exec\_iam\_role\_arn](#input\_task\_exec\_iam\_role\_arn) | Existing IAM role ARN | `string` | `null` | no | | [task\_exec\_iam\_role\_description](#input\_task\_exec\_iam\_role\_description) | Description of the role | `string` | `null` | no | | [task\_exec\_iam\_role\_name](#input\_task\_exec\_iam\_role\_name) | Name to use on IAM role created | `string` | `null` | no | diff --git a/modules/service/main.tf b/modules/service/main.tf index c024b52..b0eb840 100644 --- a/modules/service/main.tf +++ b/modules/service/main.tf @@ -586,25 +586,7 @@ module "container_definition" { locals { create_task_definition = var.create && var.create_task_definition - # This allows us to query both the existing as well as Terraform's state and get - # and get the max version of either source, useful for when external resources - # update the container definition - max_task_def_revision = local.create_task_definition ? max(aws_ecs_task_definition.this[0].revision, data.aws_ecs_task_definition.this[0].revision) : 0 - task_definition = local.create_task_definition ? "${aws_ecs_task_definition.this[0].family}:${local.max_task_def_revision}" : var.task_definition_arn -} - -# This allows us to query both the existing as well as Terraform's state and get -# and get the max version of either source, useful for when external resources -# update the container definition -data "aws_ecs_task_definition" "this" { - count = local.create_task_definition ? 1 : 0 - - task_definition = aws_ecs_task_definition.this[0].family - - depends_on = [ - # Needs to exist first on first deployment - aws_ecs_task_definition.this - ] + task_definition = local.create_task_definition ? "${aws_ecs_task_definition.this[0].family}:${aws_ecs_task_definition.this[0].revision}" : var.task_definition_arn } resource "aws_ecs_task_definition" "this" { @@ -731,6 +713,8 @@ resource "aws_ecs_task_definition" "this" { } } + track_latest = var.task_definition_track_latest + tags = merge(var.tags, var.task_tags) lifecycle { diff --git a/modules/service/variables.tf b/modules/service/variables.tf index 795172c..4babd1c 100644 --- a/modules/service/variables.tf +++ b/modules/service/variables.tf @@ -268,6 +268,12 @@ variable "task_definition_arn" { default = null } +variable "task_definition_track_latest" { + description = "Whether to track the latest task definition revision in case of external changes" + type = bool + default = true +} + variable "container_definitions" { description = "A map of valid [container definitions](http://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html). Please note that you should only provide values that are part of the container definition document" type = any diff --git a/modules/service/versions.tf b/modules/service/versions.tf index 63e9319..5f6e023 100644 --- a/modules/service/versions.tf +++ b/modules/service/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/versions.tf b/versions.tf index 63e9319..5f6e023 100644 --- a/versions.tf +++ b/versions.tf @@ -4,7 +4,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 5.34" + version = ">= 5.37" } } } diff --git a/wrappers/service/main.tf b/wrappers/service/main.tf index d91e933..50e9e7c 100644 --- a/wrappers/service/main.tf +++ b/wrappers/service/main.tf @@ -97,6 +97,7 @@ module "wrapper" { tags = try(each.value.tags, var.defaults.tags, {}) task_definition_arn = try(each.value.task_definition_arn, var.defaults.task_definition_arn, null) task_definition_placement_constraints = try(each.value.task_definition_placement_constraints, var.defaults.task_definition_placement_constraints, {}) + task_definition_track_latest = try(each.value.task_definition_track_latest, var.defaults.task_definition_track_latest, true) task_exec_iam_role_arn = try(each.value.task_exec_iam_role_arn, var.defaults.task_exec_iam_role_arn, null) task_exec_iam_role_description = try(each.value.task_exec_iam_role_description, var.defaults.task_exec_iam_role_description, null) task_exec_iam_role_name = try(each.value.task_exec_iam_role_name, var.defaults.task_exec_iam_role_name, null)