-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.tf
201 lines (168 loc) · 4.88 KB
/
vector.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
locals {
vector_api_port = 8686
vector_prometheus_port = 18687
}
##########################################
# ECS Service
##########################################
resource "aws_ecs_service" "vector" {
name = "vector"
cluster = aws_ecs_cluster.this.id
task_definition = aws_ecs_task_definition.vector.arn
desired_count = 1
launch_type = "FARGATE"
load_balancer {
target_group_arn = aws_lb_target_group.vector.arn
container_name = "vector"
container_port = local.vector_prometheus_port
}
network_configuration {
subnets = var.private_subnet_ids
security_groups = [module.vector_sg.security_group_id]
}
}
module "vector_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "5.1.0"
name = "vector-ecs-sg"
description = "Security group for vector ECS tasks"
vpc_id = var.vpc_id
ingress_with_cidr_blocks = [
{
from_port = local.vector_prometheus_port
to_port = local.vector_prometheus_port
protocol = "tcp"
cidr_blocks = data.aws_vpc.selected.cidr_block
description = "Allow VPC access"
}
]
egress_rules = ["all-all"]
}
##########################################
# ECS Task Definition
##########################################
resource "aws_ecs_task_definition" "vector" {
family = "vector"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
task_role_arn = aws_iam_role.vector_ecs_task_role.arn
execution_role_arn = aws_iam_role.ecs_task_role.arn
volume {
name = "config"
}
container_definitions = jsonencode([
{
name = "config-init"
image = "ubuntu:23.10"
entryPoint = [
"bash",
"-c",
"set -ueo pipefail; mkdir -p /etc/vector/; echo ${base64encode(
templatefile("${path.module}/templates/vector-config.yaml.tpl", {
region = data.aws_region.current.name
sqs_queue_url = aws_sqs_queue.apigw_access_log.url
api_port = local.vector_api_port
prometheus_port = local.vector_prometheus_port
})
)} | base64 -d > /etc/vector/vector.yaml; cat /etc/vector/vector.yaml",
]
mountPoints = [{
sourceVolume = "config"
containerPath = "/etc/vector"
}]
essential = false
readonlyRootFilesystem = false
privileged = false
stopTimeout = 10
logConfiguration = {
logDriver = "awslogs"
secretOptions = null
options = {
awslogs-group = aws_cloudwatch_log_group.ecs_log.name
awslogs-region = data.aws_region.current.name
awslogs-stream-prefix = "vector"
}
}
},
{
name = "vector"
image = "timberio/vector:0.30.0-distroless-libc"
command = [
"--config-dir",
"/etc/vector/"
]
essential = true
readonlyRootFilesystem = false
privileged = false
portMappings = [{
containerPort = local.vector_api_port
}, {
containerPort = local.vector_prometheus_port
}]
mountPoints = [{
sourceVolume = "config"
containerPath = "/etc/vector"
}]
dependsOn = [{
containerName = "config-init"
condition = "SUCCESS"
}]
logConfiguration = {
logDriver = "awslogs"
secretOptions = null
options = {
awslogs-group = aws_cloudwatch_log_group.ecs_log.name
awslogs-region = data.aws_region.current.name
awslogs-stream-prefix = "vector"
}
}
}
])
runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = "ARM64"
}
}
##########################################
# ECS Task Roles
##########################################
resource "aws_iam_role" "vector_ecs_task_role" {
name = "vector-ecs-task-role"
description = "The ECS task role for vector"
assume_role_policy = jsonencode({
Statement = [
{
Sid = ""
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
Version = "2012-10-17"
})
}
resource "aws_iam_role_policy_attachment" "vector_ecs_task_role_policy" {
role = aws_iam_role.vector_ecs_task_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
data "aws_iam_policy_document" "vector_sqs" {
statement {
effect = "Allow"
actions = [
"sqs:ReceiveMessage",
"sqs:DeleteMessage"
]
resources = [
aws_sqs_queue.apigw_access_log.arn
]
}
}
resource "aws_iam_role_policy" "vector_sqs" {
name = "sqs"
role = aws_iam_role.vector_ecs_task_role.id
policy = data.aws_iam_policy_document.vector_sqs.json
}