Skip to content

Commit

Permalink
Fix log issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrixroa committed Jan 14, 2021
1 parent aa47e53 commit 55f9073
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 89 deletions.
112 changes: 23 additions & 89 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@
Module to provisioning services and rolling update deployments and autoscaling ecs task with cloudwatch alarms
*/

locals {
elasticsearch_logs_config = {
"logDriver" : "awsfirelens",
"options" : {
"Name" : "es",
"Host" : var.es_url,
"Port" : "443",
"Index" : lower(var.name),
"Type" : "${lower(var.name)}_type",
"Aws_Auth" : "On",
"Aws_Region" : var.region,
"tls" : "On"
}
}

cloudwatch_logs_config = {
"logDriver" : "awslogs",
"options" : {
"awslogs-region" : var.region,
"awslogs-group" : var.name,
"awslogs-stream-prefix" : var.prefix_logs
}
}
}

// AWS ECS Service to run the task definition
resource "aws_ecs_service" "main" {
name = var.name
Expand Down Expand Up @@ -70,75 +45,34 @@ resource "aws_ecs_task_definition" "main" {
cpu = var.cpu_unit
memory = var.memory

container_definitions = <<TASK_DEFINITION
[
{
"essential": true,
"image": "906394416424.dkr.ecr.us-east-1.amazonaws.com/aws-for-fluent-bit:latest",
"name": "log_router",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"config-file-type": "file",
"config-file-value": "/fluent-bit/configs/parse-json.conf"
}
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${var.name}-firelens-container",
"awslogs-region": "${var.region}",
"awslogs-stream-prefix": "firelens"
}
},
"memoryReservation": 50
},
{
"essential": true,
"image": "${var.ecr_image_url}",
"name": "${var.name}",
"portMappings": [
{
"containerPort": ${var.port},
"hostPort": ${var.port}
}
],
"logConfiguration": ${var.use_cloudwatch_logs ? local.cloudwatch_logs_config : local.elasticsearch_logs_config},
"secrets": [
{
"name": "${var.secrets_name}",
"valueFrom": "${var.secrets_value_arn}"
}
],
"environment": [
{
"name": "DATABASE_LOG_LEVEL",
"value": "${var.database_log_level}"
},
{
"name": "APP",
"value": "${var.name}"
},
{
"name": "LOG_LEVEL",
"value": "${var.log_level}"
},
{
"name": "PORT",
"value": "${var.port}"
},
{
"name": "NEW_RELIC_APP_NAME",
"value": "${var.name}"
}
]
container_definitions = data.template_file.main.rendered

lifecycle {
create_before_destroy = true
}
}

data "template_file" "main" {
template = file("${path.module}/task_definition_${var.use_cloudwatch_logs ? "cloudwatch" : "elasticsearch"}.json")

vars = {
ecr_image_url = var.ecr_image_url
name = var.name
name_index_log = lower(var.name)
port = var.port
region = var.region
secrets_name = var.secrets_name
secrets_value_arn = var.secrets_value_arn
database_log_level = var.database_log_level
log_level = var.log_level
es_url = var.es_url
prefix_logs = var.prefix_logs
}
]
TASK_DEFINITION
}

// Auxiliary logs
resource "aws_cloudwatch_log_group" "main" {
count = var.use_cloudwatch_logs ? 0 : 1
name = "${var.name}-firelens-container"
retention_in_days = 14
}
Expand Down
49 changes: 49 additions & 0 deletions task_definition_cloudwatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"essential": true,
"image": "${ecr_image_url}",
"name": "${name}",
"portMappings": [
{
"containerPort": ${port},
"hostPort": ${port}
}
],
"logConfiguration": {
"logDriver" : "awslogs",
"options" : {
"awslogs-region" : "${region}",
"awslogs-group" : "${name}",
"awslogs-stream-prefix" : "${prefix_logs}"
}
},
"secrets": [
{
"name": "${secrets_name}",
"valueFrom": "${secrets_value_arn}"
}
],
"environment": [
{
"name": "DATABASE_LOG_LEVEL",
"value": "${database_log_level}"
},
{
"name": "APP",
"value": "${name}"
},
{
"name": "LOG_LEVEL",
"value": "${log_level}"
},
{
"name": "PORT",
"value": "${port}"
},
{
"name": "NEW_RELIC_APP_NAME",
"value": "${name}"
}
]
}
]
67 changes: 67 additions & 0 deletions task_definition_elasticsearch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[
{
"essential": true,
"image": "906394416424.dkr.ecr.us-east-1.amazonaws.com/aws-for-fluent-bit:latest",
"name": "log_router",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"config-file-type": "file",
"config-file-value": "/fluent-bit/configs/parse-json.conf"
}
},
"memoryReservation": 50
},
{
"essential": true,
"image": "${ecr_image_url}",
"name": "${name}",
"portMappings": [
{
"containerPort": ${port},
"hostPort": ${port}
}
],
"logConfiguration": {
"logDriver":"awsfirelens",
"options": {
"Name": "es",
"Host": "${es_url}",
"Port": "443",
"Index": "${name_index_log}",
"Type": "${name_index_log}_type",
"Aws_Auth": "On",
"Aws_Region": "${region}",
"tls": "On"
}
},
"secrets": [
{
"name": "${secrets_name}",
"valueFrom": "${secrets_value_arn}"
}
],
"environment": [
{
"name": "DATABASE_LOG_LEVEL",
"value": "${database_log_level}"
},
{
"name": "APP",
"value": "${name}"
},
{
"name": "LOG_LEVEL",
"value": "${log_level}"
},
{
"name": "PORT",
"value": "${port}"
},
{
"name": "NEW_RELIC_APP_NAME",
"value": "${name}"
}
]
}
]

0 comments on commit 55f9073

Please sign in to comment.