-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
155 lines (132 loc) · 4.61 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
data "aws_caller_identity" "this" {}
data "aws_region" "this" {}
data "aws_eks_cluster" "this" {
name = var.eks_cluster_name
}
data "tls_certificate" "this" {
url = data.aws_eks_cluster.this.identity[0].oidc[0].issuer
}
locals {
oidc_provider = replace(data.aws_eks_cluster.this.identity[0].oidc[0].issuer, "https://", "")
}
resource "aws_vpc_endpoint" "prometheus" {
count = var.create_amp_vpc_endpoint == true ? 1 : 0
vpc_endpoint_type = "Interface"
vpc_id = var.vpc_id
service_name = "com.amazonaws.${data.aws_region.this.name}.aps-workspaces"
subnet_ids = var.vpc_endpoint_subnets
security_group_ids = var.vpc_endpoint_security_groups
tags = var.tags
}
resource "aws_prometheus_workspace" "prod_eks_metrics" {
alias = var.prometheus_workspace_alias
}
data "aws_iam_policy_document" "remote_write_assume" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
principals {
type = "Federated"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.this.account_id}:oidc-provider/${local.oidc_provider}"]
}
condition {
test = "StringEquals"
variable = "${local.oidc_provider}:sub"
values = [
"system:serviceaccount:${var.grafana_namespace}:${var.service_account_name}"
]
}
}
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
principals {
type = "Federated"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.this.account_id}:oidc-provider/${local.oidc_provider}"]
}
condition {
test = "StringEquals"
variable = "${local.oidc_provider}:sub"
values = [
"system:serviceaccount:${var.prometheus_namespace}:${var.service_account_name}"
]
}
}
}
resource "aws_iam_role" "eks_amp_role" {
name = var.service_account_iam_role_name
description = var.service_account_iam_role_description
assume_role_policy = data.aws_iam_policy_document.remote_write_assume.json
}
resource "aws_iam_policy" "amp_write" {
name = var.service_account_iam_policy_name
description = "Permissions to write to all AMP workspaces"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"aps:RemoteWrite",
"aps:QueryMetrics",
"aps:GetSeries",
"aps:GetLabels",
"aps:GetMetricMetadata"
],
"Resource" : "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "amp_write" {
role = aws_iam_role.eks_amp_role.name
policy_arn = aws_iam_policy.amp_write.arn
}
resource "aws_iam_openid_connect_provider" "this" {
count = var.create_oidc_iam_provider == true ? 1 : 0
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = [data.tls_certificate.this.certificates[0].sha1_fingerprint]
url = data.aws_eks_cluster.this.identity[0].oidc[0].issuer
}
resource "kubernetes_namespace" "prometheus" {
count = var.create_prometheus_server == true ? 1 : 0
metadata {
name = var.prometheus_namespace
}
}
locals {
args = ["--name", "aps", "--region", data.aws_region.this.name, "--host aps-workspaces.${data.aws_region.this.name}.amazonaws.com", "--port", ":8005"]
}
data "template_file" "prometheus_values" {
count = var.create_prometheus_server == true ? 1 : 0
template = file("${path.module}/src/prometheus-for-amp.values")
vars = {
region = data.aws_region.this.name
name = var.service_account_name
arn = aws_iam_role.eks_amp_role.arn
workspace = aws_prometheus_workspace.prod_eks_metrics.id
}
}
resource "local_file" "prometheus_values" {
count = var.create_prometheus_server == true ? 1 : 0
content = data.template_file.prometheus_values[0].rendered
filename = "${path.module}/values.yaml"
}
resource "helm_release" "prometheus_install" {
count = var.create_prometheus_server == true ? 1 : 0
name = "prometheus-for-amp"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "prometheus"
namespace = var.prometheus_namespace
}
resource "null_resource" "prometheus_update" {
count = var.create_prometheus_server == true ? 1 : 0
provisioner "local-exec" {
command = "helm upgrade --install prometheus-for-amp --values ${path.module}/values.yaml --namespace ${var.prometheus_namespace} --wait prometheus-community/prometheus"
interpreter = ["/usr/bin/env", "bash", "-c"]
}
depends_on = [
helm_release.prometheus_install,
local_file.prometheus_values,
]
}