Skip to content

Commit d8e144c

Browse files
authored
feat: Add support for creating cloudwatch log groups (#94)
1 parent 83d2fdd commit d8e144c

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ No modules.
197197

198198
| Name | Type |
199199
|------|------|
200+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
200201
| [aws_iam_role.scheduled_action](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
201202
| [aws_iam_role_policy.scheduled_action](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
202203
| [aws_redshift_authentication_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/redshift_authentication_profile) | resource |
@@ -225,10 +226,15 @@ No modules.
225226
| <a name="input_automated_snapshot_retention_period"></a> [automated\_snapshot\_retention\_period](#input\_automated\_snapshot\_retention\_period) | The number of days that automated snapshots are retained. If the value is 0, automated snapshots are disabled. Even if automated snapshots are disabled, you can still create manual snapshots when you want with create-cluster-snapshot. Default is 1 | `number` | `null` | no |
226227
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | The EC2 Availability Zone (AZ) in which you want Amazon Redshift to provision the cluster. Can only be changed if `availability_zone_relocation_enabled` is `true` | `string` | `null` | no |
227228
| <a name="input_availability_zone_relocation_enabled"></a> [availability\_zone\_relocation\_enabled](#input\_availability\_zone\_relocation\_enabled) | If `true`, the cluster can be relocated to another availability zone, either automatically by AWS or when requested. Default is `false`. Available for use on clusters from the RA3 instance family | `bool` | `null` | no |
229+
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | The ARN of the KMS Key to use when encrypting log data | `string` | `null` | no |
230+
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | The number of days to retain CloudWatch logs for the redshift cluster | `number` | `0` | no |
231+
| <a name="input_cloudwatch_log_group_skip_destroy"></a> [cloudwatch\_log\_group\_skip\_destroy](#input\_cloudwatch\_log\_group\_skip\_destroy) | Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state | `bool` | `null` | no |
232+
| <a name="input_cloudwatch_log_group_tags"></a> [cloudwatch\_log\_group\_tags](#input\_cloudwatch\_log\_group\_tags) | Additional tags to add to cloudwatch log groups created | `map(string)` | `{}` | no |
228233
| <a name="input_cluster_identifier"></a> [cluster\_identifier](#input\_cluster\_identifier) | The Cluster Identifier. Must be a lower case string | `string` | `""` | no |
229234
| <a name="input_cluster_timeouts"></a> [cluster\_timeouts](#input\_cluster\_timeouts) | Create, update, and delete timeout configurations for the cluster | `map(string)` | `{}` | no |
230235
| <a name="input_cluster_version"></a> [cluster\_version](#input\_cluster\_version) | The version of the Amazon Redshift engine software that you want to deploy on the cluster. The version selected runs on all the nodes in the cluster | `string` | `null` | no |
231236
| <a name="input_create"></a> [create](#input\_create) | Determines whether to create Redshift cluster and resources (affects all resources) | `bool` | `true` | no |
237+
| <a name="input_create_cloudwatch_log_group"></a> [create\_cloudwatch\_log\_group](#input\_create\_cloudwatch\_log\_group) | Determines whether a CloudWatch log group is created for each `var.logging.log_exports` | `bool` | `false` | no |
232238
| <a name="input_create_endpoint_access"></a> [create\_endpoint\_access](#input\_create\_endpoint\_access) | Determines whether to create an endpoint access (managed VPC endpoint) | `bool` | `false` | no |
233239
| <a name="input_create_parameter_group"></a> [create\_parameter\_group](#input\_create\_parameter\_group) | Determines whether to create a parameter group or use existing | `bool` | `true` | no |
234240
| <a name="input_create_random_password"></a> [create\_random\_password](#input\_create\_random\_password) | Determines whether to create random password for cluster `master_password` | `bool` | `true` | no |

examples/complete/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Note that this example may create resources which cost money. Run `terraform des
4343
| <a name="module_s3_logs"></a> [s3\_logs](#module\_s3\_logs) | terraform-aws-modules/s3-bucket/aws | ~> 3.0 |
4444
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws//modules/redshift | ~> 5.0 |
4545
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
46+
| <a name="module_with_cloudwatch_logging"></a> [with\_cloudwatch\_logging](#module\_with\_cloudwatch\_logging) | ../../ | n/a |
4647

4748
## Resources
4849

examples/complete/main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ resource "aws_redshift_snapshot_copy_grant" "useast1" {
202202
tags = local.tags
203203
}
204204

205+
################################################################################
206+
# Cloudwatch Logging
207+
################################################################################
208+
209+
module "with_cloudwatch_logging" {
210+
source = "../../"
211+
212+
cluster_identifier = "${local.name}-with-cloudwatch-logging"
213+
node_type = "dc2.large"
214+
215+
vpc_security_group_ids = [module.security_group.security_group_id]
216+
subnet_ids = module.vpc.redshift_subnets
217+
218+
create_cloudwatch_log_group = true
219+
cloudwatch_log_group_retention_in_days = 7
220+
logging = {
221+
enable = true
222+
log_destination_type = "cloudwatch"
223+
log_exports = ["connectionlog", "userlog", "useractivitylog"]
224+
}
225+
226+
tags = local.tags
227+
}
228+
205229
################################################################################
206230
# Default
207231
################################################################################

main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ resource "aws_redshift_cluster" "this" {
9898
lifecycle {
9999
ignore_changes = [master_password]
100100
}
101+
102+
depends_on = [aws_cloudwatch_log_group.this]
101103
}
102104

103105
################################################################################
@@ -319,3 +321,18 @@ resource "aws_redshift_authentication_profile" "this" {
319321
authentication_profile_name = try(each.value.name, each.key)
320322
authentication_profile_content = jsonencode(each.value.content)
321323
}
324+
325+
################################################################################
326+
# CloudWatch Log Group
327+
################################################################################
328+
329+
resource "aws_cloudwatch_log_group" "this" {
330+
for_each = toset([for log in try(var.logging.log_exports, []) : log if var.create && var.create_cloudwatch_log_group])
331+
332+
name = "/aws/redshift/cluster/${var.cluster_identifier}/${each.value}"
333+
retention_in_days = var.cloudwatch_log_group_retention_in_days
334+
kms_key_id = var.cloudwatch_log_group_kms_key_id
335+
skip_destroy = var.cloudwatch_log_group_skip_destroy
336+
337+
tags = merge(var.tags, var.cloudwatch_log_group_tags)
338+
}

variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,37 @@ variable "authentication_profiles" {
474474
type = any
475475
default = {}
476476
}
477+
478+
################################################################################
479+
# CloudWatch Log Group
480+
################################################################################
481+
482+
variable "create_cloudwatch_log_group" {
483+
description = "Determines whether a CloudWatch log group is created for each `var.logging.log_exports`"
484+
type = bool
485+
default = false
486+
}
487+
488+
variable "cloudwatch_log_group_retention_in_days" {
489+
description = "The number of days to retain CloudWatch logs for the redshift cluster"
490+
type = number
491+
default = 0
492+
}
493+
494+
variable "cloudwatch_log_group_kms_key_id" {
495+
description = "The ARN of the KMS Key to use when encrypting log data"
496+
type = string
497+
default = null
498+
}
499+
500+
variable "cloudwatch_log_group_skip_destroy" {
501+
description = "Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state"
502+
type = bool
503+
default = null
504+
}
505+
506+
variable "cloudwatch_log_group_tags" {
507+
description = "Additional tags to add to cloudwatch log groups created"
508+
type = map(string)
509+
default = {}
510+
}

0 commit comments

Comments
 (0)