From 1be0b5e325f6ac458773c7eddc469397b57795a5 Mon Sep 17 00:00:00 2001 From: Vara Bonthu Date: Fri, 21 Jul 2023 17:58:25 +0100 Subject: [PATCH] feat: Allowing Custom CloudWatch Log Group Name or Prefix (#13) --- modules/virtual-cluster/README.md | 3 +++ modules/virtual-cluster/main.tf | 9 ++++++--- modules/virtual-cluster/variables.tf | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/modules/virtual-cluster/README.md b/modules/virtual-cluster/README.md index 000abde..7f285d4 100644 --- a/modules/virtual-cluster/README.md +++ b/modules/virtual-cluster/README.md @@ -121,7 +121,10 @@ No modules. | [annotations](#input\_annotations) | A map of annotations to add to all Kubernetes resources | `map(string)` | `{}` | no | | [cloudwatch\_log\_group\_arn](#input\_cloudwatch\_log\_group\_arn) | ARN of the log group to use for the cluster logs | `string` | `"arn:aws:logs:*:*:*"` | no | | [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 | +| [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | The name of the log group. If a name is not provided, the default name format used is: `/emr-on-eks-logs/emr-workload/` | `string` | `null` | no | | [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events. Default retention - 7 days | `number` | `7` | no | +| [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 | +| [cloudwatch\_log\_group\_use\_name\_prefix](#input\_cloudwatch\_log\_group\_use\_name\_prefix) | Determines whether the log group name (`cloudwatch_log_group_name`) is used as a prefix | `bool` | `false` | no | | [create](#input\_create) | Controls if resources should be created (affects nearly all resources) | `bool` | `true` | no | | [create\_cloudwatch\_log\_group](#input\_create\_cloudwatch\_log\_group) | Determines whether a log group is created by this module for the cluster logs. If not, AWS will automatically create one if logging is enabled | `bool` | `true` | no | | [create\_iam\_role](#input\_create\_iam\_role) | Determines whether an IAM role is created for EMR on EKS job execution role | `bool` | `true` | no | diff --git a/modules/virtual-cluster/main.tf b/modules/virtual-cluster/main.tf index 8000f6c..0a380c8 100644 --- a/modules/virtual-cluster/main.tf +++ b/modules/virtual-cluster/main.tf @@ -6,8 +6,9 @@ locals { internal_role_name = try(coalesce(var.role_name, var.name), "") - role_name = var.create_kubernetes_role ? kubernetes_role_v1.this[0].metadata[0].name : local.internal_role_name - namespace = var.create_namespace ? kubernetes_namespace_v1.this[0].metadata[0].name : var.namespace + role_name = var.create_kubernetes_role ? kubernetes_role_v1.this[0].metadata[0].name : local.internal_role_name + namespace = var.create_namespace ? kubernetes_namespace_v1.this[0].metadata[0].name : var.namespace + cloudwatch_log_group_name = coalesce(var.cloudwatch_log_group_name, "/emr-on-eks-logs/emr-workload/${local.namespace}") tags = merge(var.tags, { terraform-aws-modules = "emr" }) } @@ -269,9 +270,11 @@ resource "aws_iam_role_policy_attachment" "additional" { resource "aws_cloudwatch_log_group" "this" { count = var.create && var.create_cloudwatch_log_group ? 1 : 0 - name = "/emr-on-eks-logs/emr-workload/${local.namespace}" + name = var.cloudwatch_log_group_use_name_prefix ? null : local.cloudwatch_log_group_name + name_prefix = var.cloudwatch_log_group_use_name_prefix ? "${local.cloudwatch_log_group_name}-" : null retention_in_days = var.cloudwatch_log_group_retention_in_days kms_key_id = var.cloudwatch_log_group_kms_key_id + skip_destroy = var.cloudwatch_log_group_skip_destroy tags = local.tags } diff --git a/modules/virtual-cluster/variables.tf b/modules/virtual-cluster/variables.tf index 7647cff..042c10e 100644 --- a/modules/virtual-cluster/variables.tf +++ b/modules/virtual-cluster/variables.tf @@ -145,3 +145,21 @@ variable "cloudwatch_log_group_kms_key_id" { type = string default = null } + +variable "cloudwatch_log_group_name" { + description = "The name of the log group. If a name is not provided, the default name format used is: `/emr-on-eks-logs/emr-workload/`" + type = string + default = null +} + +variable "cloudwatch_log_group_use_name_prefix" { + description = "Determines whether the log group name (`cloudwatch_log_group_name`) is used as a prefix" + type = bool + default = false +} + +variable "cloudwatch_log_group_skip_destroy" { + 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" + type = bool + default = null +}