Skip to content

Commit

Permalink
fix(): exception when config turn off "is_log" and without "logConfig…
Browse files Browse the repository at this point in the history
…uration"
  • Loading branch information
angle319 committed Jul 24, 2023
1 parent d204291 commit abefc74
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 37 deletions.
128 changes: 99 additions & 29 deletions example/example.tf
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
/* This sample is for create base ecs container with aws log driver */
# module "some-example" {
# source = "../"
# vpc_id = var.vpc_id
# cs_id = var.cs_id
# env = var.env
# is_log = true
# name = "some-example"
# task_def = [{
# "name" = "some-example"
# "image" = "docker_path:tag"
# "cpu" = 200
# "memoryReservation" = 400
# "command" : [
# "node", "index.js"
# ]
# "environment" = [for k, v in {
# NODE_ENV = "dev"
# } : { "name" : k, "value" : v }]
# }]
# https_listener_rules = [
# {
# conditions = [{
# path_patterns = ["/path"]
# host_headers = ["example.domain.com"]
# }]
# priority = 901
# }
# ]
# }
module "some-example" {
source = "../"
vpc_id = "1111"
cs_id = "1111"
env = "test"
is_log = true
name = "some-example"
task_def = [{
"name" = "some-example"
"image" = "docker_path:tag"
"cpu" = 200
"memoryReservation" = 400
"command" : [
"node", "index.js"
]
"environment" = [for k, v in {
NODE_ENV = "dev"
} : { "name" : k, "value" : v }]
}]
https_listener_rules = [
{
conditions = [{
path_patterns = ["/path"]
host_headers = ["example.domain.com"]
}]
priority = 901
}
]
}

/* This sample is for create base ecs container without auto gen log*/
module "without-log-driver-example" {
source = "../"
vpc_id = "test"
cs_id = "test"
env = "test"
is_log = false // must close auto log flag
name = "some-example"
task_def = [{
"name" = "some-example"
"image" = "docker_path:tag"
"cpu" = 200
"memoryReservation" = 400
"command" : [
"node", "index.js"
]
"environment" = [for k, v in {
NODE_ENV = "dev"
} : { "name" : k, "value" : v }]
}]
https_listener_rules = [
{
conditions = [{
path_patterns = ["/path"]
host_headers = ["example.domain.com"]
}]
priority = 901
}
]
}

/* This sample is for create base ecs container with fluentd driver */
module "fluentd-log-driver-example" {
Expand Down Expand Up @@ -67,3 +98,42 @@ module "fluentd-log-driver-example" {
}
]
}

/* This sample is for create base ecs container with fluentd driver (log group resource not auto gen)*/
module "fluentd-log-driver-without-gen-cwgroup-example" {
source = "../"
vpc_id = "test"
cs_id = "test"
env = "test"
is_log = false // must close auto log flag
name = "some-example"
task_def = [{
"name" = "some-example"
"image" = "docker_path:tag"
"cpu" = 200
"memoryReservation" = 400
"command" : [
"node", "index.js"
]
logConfiguration = {
"logDriver" = "fluentd",
"options" = {
"fluentd-address" = "10.1.100.0:24224",
"tag" = "ecs-myself",
}
}
"environment" = [for k, v in {
NODE_ENV = "dev"
} : { "name" : k, "value" : v }]
}]
https_listener_rules = [
{
conditions = [{
path_patterns = ["/path"]
host_headers = ["example.domain.com"]
}]
priority = 901
}
]
}

4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ locals {
is_log = var.is_log
log_taskdefs = [for x in var.task_def : x if lookup(x, "logConfiguration", null) != null]
load_balancing_algorithm_type = var.load_balancing_algorithm_type
list_task_wtih_auto_cwlg = [for x in [for x in var.task_def : { for k, v in lookup(x, "logConfiguration", null) : k => v if k == var.auto_generate_cw_group_key }] : x if x != {}]
list_task_wtih_auto_cwlg = local.is_log == false ? [for x in var.task_def : { for k, v in x.logConfiguration : k => v if k == var.auto_generate_cw_group_key } if try(x["logConfiguration"]["cloudwatchGroupName"], null) != null] : []
}

data "aws_region" "current" {}
Expand Down Expand Up @@ -68,7 +68,7 @@ resource "aws_cloudwatch_log_group" "customize_naming" {
resource "aws_ecs_task_definition" "this" {
family = "${local.alias}-task"
container_definitions = local.is_log == false ? jsonencode(
[for x in var.task_def : merge(x, { logConfiguration = { for k, v in lookup(x, "logConfiguration", null) : k => v if k != var.auto_generate_cw_group_key } })]
[for x in var.task_def : merge(x, { logConfiguration = { for k, v in lookup(x, "logConfiguration", null) : k => v if k != var.auto_generate_cw_group_key } }) if try(x["logConfiguration"]["cloudwatchGroupName"], null) != null]
) : jsonencode([for x in var.task_def : merge(x, { logConfiguration = {
logDriver = "awslogs"
options = {
Expand Down
12 changes: 6 additions & 6 deletions test/obj-parse.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ locals {
"node", "index.js"
]
"logConfiguration" = {
cloudwatchGroupName ="11234"
"logDriver" = "fluentd",
"options" = {
"fluentd-address" = "10.1.100.181:24224",
Expand All @@ -20,33 +21,32 @@ locals {
}]
}


output "get_specail_key_from_obj" {
value = [for x in [for x in local.sample_task_def : { for k, v in lookup(x, "logConfiguration", null) : k => v if k == "cloudwatchGroupName" }] : x if x != {}]
value = [for x in local.sample_task_def : { for k, v in x.logConfiguration : k => v if k == "cloudwatchGroupName" } if try(x["logConfiguration"]["cloudwatchGroupName"], null) != null]
}


output "filter_obj_key" {
value = [for x in local.sample_task_def : merge(x,
{ logConfiguration = { for k, v in lookup(x, "logConfiguration", null) : k => v if k != "cloudwatchGroupName" } }

)]
) if try(x["logConfiguration"]["cloudwatchGroupName"], null) != null]
}



output "get_obj_and_filter_key" {
value = [for x in local.sample_task_def :

{ for k, v in lookup(x, "logConfiguration", null) : k => v if k != "cloudwatchGroupName" }
{ for k, v in lookup(x, "logConfiguration", null) : k => v if k != "cloudwatchGroupName" } if try(x["logConfiguration"]["cloudwatchGroupName"], null) != null
]
}

output "filter_some_key" {
value = {
test = { for k, v in
local.sample_task_def[0]["logConfiguration"]
: k => v if k != "logDriver" }
try(local.sample_task_def[0]["logConfiguration"], {})
: k => v if k != "logDriver" }
}
}

Expand Down

0 comments on commit abefc74

Please sign in to comment.