Skip to content

Commit 66b2ef3

Browse files
committed
Add Loki and Monitoring Support in EKS Module
- Introduced support for Loki by adding an IRSA role and IAM policy for S3 access in the comet_eks module. - Added variables for enabling Loki and configuring the S3 bucket for Loki logs. - Implemented monitoring setup with a dedicated namespace and Grafana credentials management. - Updated outputs to include ARNs and names for the new Loki resources. - Enhanced the comet_s3 module to create an S3 bucket for Loki logs based on configuration settings.
1 parent 7327062 commit 66b2ef3

9 files changed

Lines changed: 235 additions & 1 deletion

File tree

main.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ module "comet_eks" {
160160
# External Secrets IRSA and Helm chart
161161
enable_external_secrets = var.eks_enable_external_secrets
162162
external_secrets_chart_version = var.eks_external_secrets_chart_version
163+
164+
# Loki IRSA for S3 access
165+
enable_loki = var.enable_loki_bucket
166+
loki_s3_bucket_arn = var.enable_s3 && var.enable_loki_bucket ? module.comet_s3[0].comet_loki_bucket_arn : null
167+
168+
# Monitoring namespace and Grafana credentials
169+
enable_monitoring_setup = var.enable_monitoring_setup
170+
monitoring_namespace = var.monitoring_namespace
171+
grafana_admin_user = var.grafana_admin_user
172+
grafana_admin_password = var.grafana_admin_password
163173
}
164174

165175
module "comet_elasticache" {
@@ -218,7 +228,8 @@ module "comet_s3" {
218228
comet_s3_bucket = var.s3_bucket_name
219229
s3_force_destroy = var.s3_force_destroy
220230

221-
enable_mpm_infra = var.enable_mpm_infra
231+
enable_mpm_infra = var.enable_mpm_infra
232+
enable_loki_bucket = var.enable_loki_bucket
222233
}
223234

224235
module "comet_secretsmanager" {

modules/comet_eks/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,4 +471,107 @@ resource "kubernetes_manifest" "cluster_secret_store" {
471471
depends_on = [
472472
helm_release.external_secrets
473473
]
474+
}
475+
476+
#########################################
477+
#### Loki IRSA Role and IAM Policy ####
478+
#########################################
479+
data "aws_iam_policy_document" "loki" {
480+
count = var.enable_loki ? 1 : 0
481+
482+
statement {
483+
actions = [
484+
"s3:ListBucket",
485+
"s3:PutObject",
486+
"s3:GetObject",
487+
"s3:DeleteObject",
488+
]
489+
resources = [
490+
var.loki_s3_bucket_arn,
491+
"${var.loki_s3_bucket_arn}/*"
492+
]
493+
}
494+
}
495+
496+
resource "aws_iam_policy" "loki" {
497+
count = var.enable_loki ? 1 : 0
498+
499+
name_prefix = "${var.environment}-loki-"
500+
description = "Provides permissions for Loki on ${var.environment} cluster"
501+
policy = data.aws_iam_policy_document.loki[0].json
502+
503+
tags = merge(
504+
var.common_tags,
505+
{
506+
Name = "${var.environment}-loki"
507+
}
508+
)
509+
}
510+
511+
module "loki_irsa_role" {
512+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
513+
version = "~> 5.39"
514+
515+
count = var.enable_loki ? 1 : 0
516+
517+
role_name = "${var.environment}-loki"
518+
519+
role_policy_arns = {
520+
loki = aws_iam_policy.loki[0].arn
521+
}
522+
523+
oidc_providers = {
524+
ex = {
525+
provider_arn = module.eks.oidc_provider_arn
526+
namespace_service_accounts = ["monitoring:monitoring-loki"]
527+
}
528+
}
529+
530+
depends_on = [
531+
module.eks,
532+
aws_iam_policy.loki
533+
]
534+
535+
tags = merge(
536+
var.common_tags,
537+
{
538+
Name = "${var.environment}-loki"
539+
Description = "IRSA role for Loki to access S3 bucket for log storage"
540+
}
541+
)
542+
}
543+
544+
#########################################
545+
#### Monitoring Namespace and Secrets ####
546+
#########################################
547+
resource "kubernetes_namespace" "monitoring" {
548+
count = var.enable_monitoring_setup ? 1 : 0
549+
550+
metadata {
551+
name = var.monitoring_namespace
552+
}
553+
554+
depends_on = [
555+
module.eks,
556+
module.eks_blueprints_addons
557+
]
558+
}
559+
560+
resource "kubernetes_secret" "monitoring" {
561+
count = var.enable_monitoring_setup ? 1 : 0
562+
563+
metadata {
564+
name = "monitoring"
565+
namespace = kubernetes_namespace.monitoring[0].metadata[0].name
566+
}
567+
568+
data = {
569+
grafana-admin-user = var.grafana_admin_user
570+
grafana-admin-password = var.grafana_admin_password
571+
}
572+
573+
type = "Opaque"
574+
immutable = false
575+
576+
depends_on = [kubernetes_namespace.monitoring]
474577
}

modules/comet_eks/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ output "external_secrets_irsa_role_name" {
3131
output "oidc_provider_arn" {
3232
description = "ARN of the OIDC provider for the EKS cluster"
3333
value = module.eks.oidc_provider_arn
34+
}
35+
36+
output "loki_irsa_role_arn" {
37+
description = "ARN of the Loki IRSA role for S3 access"
38+
value = var.enable_loki ? module.loki_irsa_role[0].iam_role_arn : null
39+
}
40+
41+
output "loki_irsa_role_name" {
42+
description = "Name of the Loki IRSA role"
43+
value = var.enable_loki ? module.loki_irsa_role[0].iam_role_name : null
3444
}

modules/comet_eks/variables.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,43 @@ variable "enable_external_secrets" {
221221
default = true
222222
}
223223

224+
variable "enable_loki" {
225+
description = "Enable Loki IRSA role for accessing S3 bucket for log storage"
226+
type = bool
227+
default = false
228+
}
229+
230+
variable "loki_s3_bucket_arn" {
231+
description = "ARN of the S3 bucket for Loki log storage"
232+
type = string
233+
default = null
234+
}
235+
236+
variable "enable_monitoring_setup" {
237+
description = "Enable monitoring namespace and Grafana credentials secret"
238+
type = bool
239+
default = false
240+
}
241+
242+
variable "monitoring_namespace" {
243+
description = "Kubernetes namespace for monitoring resources"
244+
type = string
245+
default = "monitoring"
246+
}
247+
248+
variable "grafana_admin_user" {
249+
description = "Grafana admin username"
250+
type = string
251+
default = "admin"
252+
}
253+
254+
variable "grafana_admin_password" {
255+
description = "Grafana admin password"
256+
type = string
257+
sensitive = true
258+
default = null
259+
}
260+
224261
# Druid Node Group Variables
225262
variable "eks_druid_name" {
226263
description = "Name for the druid node group"

modules/comet_s3/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ resource "aws_s3_bucket" "comet_airflow_bucket" {
4545
)
4646
}
4747

48+
resource "aws_s3_bucket" "comet_loki_bucket" {
49+
count = var.enable_loki_bucket ? 1 : 0
50+
51+
bucket = "comet-loki-${local.suffix}"
52+
53+
force_destroy = var.s3_force_destroy
54+
55+
tags = merge(
56+
var.common_tags,
57+
{
58+
Name = "comet-loki-${local.suffix}"
59+
}
60+
)
61+
}
62+
4863
resource "aws_iam_policy" "comet_s3_iam_policy" {
4964
name = "comet-s3-access-policy-${local.suffix}"
5065
description = "Policy for access to comet S3 buckets"
@@ -65,6 +80,10 @@ resource "aws_iam_policy" "comet_s3_iam_policy" {
6580
"${aws_s3_bucket.comet_druid_bucket[0].arn}/*",
6681
aws_s3_bucket.comet_airflow_bucket[0].arn,
6782
"${aws_s3_bucket.comet_airflow_bucket[0].arn}/*"
83+
] : [],
84+
var.enable_loki_bucket ? [
85+
aws_s3_bucket.comet_loki_bucket[0].arn,
86+
"${aws_s3_bucket.comet_loki_bucket[0].arn}/*"
6887
] : []
6988
)
7089
}

modules/comet_s3/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
output "comet_s3_iam_policy_arn" {
22
description = "ARN of the IAM policy granting access to the provisioned bucket(s)"
33
value = aws_iam_policy.comet_s3_iam_policy.arn
4+
}
5+
6+
output "comet_loki_bucket_name" {
7+
description = "Name of the Loki S3 bucket"
8+
value = var.enable_loki_bucket ? aws_s3_bucket.comet_loki_bucket[0].id : null
9+
}
10+
11+
output "comet_loki_bucket_arn" {
12+
description = "ARN of the Loki S3 bucket"
13+
value = var.enable_loki_bucket ? aws_s3_bucket.comet_loki_bucket[0].arn : null
414
}

modules/comet_s3/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ variable "enable_mpm_infra" {
1818
type = bool
1919
}
2020

21+
variable "enable_loki_bucket" {
22+
description = "Enable creation of S3 bucket for Loki logs"
23+
type = bool
24+
default = false
25+
}
26+
2127
variable "common_tags" {
2228
type = map(string)
2329
description = "A map of common tags"

outputs.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,24 @@ output "external_secrets_irsa_role_arn" {
8484
output "external_secrets_irsa_role_name" {
8585
description = "Name of the External Secrets IRSA role"
8686
value = var.enable_eks && var.eks_enable_external_secrets ? module.comet_eks[0].external_secrets_irsa_role_name : null
87+
}
88+
89+
output "comet_loki_bucket_name" {
90+
description = "Name of the Loki S3 bucket"
91+
value = var.enable_s3 && var.enable_loki_bucket ? module.comet_s3[0].comet_loki_bucket_name : null
92+
}
93+
94+
output "comet_loki_bucket_arn" {
95+
description = "ARN of the Loki S3 bucket"
96+
value = var.enable_s3 && var.enable_loki_bucket ? module.comet_s3[0].comet_loki_bucket_arn : null
97+
}
98+
99+
output "loki_irsa_role_arn" {
100+
description = "ARN of the Loki IRSA role for S3 access"
101+
value = var.enable_eks && var.enable_loki_bucket ? module.comet_eks[0].loki_irsa_role_arn : null
102+
}
103+
104+
output "loki_irsa_role_name" {
105+
description = "Name of the Loki IRSA role"
106+
value = var.enable_eks && var.enable_loki_bucket ? module.comet_eks[0].loki_irsa_role_name : null
87107
}

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ variable "enable_mpm_infra" {
4242
default = false
4343
}
4444

45+
variable "enable_loki_bucket" {
46+
description = "Enable creation of S3 bucket for Loki logs (used by comet_s3 module)"
47+
type = bool
48+
default = false
49+
}
50+
51+
variable "enable_monitoring_setup" {
52+
description = "Enable monitoring namespace and Grafana credentials secret in EKS (used by comet_eks module)"
53+
type = bool
54+
default = false
55+
}
56+
57+
variable "monitoring_namespace" {
58+
description = "Kubernetes namespace for monitoring resources"
59+
type = string
60+
default = "monitoring"
61+
}
62+
4563
variable "enable_secretsmanager" {
4664
description = "Toggles the comet_secretsmanager module for provisioning Comet Secrets Manager secrets. Requires enable_rds and enable_elasticache to be true."
4765
type = bool

0 commit comments

Comments
 (0)