Skip to content

Commit

Permalink
feat(infra): add ECS tasks for log routing to Loki (#1213)
Browse files Browse the repository at this point in the history
feat: add fluentbit to ecs tasks
  • Loading branch information
k1g99 authored Feb 17, 2024
1 parent 5693839 commit 15b6d6d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 21 deletions.
34 changes: 27 additions & 7 deletions infra/modules/codedang-infra/backend/admin-task-definition.tftpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
[
{
"name": "log_router",
"image": "grafana/fluent-bit-plugin-loki:2.0.0-amd64",
"cpu": 0,
"memoryReservation": 50,
"portMappings": [],
"essential": true,
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"user": "0",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"enable-ecs-log-metadata": "true"
}
}
},
{
"name": "${task_name}",
"image": "${ecr_uri}",
"cpu": 512,
"memory": 512,
"essential": true,
"portMappings": [
{
Expand Down Expand Up @@ -45,12 +61,16 @@
}
],
"logConfiguration": {
"logDriver": "awslogs",
"logDriver": "awsfirelens",
"options": {
"awslogs-group": "ecs/Codedang-Api-Admin",
"awslogs-region": "${cloudwatch_region}",
"awslogs-stream-prefix": "ecs"
}
"LabelKeys": "container_name,ecs_task_definition,source,ecs_cluster",
"Labels": "{job=\"firelens\"}",
"LineFormat": "key_value",
"Name": "loki",
"RemoveKeys": "container_id,ecs_task_arn",
"Url": "${loki_url}"
},
"secretOptions": []
}
}
]
34 changes: 27 additions & 7 deletions infra/modules/codedang-infra/backend/client-task-definition.tftpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
[
{
"name": "log_router",
"image": "grafana/fluent-bit-plugin-loki:2.0.0-amd64",
"cpu": 0,
"memoryReservation": 50,
"portMappings": [],
"essential": true,
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"user": "0",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"enable-ecs-log-metadata": "true"
}
}
},
{
"name": "${task_name}",
"image": "${ecr_uri}",
"cpu": 1024,
"memory": 512,
"essential": true,
"portMappings": [
{
Expand Down Expand Up @@ -77,12 +93,16 @@
}
],
"logConfiguration": {
"logDriver": "awslogs",
"logDriver": "awsfirelens",
"options": {
"awslogs-group": "ecs/Codedang-Api-Client",
"awslogs-region": "${cloudwatch_region}",
"awslogs-stream-prefix": "ecs"
}
"LabelKeys": "container_name,ecs_task_definition,source,ecs_cluster",
"Labels": "{job=\"firelens\"}",
"LineFormat": "key_value",
"Name": "loki",
"RemoveKeys": "container_id,ecs_task_arn",
"Url": "${loki_url}"
},
"secretOptions": []
}
}
]
3 changes: 3 additions & 0 deletions infra/modules/codedang-infra/ecs-api-admin.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ resource "aws_ecs_task_definition" "admin_api" {
family = "Codedang-Admin-Api"
requires_compatibilities = ["EC2"]
network_mode = "bridge"
cpu = 512
memory = 512
container_definitions = templatefile("${path.module}/backend/admin-task-definition.tftpl", {
task_name = "Codedang-Admin-Api",
# aurora-posrgresql
Expand All @@ -111,6 +113,7 @@ resource "aws_ecs_task_definition" "admin_api" {
testcase_bucket_name = aws_s3_bucket.testcase.id,
testcase_access_key = aws_iam_access_key.testcase.id,
testcase_secret_key = aws_iam_access_key.testcase.secret,
loki_url = var.loki_url,
})
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
}
3 changes: 3 additions & 0 deletions infra/modules/codedang-infra/ecs-api-client.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ resource "aws_ecs_task_definition" "client_api" {
family = "Codedang-Client-Api"
requires_compatibilities = ["EC2"]
network_mode = "bridge"
cpu = 512
memory = 512
container_definitions = templatefile("${path.module}/backend/client-task-definition.tftpl", {
task_name = "Codedang-Client-Api",
# aurora-posrgresql
Expand All @@ -115,6 +117,7 @@ resource "aws_ecs_task_definition" "client_api" {
rabbitmq_api_url = aws_mq_broker.judge_queue.instances.0.console_url,
github_client_id = var.github_client_id,
github_client_secret = var.github_client_secret,
loki_url = var.loki_url,
kakao_client_id = var.kakao_client_id,
kakao_client_secret = var.kakao_client_secret,
})
Expand Down
3 changes: 3 additions & 0 deletions infra/modules/codedang-infra/ecs-iris.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ resource "aws_ecs_task_definition" "iris" {
family = "Codedang-Iris-Api"
requires_compatibilities = ["EC2"]
network_mode = "bridge"
cpu = 512
memory = 512
container_definitions = templatefile("${path.module}/iris/task-definition.tftpl", {
ecr_uri = data.aws_ecr_repository.iris.repository_url,
testcase_bucket_name = aws_s3_bucket.testcase.id,
Expand All @@ -49,6 +51,7 @@ resource "aws_ecs_task_definition" "iris" {
cloudwatch_region = var.region,
redis_host = aws_elasticache_cluster.db_cache.cache_nodes.0.address,
redis_port = var.redis_port,
loki_url = var.loki_url,
})
execution_role_arn = aws_iam_role.ecs_iris_task_execution_role.arn
task_role_arn = aws_iam_role.ecs_iris_task_role.arn
Expand Down
35 changes: 28 additions & 7 deletions infra/modules/codedang-infra/iris/task-definition.tftpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
[
{
"name": "log_router",
"image": "grafana/fluent-bit-plugin-loki:2.0.0-amd64",
"cpu": 0,
"memoryReservation": 50,
"portMappings": [],
"essential": true,
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"user": "0",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"enable-ecs-log-metadata": "true"
}
}
},
{
"name": "Codedang-Iris",
"image": "${ecr_uri}",
"cpu": 512,
"memory": 512,
"essential": true,
"environment": [
{
Expand Down Expand Up @@ -48,12 +64,17 @@
}
],
"logConfiguration": {
"logDriver": "awslogs",
"logDriver": "awsfirelens",
"options": {
"awslogs-group": "ecs/Codedang-Iris",
"awslogs-region": "${cloudwatch_region}",
"awslogs-stream-prefix": "ecs"
}
"LabelKeys": "container_name,ecs_task_definition,source,ecs_cluster",
"Labels": "{job=\"firelens\"}",
"LineFormat": "key_value",
"Name": "loki",
"RemoveKeys": "container_id,ecs_task_arn",
"Url": "${loki_url}"
},
"secretOptions": []
}

}
]
5 changes: 5 additions & 0 deletions infra/modules/codedang-infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ variable "github_client_secret" {
sensitive = true
}

variable "loki_url" {
type = string
default = "https://grafana.codedang.com/lokiaws/loki/api/v1/push"
}

variable "kakao_client_id" {
type = string
default = "kakao_client_id"
Expand Down

0 comments on commit 15b6d6d

Please sign in to comment.