Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A Terraform module for creating and managing Amazon EKS (Elastic Kubernetes Serv
- **EKS Capabilities**: Support for ACK, KRO, and ArgoCD capabilities
- **AWS Load Balancer Controller**: Optional IAM role creation for AWS Load Balancer Controller (IRSA)
- **Security**: KMS encryption, IMDSv2 enforcement, security groups
- **CloudWatch Log Group**: Optional log group for EKS control plane logs; set `cloudwatch_log_group_force_destroy = true` to allow the log group to be deleted on `terraform destroy` (default is to protect it).

## Prerequisites

Expand Down
25 changes: 23 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ data "aws_caller_identity" "current" {}
# CloudWatch Log Group
################################################################################

resource "aws_cloudwatch_log_group" "this" {
count = var.create_cloudwatch_log_group ? 1 : 0
resource "aws_cloudwatch_log_group" "this_allow_destroy" {
count = var.create_cloudwatch_log_group && var.cloudwatch_log_group_force_destroy ? 1 : 0

region = var.region

Expand All @@ -38,6 +38,27 @@ resource "aws_cloudwatch_log_group" "this" {
)
}

resource "aws_cloudwatch_log_group" "this_prevent_destroy" {
count = var.create_cloudwatch_log_group && !var.cloudwatch_log_group_force_destroy ? 1 : 0

region = var.region

name = "/aws/eks/${var.name}/cluster"
retention_in_days = var.cloudwatch_log_group_retention_in_days
kms_key_id = var.cloudwatch_log_group_kms_key_id
log_group_class = var.cloudwatch_log_group_class

tags = merge(
var.tags,
var.cloudwatch_log_group_tags,
{ Name = "/aws/eks/${var.name}/cluster" }
)

lifecycle {
prevent_destroy = true
}
}

################################################################################
# KMS Key for EKS Cluster Encryption
################################################################################
Expand Down
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ output "access_policy_associations" {

output "cloudwatch_log_group_name" {
description = "Name of cloudwatch log group created"
value = try(aws_cloudwatch_log_group.this[0].name, null)
value = coalesce(try(aws_cloudwatch_log_group.this_allow_destroy[0].name, null), try(aws_cloudwatch_log_group.this_prevent_destroy[0].name, null))
}

output "cloudwatch_log_group_arn" {
description = "Arn of cloudwatch log group created"
value = try(aws_cloudwatch_log_group.this[0].arn, null)
value = coalesce(try(aws_cloudwatch_log_group.this_allow_destroy[0].arn, null), try(aws_cloudwatch_log_group.this_prevent_destroy[0].arn, null))
}

################################################################################
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ variable "cloudwatch_log_group_tags" {
default = {}
}

variable "cloudwatch_log_group_force_destroy" {
description = "When true, allow the CloudWatch log group to be deleted on terraform destroy. When false, protect it with lifecycle { prevent_destroy = true }."
type = bool
default = false
}

variable "region" {
description = "AWS region for CloudWatch log group"
type = string
Expand Down