Skip to content

Commit 9a8c7d3

Browse files
feat: Add support for cluster and container definition custom CloudWatch log group names (#160)
* feat: Custom CW Log Group name for cluster with cloudwatch_log_group_name var * feat: Custom CW Log Group name for ECS service containers
1 parent f7245ed commit 9a8c7d3

File tree

15 files changed

+39
-15
lines changed

15 files changed

+39
-15
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ No resources.
183183
|------|-------------|------|---------|:--------:|
184184
| <a name="input_autoscaling_capacity_providers"></a> [autoscaling\_capacity\_providers](#input\_autoscaling\_capacity\_providers) | Map of autoscaling capacity provider definitions to create for the cluster | `any` | `{}` | no |
185185
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no |
186+
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | Custom name of CloudWatch Log Group for ECS cluster | `string` | `null` | no |
186187
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events | `number` | `90` | no |
187188
| <a name="input_cloudwatch_log_group_tags"></a> [cloudwatch\_log\_group\_tags](#input\_cloudwatch\_log\_group\_tags) | A map of additional tags to add to the log group created | `map(string)` | `{}` | no |
188189
| <a name="input_cluster_configuration"></a> [cluster\_configuration](#input\_cluster\_configuration) | The execute command configuration for the cluster | `any` | `{}` | no |
@@ -214,8 +215,8 @@ No resources.
214215
| Name | Description |
215216
|------|-------------|
216217
| <a name="output_autoscaling_capacity_providers"></a> [autoscaling\_capacity\_providers](#output\_autoscaling\_capacity\_providers) | Map of autoscaling capacity providers created and their attributes |
217-
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
218-
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
218+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of CloudWatch log group created |
219+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of CloudWatch log group created |
219220
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | ARN that identifies the cluster |
220221
| <a name="output_cluster_capacity_providers"></a> [cluster\_capacity\_providers](#output\_cluster\_capacity\_providers) | Map of cluster capacity providers attributes |
221222
| <a name="output_cluster_id"></a> [cluster\_id](#output\_cluster\_id) | ID that identifies the cluster |

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module "cluster" {
1515

1616
# Cluster Cloudwatch log group
1717
create_cloudwatch_log_group = var.create_cloudwatch_log_group
18+
cloudwatch_log_group_name = var.cloudwatch_log_group_name
1819
cloudwatch_log_group_retention_in_days = var.cloudwatch_log_group_retention_in_days
1920
cloudwatch_log_group_kms_key_id = var.cloudwatch_log_group_kms_key_id
2021
cloudwatch_log_group_tags = var.cloudwatch_log_group_tags

modules/cluster/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ No modules.
168168
|------|-------------|------|---------|:--------:|
169169
| <a name="input_autoscaling_capacity_providers"></a> [autoscaling\_capacity\_providers](#input\_autoscaling\_capacity\_providers) | Map of autoscaling capacity provider definitions to create for the cluster | `any` | `{}` | no |
170170
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no |
171+
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | Custom name of CloudWatch Log Group for ECS cluster | `string` | `null` | no |
171172
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events | `number` | `90` | no |
172173
| <a name="input_cloudwatch_log_group_tags"></a> [cloudwatch\_log\_group\_tags](#input\_cloudwatch\_log\_group\_tags) | A map of additional tags to add to the log group created | `map(string)` | `{}` | no |
173174
| <a name="input_cluster_configuration"></a> [cluster\_configuration](#input\_cluster\_configuration) | The execute command configuration for the cluster | `any` | `{}` | no |
@@ -198,8 +199,8 @@ No modules.
198199
|------|-------------|
199200
| <a name="output_arn"></a> [arn](#output\_arn) | ARN that identifies the cluster |
200201
| <a name="output_autoscaling_capacity_providers"></a> [autoscaling\_capacity\_providers](#output\_autoscaling\_capacity\_providers) | Map of autoscaling capacity providers created and their attributes |
201-
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
202-
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
202+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of CloudWatch log group created |
203+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of CloudWatch log group created |
203204
| <a name="output_cluster_capacity_providers"></a> [cluster\_capacity\_providers](#output\_cluster\_capacity\_providers) | Map of cluster capacity providers attributes |
204205
| <a name="output_id"></a> [id](#output\_id) | ID that identifies the cluster |
205206
| <a name="output_name"></a> [name](#output\_name) | Name that identifies the cluster |

modules/cluster/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ resource "aws_ecs_cluster" "this" {
9393
################################################################################
9494
# CloudWatch Log Group
9595
################################################################################
96-
9796
resource "aws_cloudwatch_log_group" "this" {
9897
count = var.create && var.create_cloudwatch_log_group ? 1 : 0
9998

100-
name = "/aws/ecs/${var.cluster_name}"
99+
name = try(coalesce(var.cloudwatch_log_group_name, "/aws/ecs/${var.cluster_name}"), "")
101100
retention_in_days = var.cloudwatch_log_group_retention_in_days
102101
kms_key_id = var.cloudwatch_log_group_kms_key_id
103102

modules/cluster/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ output "name" {
2222
################################################################################
2323

2424
output "cloudwatch_log_group_name" {
25-
description = "Name of cloudwatch log group created"
25+
description = "Name of CloudWatch log group created"
2626
value = try(aws_cloudwatch_log_group.this[0].name, null)
2727
}
2828

2929
output "cloudwatch_log_group_arn" {
30-
description = "Arn of cloudwatch log group created"
30+
description = "ARN of CloudWatch log group created"
3131
value = try(aws_cloudwatch_log_group.this[0].arn, null)
3232
}
3333

modules/cluster/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ variable "create_cloudwatch_log_group" {
5353
default = true
5454
}
5555

56+
variable "cloudwatch_log_group_name" {
57+
description = "Custom name of CloudWatch Log Group for ECS cluster"
58+
type = string
59+
default = null
60+
}
61+
5662
variable "cloudwatch_log_group_retention_in_days" {
5763
description = "Number of days to retain log events"
5864
type = number

modules/container-definition/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ No modules.
140140
| Name | Description | Type | Default | Required |
141141
|------|-------------|------|---------|:--------:|
142142
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no |
143+
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | Custom name of CloudWatch log group for a service associated with the container definition | `string` | `null` | no |
143144
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events. Default is 30 days | `number` | `30` | no |
144145
| <a name="input_cloudwatch_log_group_use_name_prefix"></a> [cloudwatch\_log\_group\_use\_name\_prefix](#input\_cloudwatch\_log\_group\_use\_name\_prefix) | Determines whether the log group name should be used as a prefix | `bool` | `false` | no |
145146
| <a name="input_command"></a> [command](#input\_command) | The command that's passed to the container | `list(string)` | `[]` | no |
@@ -192,8 +193,8 @@ No modules.
192193

193194
| Name | Description |
194195
|------|-------------|
195-
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
196-
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
196+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of CloudWatch log group created |
197+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of CloudWatch log group created |
197198
| <a name="output_container_definition"></a> [container\_definition](#output\_container\_definition) | Container definition |
198199
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
199200

modules/container-definition/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ data "aws_region" "current" {}
33
locals {
44
is_not_windows = contains(["LINUX"], var.operating_system_family)
55

6-
log_group_name = "/aws/ecs/${var.service}/${var.name}"
6+
log_group_name = try(coalesce(var.cloudwatch_log_group_name, "/aws/ecs/${var.service}/${var.name}"), "")
77

88
log_configuration = merge(
99
{ for k, v in {

modules/container-definition/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ output "container_definition" {
1212
################################################################################
1313

1414
output "cloudwatch_log_group_name" {
15-
description = "Name of cloudwatch log group created"
15+
description = "Name of CloudWatch log group created"
1616
value = try(aws_cloudwatch_log_group.this[0].name, null)
1717
}
1818

1919
output "cloudwatch_log_group_arn" {
20-
description = "Arn of cloudwatch log group created"
20+
description = "ARN of CloudWatch log group created"
2121
value = try(aws_cloudwatch_log_group.this[0].arn, null)
2222
}

modules/container-definition/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ variable "create_cloudwatch_log_group" {
292292
default = true
293293
}
294294

295+
variable "cloudwatch_log_group_name" {
296+
description = "Custom name of CloudWatch log group for a service associated with the container definition"
297+
type = string
298+
default = null
299+
}
300+
295301
variable "cloudwatch_log_group_use_name_prefix" {
296302
description = "Determines whether the log group name should be used as a prefix"
297303
type = bool

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ output "cluster_name" {
1818
}
1919

2020
output "cloudwatch_log_group_name" {
21-
description = "Name of cloudwatch log group created"
21+
description = "Name of CloudWatch log group created"
2222
value = module.cluster.cloudwatch_log_group_name
2323
}
2424

2525
output "cloudwatch_log_group_arn" {
26-
description = "Arn of cloudwatch log group created"
26+
description = "ARN of CloudWatch log group created"
2727
value = module.cluster.cloudwatch_log_group_arn
2828
}
2929

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ variable "create_cloudwatch_log_group" {
5959
default = true
6060
}
6161

62+
variable "cloudwatch_log_group_name" {
63+
description = "Custom name of CloudWatch Log Group for ECS cluster"
64+
type = string
65+
default = null
66+
}
67+
6268
variable "cloudwatch_log_group_retention_in_days" {
6369
description = "Number of days to retain log events"
6470
type = number

wrappers/cluster/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module "wrapper" {
55

66
autoscaling_capacity_providers = try(each.value.autoscaling_capacity_providers, var.defaults.autoscaling_capacity_providers, {})
77
cloudwatch_log_group_kms_key_id = try(each.value.cloudwatch_log_group_kms_key_id, var.defaults.cloudwatch_log_group_kms_key_id, null)
8+
cloudwatch_log_group_name = try(each.value.cloudwatch_log_group_name, var.defaults.cloudwatch_log_group_name, null)
89
cloudwatch_log_group_retention_in_days = try(each.value.cloudwatch_log_group_retention_in_days, var.defaults.cloudwatch_log_group_retention_in_days, 90)
910
cloudwatch_log_group_tags = try(each.value.cloudwatch_log_group_tags, var.defaults.cloudwatch_log_group_tags, {})
1011
cluster_configuration = try(each.value.cluster_configuration, var.defaults.cluster_configuration, {})

wrappers/container-definition/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module "wrapper" {
44
for_each = var.items
55

66
cloudwatch_log_group_kms_key_id = try(each.value.cloudwatch_log_group_kms_key_id, var.defaults.cloudwatch_log_group_kms_key_id, null)
7+
cloudwatch_log_group_name = try(each.value.cloudwatch_log_group_name, var.defaults.cloudwatch_log_group_name, null)
78
cloudwatch_log_group_retention_in_days = try(each.value.cloudwatch_log_group_retention_in_days, var.defaults.cloudwatch_log_group_retention_in_days, 30)
89
cloudwatch_log_group_use_name_prefix = try(each.value.cloudwatch_log_group_use_name_prefix, var.defaults.cloudwatch_log_group_use_name_prefix, false)
910
command = try(each.value.command, var.defaults.command, [])

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module "wrapper" {
55

66
autoscaling_capacity_providers = try(each.value.autoscaling_capacity_providers, var.defaults.autoscaling_capacity_providers, {})
77
cloudwatch_log_group_kms_key_id = try(each.value.cloudwatch_log_group_kms_key_id, var.defaults.cloudwatch_log_group_kms_key_id, null)
8+
cloudwatch_log_group_name = try(each.value.cloudwatch_log_group_name, var.defaults.cloudwatch_log_group_name, null)
89
cloudwatch_log_group_retention_in_days = try(each.value.cloudwatch_log_group_retention_in_days, var.defaults.cloudwatch_log_group_retention_in_days, 90)
910
cloudwatch_log_group_tags = try(each.value.cloudwatch_log_group_tags, var.defaults.cloudwatch_log_group_tags, {})
1011
cluster_configuration = try(each.value.cluster_configuration, var.defaults.cluster_configuration, {})

0 commit comments

Comments
 (0)