Skip to content

Commit f678c15

Browse files
committed
feat(eks)!: add option to create IAM role for the metrics storage
This commit solves ISDEVOPS-279 and ISDEVOPS-283 for the EKS variants.
1 parent 1b6dce5 commit f678c15

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

eks/extra-variables.tf

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
variable "metrics_storage" {
22
description = "AWS S3 bucket configuration values for the bucket where the archived metrics will be stored."
33
type = object({
4-
bucket_id = string
5-
region = string
6-
iam_role_arn = string
4+
bucket_id = string
5+
create_role = bool
6+
iam_role_arn = optional(string, null)
7+
cluster_oidc_issuer_url = optional(string, null)
78
})
9+
810
default = null
11+
12+
validation {
13+
# We use the try() function to avoid errors here when we deactivate the metrics storage by setting the
14+
# `metrics_storage` variable to `null`.
15+
condition = try(var.metrics_storage.create_role ? var.metrics_storage.cluster_oidc_issuer_url != null : var.metrics_storage.iam_role_arn != null, true)
16+
error_message = "If you want to create a role, you need to provide the OIDC issuer's URL for the EKS cluster. Otherwise, you need to provide the ARN of the IAM role you created."
17+
}
918
}

eks/locals.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
locals {
2+
# We use the try() function to avoid errors here when we deactivate the metrics storage by setting the
3+
# `metrics_storage` variable to `null`.
4+
iam_role_arn = try(var.metrics_storage.create_role ? module.iam_assumable_role_kube_prometheus_stack.iam_role_arn : var.metrics_storage.iam_role_arn, null)
5+
26
metrics_storage = var.metrics_storage != null ? {
37
storage_config = {
48
type = "s3"
59
config = {
6-
bucket = "${var.metrics_storage.bucket_id}"
7-
endpoint = "s3.${var.metrics_storage.region}.amazonaws.com"
10+
bucket = "${data.aws_s3_bucket.kube_prometheus_stack[0].id}"
11+
endpoint = "s3.${data.aws_s3_bucket.kube_prometheus_stack[0].region}.amazonaws.com"
812
}
913
}
1014
} : null
@@ -14,7 +18,7 @@ locals {
1418
prometheus = {
1519
serviceAccount = {
1620
annotations = {
17-
"eks.amazonaws.com/role-arn" = var.metrics_storage.iam_role_arn
21+
"eks.amazonaws.com/role-arn" = local.iam_role_arn
1822
}
1923
}
2024
}

eks/main.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,56 @@
1+
data "aws_s3_bucket" "kube_prometheus_stack" {
2+
count = var.metrics_storage != null ? 1 : 0
3+
4+
bucket = var.metrics_storage.bucket_id
5+
}
6+
7+
data "aws_iam_policy_document" "kube_prometheus_stack" {
8+
count = var.metrics_storage != null ? (var.metrics_storage.create_role ? 1 : 0) : 0
9+
10+
statement {
11+
actions = [
12+
"s3:ListBucket",
13+
"s3:GetBucketLocation",
14+
"s3:PutObject",
15+
"s3:GetObject",
16+
"s3:DeleteObject",
17+
]
18+
19+
resources = [
20+
data.aws_s3_bucket.kube_prometheus_stack[0].arn,
21+
format("%s/*", data.aws_s3_bucket.kube_prometheus_stack[0].arn),
22+
]
23+
24+
effect = "Allow"
25+
}
26+
}
27+
28+
resource "aws_iam_policy" "kube_prometheus_stack" {
29+
count = var.metrics_storage != null ? (var.metrics_storage.create_role ? 1 : 0) : 0
30+
31+
name = "kube-prometheus-stack-s3"
32+
description = "IAM policy for the kube-prometheus-stack to access the S3 bucket named ${data.aws_s3_bucket.kube_prometheus_stack[0].id}"
33+
policy = data.aws_iam_policy_document.kube_prometheus_stack[0].json
34+
}
35+
36+
module "iam_assumable_role_kube_prometheus_stack" {
37+
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
38+
version = "~> 5.0"
39+
create_role = var.metrics_storage != null ? var.metrics_storage.create_role : false
40+
number_of_role_policy_arns = 1
41+
role_name_prefix = "kube-prometheus-stack-s3-"
42+
43+
# We use the try() function to avoid errors here when we deactivate the metrics storage by setting the
44+
# `metrics_storage` variable to `null`.
45+
provider_url = try(trimprefix(var.metrics_storage.cluster_oidc_issuer_url, "https://"), "")
46+
role_policy_arns = [try(resource.aws_iam_policy.kube_prometheus_stack[0].arn, null)]
47+
48+
# List of ServiceAccounts that have permission to attach to this IAM role
49+
oidc_fully_qualified_subjects = [
50+
"system:serviceaccount:kube-prometheus-stack:kube-prometheus-stack-prometheus"
51+
]
52+
}
53+
154
module "kube-prometheus-stack" {
255
source = "../"
356

0 commit comments

Comments
 (0)