Skip to content

Commit fe5d295

Browse files
committed
feat: Add support for creating the CloudWatch log group
1 parent 9979b15 commit fe5d295

File tree

10 files changed

+95
-7
lines changed

10 files changed

+95
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ No modules.
4747

4848
| Name | Type |
4949
|------|------|
50+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
5051
| [aws_elasticache_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_cluster) | resource |
5152
| [aws_elasticache_parameter_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_parameter_group) | resource |
5253
| [aws_elasticache_replication_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_replication_group) | resource |
@@ -82,7 +83,7 @@ No modules.
8283
| <a name="input_global_replication_group_id"></a> [global\_replication\_group\_id](#input\_global\_replication\_group\_id) | The ID of the global replication group to which this replication group should belong | `string` | `null` | no |
8384
| <a name="input_ip_discovery"></a> [ip\_discovery](#input\_ip\_discovery) | The IP version to advertise in the discovery protocol. Valid values are `ipv4` or `ipv6` | `string` | `null` | no |
8485
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | The ARN of the key that you wish to use if encrypting at rest. If not supplied, uses service managed encryption. Can be specified only if `at_rest_encryption_enabled = true` | `string` | `null` | no |
85-
| <a name="input_log_delivery_configuration"></a> [log\_delivery\_configuration](#input\_log\_delivery\_configuration) | (Redis only) Specifies the destination and format of Redis SLOWLOG or Redis Engine Log | `list(map(string))` | `[]` | no |
86+
| <a name="input_log_delivery_configuration"></a> [log\_delivery\_configuration](#input\_log\_delivery\_configuration) | (Redis only) Specifies the destination and format of Redis SLOWLOG or Redis Engine Log | `any` | <pre>{<br> "slow-log": {<br> "destination_type": "cloudwatch-logs",<br> "log_format": "json"<br> }<br>}</pre> | no |
8687
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Specifies the weekly time range for when maintenance on the cache cluster is performed. The format is `ddd:hh24:mi-ddd:hh24:mi` (24H Clock UTC) | `string` | `null` | no |
8788
| <a name="input_multi_az_enabled"></a> [multi\_az\_enabled](#input\_multi\_az\_enabled) | Specifies whether to enable Multi-AZ Support for the replication group. If true, `automatic_failover_enabled` must also be enabled. Defaults to `false` | `bool` | `null` | no |
8889
| <a name="input_network_type"></a> [network\_type](#input\_network\_type) | The IP versions for cache cluster connections. Valid values are `ipv4`, `ipv6` or `dual_stack` | `string` | `null` | no |
@@ -125,6 +126,8 @@ No modules.
125126

126127
| Name | Description |
127128
|------|-------------|
129+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
130+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
128131
| <a name="output_cluster_address"></a> [cluster\_address](#output\_cluster\_address) | (Memcached only) DNS name of the cache cluster without the port appended |
129132
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | The ARN of the ElastiCache Cluster |
130133
| <a name="output_cluster_cache_nodes"></a> [cluster\_cache\_nodes](#output\_cluster\_cache\_nodes) | List of node objects including `id`, `address`, `port` and `availability_zone` |

examples/memcached-cluster/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ No inputs.
5050

5151
| Name | Description |
5252
|------|-------------|
53+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
54+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
5355
| <a name="output_cluster_address"></a> [cluster\_address](#output\_cluster\_address) | (Memcached only) DNS name of the cache cluster without the port appended |
5456
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | The ARN of the ElastiCache Cluster |
5557
| <a name="output_cluster_cache_nodes"></a> [cluster\_cache\_nodes](#output\_cluster\_cache\_nodes) | List of node objects including `id`, `address`, `port` and `availability_zone` |

examples/memcached-cluster/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ output "rep_group_reader_endpoint_address" {
6666
value = module.elasticache.rep_group_reader_endpoint_address
6767
}
6868

69+
################################################################################
70+
# CloudWatch Log Group
71+
################################################################################
72+
73+
output "cloudwatch_log_group_name" {
74+
description = "Name of cloudwatch log group created"
75+
value = module.elasticache.cloudwatch_log_group_name
76+
}
77+
78+
output "cloudwatch_log_group_arn" {
79+
description = "Arn of cloudwatch log group created"
80+
value = module.elasticache.cloudwatch_log_group_arn
81+
}
82+
6983
################################################################################
7084
# Parameter Group
7185
################################################################################

examples/redis-cluster/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ No inputs.
5050

5151
| Name | Description |
5252
|------|-------------|
53+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
54+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
5355
| <a name="output_cluster_address"></a> [cluster\_address](#output\_cluster\_address) | (Memcached only) DNS name of the cache cluster without the port appended |
5456
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | The ARN of the ElastiCache Cluster |
5557
| <a name="output_cluster_cache_nodes"></a> [cluster\_cache\_nodes](#output\_cluster\_cache\_nodes) | List of node objects including `id`, `address`, `port` and `availability_zone` |

examples/redis-cluster/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ output "rep_group_reader_endpoint_address" {
6666
value = module.elasticache.rep_group_reader_endpoint_address
6767
}
6868

69+
################################################################################
70+
# CloudWatch Log Group
71+
################################################################################
72+
73+
output "cloudwatch_log_group_name" {
74+
description = "Name of cloudwatch log group created"
75+
value = module.elasticache.cloudwatch_log_group_name
76+
}
77+
78+
output "cloudwatch_log_group_arn" {
79+
description = "Arn of cloudwatch log group created"
80+
value = module.elasticache.cloudwatch_log_group_arn
81+
}
82+
6983
################################################################################
7084
# Parameter Group
7185
################################################################################

examples/redis-replication-group/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ No inputs.
4949

5050
| Name | Description |
5151
|------|-------------|
52+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
53+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
5254
| <a name="output_cluster_address"></a> [cluster\_address](#output\_cluster\_address) | (Memcached only) DNS name of the cache cluster without the port appended |
5355
| <a name="output_cluster_arn"></a> [cluster\_arn](#output\_cluster\_arn) | The ARN of the ElastiCache Cluster |
5456
| <a name="output_cluster_cache_nodes"></a> [cluster\_cache\_nodes](#output\_cluster\_cache\_nodes) | List of node objects including `id`, `address`, `port` and `availability_zone` |

examples/redis-replication-group/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ output "rep_group_reader_endpoint_address" {
6666
value = module.elasticache.rep_group_reader_endpoint_address
6767
}
6868

69+
################################################################################
70+
# CloudWatch Log Group
71+
################################################################################
72+
73+
output "cloudwatch_log_group_name" {
74+
description = "Name of cloudwatch log group created"
75+
value = module.elasticache.cloudwatch_log_group_name
76+
}
77+
78+
output "cloudwatch_log_group_arn" {
79+
description = "Arn of cloudwatch log group created"
80+
value = module.elasticache.cloudwatch_log_group_arn
81+
}
82+
6983
################################################################################
7084
# Parameter Group
7185
################################################################################

main.tf

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ resource "aws_elasticache_cluster" "this" {
2626
ip_discovery = var.ip_discovery
2727

2828
dynamic "log_delivery_configuration" {
29-
for_each = var.log_delivery_configuration
29+
for_each = { for k, v in var.log_delivery_configuration : k => v if var.engine == "redis" }
3030

3131
content {
32-
destination = log_delivery_configuration.value.destination
32+
destination = try(v.create_cloudwatch_log_group, true) && log_delivery_configuration.value.destination_type == "cloudwatch-logs" ? aws_cloudwatch_log_group.this[each.key].name : log_delivery_configuration.value.destination
3333
destination_type = log_delivery_configuration.value.destination_type
3434
log_format = log_delivery_configuration.value.log_format
3535
log_type = try(log_delivery_configuration.value.log_type, each.key)
@@ -86,10 +86,10 @@ resource "aws_elasticache_replication_group" "this" {
8686
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_arn : null
8787

8888
dynamic "log_delivery_configuration" {
89-
for_each = var.log_delivery_configuration
89+
for_each = { for k, v in var.log_delivery_configuration : k => v if var.engine == "redis" }
9090

9191
content {
92-
destination = log_delivery_configuration.value.destination
92+
destination = try(v.create_cloudwatch_log_group, true) && log_delivery_configuration.value.destination_type == "cloudwatch-logs" ? aws_cloudwatch_log_group.this[each.key].name : log_delivery_configuration.value.destination
9393
destination_type = log_delivery_configuration.value.destination_type
9494
log_format = log_delivery_configuration.value.log_format
9595
log_type = try(log_delivery_configuration.value.log_type, each.key)
@@ -121,6 +121,24 @@ resource "aws_elasticache_replication_group" "this" {
121121
tags = local.tags
122122
}
123123

124+
################################################################################
125+
# Cloudwatch Log Group
126+
################################################################################
127+
128+
locals {
129+
create_cloudwatch_log_group = var.create && var.engine == "redis"
130+
}
131+
132+
resource "aws_cloudwatch_log_group" "this" {
133+
for_each = { for k, v in var.log_delivery_configuration : k => v if local.create_cloudwatch_log_group && try(v.create_cloudwatch_log_group, true) && try(v.destination_type, "") == "cloudwatch-logs" }
134+
135+
name = "/aws/elasticache/${try(each.value.cloudwatch_log_group_name, coalesce(var.cluster_id, var.replication_group_id), "")}"
136+
retention_in_days = try(each.value.cloudwatch_log_group_retention_in_days, 14)
137+
kms_key_id = try(each.value.cloudwatch_log_group_kms_key_id, null)
138+
139+
tags = merge(local.tags, try(each.value.tags, {}))
140+
}
141+
124142
################################################################################
125143
# Parameter Group
126144
################################################################################

outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ output "rep_group_reader_endpoint_address" {
6666
value = try(aws_elasticache_replication_group.this[0].reader_endpoint_address, null)
6767
}
6868

69+
################################################################################
70+
# CloudWatch Log Group
71+
################################################################################
72+
73+
output "cloudwatch_log_group_name" {
74+
description = "Name of cloudwatch log group created"
75+
value = try(aws_cloudwatch_log_group.this[0].name, null)
76+
}
77+
78+
output "cloudwatch_log_group_arn" {
79+
description = "Arn of cloudwatch log group created"
80+
value = try(aws_cloudwatch_log_group.this[0].arn, null)
81+
}
82+
6983
################################################################################
7084
# Parameter Group
7185
################################################################################

variables.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ variable "ip_discovery" {
8282

8383
variable "log_delivery_configuration" {
8484
description = "(Redis only) Specifies the destination and format of Redis SLOWLOG or Redis Engine Log"
85-
type = list(map(string))
86-
default = []
85+
type = any
86+
default = {
87+
slow-log = {
88+
destination_type = "cloudwatch-logs"
89+
log_format = "json"
90+
}
91+
}
8792
}
8893

8994
variable "maintenance_window" {

0 commit comments

Comments
 (0)