-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafana_agent.tf
135 lines (115 loc) · 3.11 KB
/
grafana_agent.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
data "aws_iam_policy_document" "grafana_agent_tasks_execution_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "grafana_agent_execution_role" {
name = "${var.name}-ecs-grafana-agent-execution-role"
path = "/${var.name}/ecs/grafana-agent/"
managed_policy_arns = [
"arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
]
assume_role_policy = data.aws_iam_policy_document.grafana_agent_tasks_execution_role.json
}
resource "aws_iam_role" "grafana_agent_task_role" {
name = "${var.name}-ecs-grafana-agent-role"
path = "/${var.name}/ecs/grafana-agent/"
assume_role_policy = data.aws_iam_policy_document.grafana_agent_tasks_execution_role.json
inline_policy {
name = "${var.name}-grafana-agent-discovery"
policy = data.aws_iam_policy_document.grafana_agent_discovery.json
}
}
data "aws_iam_policy_document" "grafana_agent_discovery" {
statement {
actions = [
"ecs:ListClusters",
"ecs:ListTasks",
"ecs:DescribeTask",
"ec2:DescribeInstances",
"ecs:DescribeContainerInstances",
"ecs:DescribeTasks",
"ecs:DescribeTaskDefinition"
]
resources = [
"*"
]
}
}
resource "aws_ecs_task_definition" "grafana_agent" {
family = "monitoring_grafana_agent"
container_definitions = jsonencode([
{
name = "grafana-agent"
image = var.grafana_agent_docker_image
cpu = 0
memory = 128
essential = true
mountPoints = [
{
containerPath = "/output",
sourceVolume = "config"
},
{
containerPath = "/etc/agent/agent.yaml",
sourceVolume = "agent_config"
}
]
},
{
name = "prometheus-ecs-discovery"
image = "tkgregory/prometheus-ecs-discovery:latest"
cpu = 0
memory = 128
essential = true
mountPoints = [
{
containerPath = "/output",
sourceVolume = "config"
}
]
command = [
"-config.write-to=/output/ecs_file_sd.yml",
"-config.scrape-interval=1s"
]
environment = [
{
"name" : "AWS_REGION",
"value" : data.aws_region.current.name
}
]
}
])
volume {
name = "agent_config"
host_path = "/ecs/monitoring-resources/agent.yaml"
}
volume {
name = "config"
}
requires_compatibilities = [
"EC2",
]
task_role_arn = aws_iam_role.grafana_agent_task_role.arn
execution_role_arn = aws_iam_role.grafana_agent_execution_role.arn
}
resource "aws_ecs_service" "agent" {
name = "grafana_agent"
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.grafana_agent.arn
desired_count = var.agent_service_desired_count
capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.this.name
weight = 1
}
placement_constraints {
type = "distinctInstance"
}
lifecycle {
ignore_changes = [desired_count]
}
}