Skip to content

Commit 499fed7

Browse files
authored
[fix] aws-ecs support not applying tags to services (#140)
1 parent 4920998 commit 499fed7

File tree

12 files changed

+79
-13
lines changed

12 files changed

+79
-13
lines changed

aws-ecs-job-fargate/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ container definition external to Terraform (e.g. using [czecs](https://github.co
1313
Terraform will use a stub definition, but from that point forward will ignore any
1414
changes to the definition, allowing external task definition management.
1515

16+
## Migrating old ECS services
17+
Older ECS services were created with an ARN in an old format that did not include the ECS cluster name as part of the ARN. AWS began allowing opt-in to the new ARN format starting November 15, 2018, and will require the new format starting January 1, 2020. ECS only allows applying tags (such as cost tags) on services that have the new ARN format. Applying tags to older ECS services using the old ARN format will return the following error message:
18+
```
19+
InvalidParameterException: Long arn format must be used for tagging operations
20+
```
21+
This module by default will assume your organization has opted in to the new ARN format and will apply tags to the ECS service. Creating new services after the opt-in will work fine, but migrating an existing older ECS service to using this module (via a state mv or an import) will encounter the above error message the next time it is applied.
22+
23+
Since changing a service to use the new ARN requires destroying and recreating the service, this can result in downtime. In such cases, you can opt-out applying tags by passing `tag_service = false` as an argument to the module. It is recommended that at the next possible down time, the ECS service be replaced by running `terraform taint`, and if `manage_task_definition = false` restoring the ECS task definition version (the taint/replace will restore to only the last stub definition). After the service is destroy/replaced, the `tag_service = false` argument can be removed.
24+
1625
<!-- START -->
1726
## Inputs
1827

@@ -30,6 +39,7 @@ changes to the definition, allowing external task definition management.
3039
| registry\_secretsmanager\_arn | ARN for AWS Secrets Manager secret for credentials to private registry | string | `null` | no |
3140
| security\_group\_ids | Security group to use for the Fargate task. | list | `<list>` | no |
3241
| service | Service for tagging and naming. See [doc](../README.md#consistent-tagging). | string | n/a | yes |
42+
| tag\_service | Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services. | bool | `true` | no |
3343
| task\_definition | JSON to describe task. If omitted, defaults to a stub task that is expected to be managed outside of Terraform. | string | `null` | no |
3444
| task\_role\_arn | | string | n/a | yes |
3545
| task\_subnets | Subnets to launch Fargate task in. | list | `<list>` | no |

aws-ecs-job-fargate/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ resource "aws_ecs_service" "job" {
3333
security_groups = var.security_group_ids
3434
}
3535

36-
tags = local.tags
36+
tags = var.tag_service ? local.tags : {}
3737
}
3838

3939
resource "aws_ecs_service" "unmanaged-job" {
@@ -57,7 +57,7 @@ resource "aws_ecs_service" "unmanaged-job" {
5757
ignore_changes = [task_definition]
5858
}
5959

60-
tags = local.tags
60+
tags = var.tag_service ? local.tags : {}
6161
}
6262

6363
# Default container definition if var.manage_task_definition == false

aws-ecs-job-fargate/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,10 @@ variable "manage_task_definition" {
8888
description = "If false, Terraform will not touch the task definition for the ECS service after initial creation"
8989
type = bool
9090
default = true
91+
}
92+
93+
variable "tag_service" {
94+
description = "Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services."
95+
type = bool
96+
default = true
9197
}

aws-ecs-job/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ container definition external to Terraform (e.g. using [czecs](https://github.co
1313
Terraform will use a stub definition, but from that point forward will ignore any
1414
changes to the definition, allowing external task definition management.
1515

16+
## Migrating old ECS services
17+
Older ECS services were created with an ARN in an old format that did not include the ECS cluster name as part of the ARN. AWS began allowing opt-in to the new ARN format starting November 15, 2018, and will require the new format starting January 1, 2020. ECS only allows applying tags (such as cost tags) on services that have the new ARN format. Applying tags to older ECS services using the old ARN format will return the following error message:
18+
```
19+
InvalidParameterException: Long arn format must be used for tagging operations
20+
```
21+
This module by default will assume your organization has opted in to the new ARN format and will apply tags to the ECS service. Creating new services after the opt-in will work fine, but migrating an existing older ECS service to using this module (via a state mv or an import) will encounter the above error message the next time it is applied.
22+
23+
Since changing a service to use the new ARN requires destroying and recreating the service, this can result in downtime. In such cases, you can opt-out applying tags by passing `tag_service = false` as an argument to the module. It is recommended that at the next possible down time, the ECS service be replaced by running `terraform taint`, and if `manage_task_definition = false` restoring the ECS task definition version (the taint/replace will restore to only the last stub definition). After the service is destroy/replaced, the `tag_
24+
service = false` argument can be removed.
25+
1626
<!-- START -->
1727
## Inputs
1828

@@ -27,6 +37,7 @@ changes to the definition, allowing external task definition management.
2737
| project | Project for tagging and naming. See [doc](../README.md#consistent-tagging) | string | n/a | yes |
2838
| scheduling\_strategy | Scheduling strategy for the service: REPLICA or DAEMON. | string | `"REPLICA"` | no |
2939
| service | Service for tagging and naming. See [doc](../README.md#consistent-tagging). | string | n/a | yes |
40+
| tag\_service | Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services. | bool | `true` | no |
3041
| task\_definition | JSON to describe task. If omitted, defaults to a stub task that is expected to be managed outside of Terraform. | string | `null` | no |
3142
| task\_role\_arn | | string | n/a | yes |
3243

aws-ecs-job/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ resource "aws_ecs_service" "job" {
2727
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
2828
scheduling_strategy = var.scheduling_strategy
2929

30-
tags = local.tags
30+
tags = var.tag_service ? local.tags : {}
3131
}
3232

3333
resource "aws_ecs_service" "unmanaged-job" {
@@ -45,7 +45,7 @@ resource "aws_ecs_service" "unmanaged-job" {
4545
ignore_changes = [task_definition]
4646
}
4747

48-
tags = local.tags
48+
tags = var.tag_service ? local.tags : {}
4949
}
5050

5151
# Default container definition if var.manage_task_definition == false

aws-ecs-job/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ variable "manage_task_definition" {
6969
description = "If false, Terraform will not touch the task definition for the ECS service after initial creation"
7070
type = bool
7171
default = true
72+
}
73+
74+
variable "tag_service" {
75+
description = "Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services."
76+
type = bool
77+
default = true
7278
}

aws-ecs-service-fargate/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ resource "aws_iam_role" "role" {
4242
}
4343
4444
module "role-policy" {
45-
source = "github.com/chanzuckerberg/cztack//aws-params-reader-policy?ref=v0.19.4"
45+
source = "github.com/chanzuckerberg/cztack//aws-params-reader-policy?ref=v0.21.3"
4646
project = var.project
4747
env = var.env
4848
service = var.component
@@ -90,7 +90,7 @@ data "aws_acm_certificate" "staging" {
9090
}
9191
9292
module "web-service" {
93-
source = "github.com/chanzuckerberg/cztack//aws-ecs-service-fargate?ref=v0.20.0"
93+
source = "github.com/chanzuckerberg/cztack//aws-ecs-service-fargate?ref=v0.21.3"
9494
9595
# this is the name of the service and many of the resources will have this name
9696
service = "myservice"
@@ -142,6 +142,16 @@ tasks with DNS via ECS service discovery. If with_service_discovery is true, a n
142142
DNS zone is created, and the tasks are registered in that DNS zone. The domain name is only
143143
resolvable from within the VPC; it is not publicly resolvable.
144144

145+
## Migrating old ECS services
146+
Older ECS services were created with an ARN in an old format that did not include the ECS cluster name as part of the ARN. AWS began allowing opt-in to the new ARN format starting November 15, 2018, and will require the new format starting January 1, 2020. ECS only allows applying tags (such as cost tags) on services that have the new ARN format. Applying tags to older ECS services using the old ARN format will return the following error message:
147+
```
148+
InvalidParameterException: Long arn format must be used for tagging operations
149+
```
150+
This module by default will assume your organization has opted in to the new ARN format and will apply tags to the ECS service. Creating new services after the opt-in will work fine, but migrating an existing older ECS service to using this module (via a state mv or an import) will encounter the above error message the next time it is applied.
151+
152+
Since changing a service to use the new ARN requires destroying and recreating the service, this can result in downtime. In such cases, you can opt-out applying tags by passing `tag_service = false` as an argument to the module. It is recommended that at the next possible down time, the ECS service be replaced by running `terraform taint`, and if `manage_task_definition = false` restoring the ECS task definition version (the taint/replace will restore to only the last stub definition). After the service is destroy/replaced, the `tag_
153+
service = false` argument can be removed.
154+
145155
<!-- START -->
146156
## Inputs
147157

@@ -176,6 +186,7 @@ resolvable from within the VPC; it is not publicly resolvable.
176186
| service | Service for tagging and naming. See [doc](../README.md#consistent-tagging). | string | n/a | yes |
177187
| ssl\_policy | ELB policy to determine which SSL/TLS encryption protocols are enabled. Probably don't touch this. | string | `null` | no |
178188
| subdomain | Subdomain in the zone. Final domain name will be subdomain.zone | string | n/a | yes |
189+
| tag\_service | Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services. | bool | `true` | no |
179190
| task\_definition | JSON to describe task. If omitted, defaults to a stub task that is expected to be managed outside of Terraform. | string | `null` | no |
180191
| task\_role\_arn | | string | n/a | yes |
181192
| task\_subnets | List of subnets in which to deploy the task for awsvpc networking mode. | list | `[]` | no |

aws-ecs-service-fargate/service.tf

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ resource "aws_ecs_service" "job" {
6666
}
6767
}
6868

69-
tags = local.tags
69+
tags = var.tag_service ? local.tags : {}
7070

7171
depends_on = [aws_lb.service]
7272
}
@@ -104,8 +104,7 @@ resource "aws_ecs_service" "unmanaged-job" {
104104
ignore_changes = [task_definition]
105105
}
106106

107-
tags = local.tags
108-
107+
tags = var.tag_service ? local.tags : {}
109108

110109
depends_on = [aws_lb.service]
111110
}

aws-ecs-service-fargate/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,10 @@ variable "manage_task_definition" {
189189
description = "If false, Terraform will not touch the task definition for the ECS service after initial creation"
190190
type = bool
191191
default = true
192+
}
193+
194+
variable "tag_service" {
195+
description = "Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services."
196+
type = bool
197+
default = true
192198
}

aws-ecs-service/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ resource "aws_iam_role" "role" {
3838
}
3939
4040
module "role-policy" {
41-
source = "github.com/chanzuckerberg/cztack//aws-params-reader-policy?ref=v0.19.4"
41+
source = "github.com/chanzuckerberg/cztack//aws-params-reader-policy?ref=v0.21.3"
4242
project = var.project
4343
env = var.env
4444
service = var.component
@@ -86,7 +86,7 @@ data "aws_acm_certificate" "staging" {
8686
}
8787
8888
module "web-service" {
89-
source = "github.com/chanzuckerberg/cztack//aws-ecs-service?ref=v0.20.0"
89+
source = "github.com/chanzuckerberg/cztack//aws-ecs-service?ref=v0.21.3"
9090
9191
# this is the name of the service and many of the resources will have this name
9292
service = "myservice"
@@ -134,6 +134,16 @@ tasks with DNS via ECS service discovery. If with_service_discovery is true, a n
134134
DNS zone is created, and the tasks are registered in that DNS zone. The domain name is only
135135
resolvable from within the VPC; it is not publicly resolvable.
136136

137+
## Migrating old ECS services
138+
Older ECS services were created with an ARN in an old format that did not include the ECS cluster name as part of the ARN. AWS began allowing opt-in to the new ARN format starting November 15, 2018, and will require the new format starting January 1, 2020. ECS only allows applying tags (such as cost tags) on services that have the new ARN format. Applying tags to older ECS services using the old ARN format will return the following error message:
139+
```
140+
InvalidParameterException: Long arn format must be used for tagging operations
141+
```
142+
This module by default will assume your organization has opted in to the new ARN format and will apply tags to the ECS service. Creating new services after the opt-in will work fine, but migrating an existing older ECS service to using this module (via a state mv or an import) will encounter the above error message the next time it is applied.
143+
144+
Since changing a service to use the new ARN requires destroying and recreating the service, this can result in downtime. In such cases, you can opt-out applying tags by passing `tag_service = false` as an argument to the module. It is recommended that at the next possible down time, the ECS service be replaced by running `terraform taint`, and if `manage_task_definition = false` restoring the ECS task definition version (the taint/replace will restore to only the last stub definition). After the service is destroy/replaced, the `tag_
145+
service = false` argument can be removed.
146+
137147
<!-- START -->
138148
## Inputs
139149

@@ -166,6 +176,7 @@ resolvable from within the VPC; it is not publicly resolvable.
166176
| service | Service for tagging and naming. See [doc](../README.md#consistent-tagging). | string | n/a | yes |
167177
| ssl\_policy | ELB policy to determine which SSL/TLS encryption protocols are enabled. Probably don't touch this. | string | `null` | no |
168178
| subdomain | Subdomain in the zone. Final domain name will be subdomain.zone | string | n/a | yes |
179+
| tag\_service | Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services. | bool | `true` | no |
169180
| task\_definition | JSON to describe task. If omitted, defaults to a stub task that is expected to be managed outside of Terraform. | string | `null` | no |
170181
| task\_role\_arn | | string | n/a | yes |
171182
| task\_subnets | List of subnets in which to deploy the task for awsvpc networking mode. Only used if awsvpc_network_mode is true. | list | `[]` | no |

aws-ecs-service/service.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ resource "aws_ecs_service" "job" {
6969
}
7070
}
7171

72-
tags = local.tags
72+
tags = var.tag_service ? local.tags : {}
7373

7474
depends_on = [aws_lb.service]
7575
}
@@ -109,7 +109,7 @@ resource "aws_ecs_service" "unmanaged-job" {
109109
ignore_changes = [task_definition]
110110
}
111111

112-
tags = local.tags
112+
tags = var.tag_service ? local.tags : {}
113113

114114
depends_on = [aws_lb.service]
115115
}

aws-ecs-service/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,10 @@ variable "manage_task_definition" {
183183
description = "If false, Terraform will not touch the task definition for the ECS service after initial creation"
184184
type = bool
185185
default = true
186+
}
187+
188+
variable "tag_service" {
189+
description = "Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services."
190+
type = bool
191+
default = true
186192
}

0 commit comments

Comments
 (0)