Skip to content

Commit 22cca64

Browse files
committed
Adding firelens option to ECS
1 parent 3de8c37 commit 22cca64

File tree

7 files changed

+105
-36
lines changed

7 files changed

+105
-36
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ The following inputs can be used as `step.with` keys
454454
| `aws_ecs_autoscaling_max_cpu`| String | Define autoscaling max cpu. |
455455
| `aws_ecs_cloudwatch_enable`| Boolean | Toggle cloudwatch for ECS. Default `false`. |
456456
| `aws_ecs_cloudwatch_lg_name`| String | Log group name. Will default to `aws_identifier` if none. |
457+
| `aws_ecs_cloudwatch_log_driver`| String | Log driver to use for the ECS task. (awslogs/awsfirelens) Defaults to `awslogs`. |
458+
| `aws_ecs_firelens_output_type`| String | Firelens type, one of `otlp`, `loki`, `cloudwatch`, `firehose`, `kinesis`, `es`, `splunk`, `http`, `kafka`, `syslog`. |
459+
| `aws_ecs_firelens_output_options`| String | Option definitions. Eg. `"{\"Host\":\"collector\",\"Port\":\"4317\"}"`. See [this example repo.](https://github.com/aws-samples/amazon-ecs-firelens-examples)|
457460
| `aws_ecs_cloudwatch_skip_destroy`| Boolean | Toggle deletion or not when destroying the stack. |
458461
| `aws_ecs_cloudwatch_retention_days`| String | Number of days to retain logs. 0 to never expire. Defaults to `14`. |
459462
| `aws_ecs_additional_tags`| JSON | Add additional tags to the terraform [default tags](https://www.hashicorp.com/blog/default-tags-in-the-terraform-aws-provider), any tags put here will be added to ECS provisioned resources.|

action.yaml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,15 @@ inputs:
877877
aws_ecs_cloudwatch_lg_name:
878878
description: "Log group name. Will default to aws_identifier if none."
879879
required: false
880+
aws_ecs_cloudwatch_log_driver:
881+
description: "Log driver to use for the ECS task."
882+
required: false
883+
aws_ecs_firelens_output_type:
884+
description: "The type of output for FireLens."
885+
required: false
886+
aws_ecs_firelens_output_options:
887+
description: "The options for the FireLens output."
888+
required: false
880889
aws_ecs_cloudwatch_skip_destroy:
881890
description: "Toggle deletion or not when destroying the stack."
882891
required: false
@@ -1435,34 +1444,37 @@ runs:
14351444
AWS_ECS_ENABLE: ${{ inputs.aws_ecs_enable }}
14361445
AWS_ECS_SERVICE_NAME: ${{ inputs.aws_ecs_service_name }}
14371446
AWS_ECS_CLUSTER_NAME: ${{ inputs.aws_ecs_cluster_name }}
1438-
AWS_ECS_SERVICE_LAUNCH_TYPE : ${{ inputs.aws_ecs_service_launch_type }}
1439-
AWS_ECS_TASK_TYPE : ${{ inputs.aws_ecs_task_type }}
1447+
AWS_ECS_SERVICE_LAUNCH_TYPE: ${{ inputs.aws_ecs_service_launch_type }}
1448+
AWS_ECS_TASK_TYPE: ${{ inputs.aws_ecs_task_type }}
14401449
AWS_ECS_TASK_NAME: ${{ inputs.aws_ecs_task_name }}
14411450
AWS_ECS_TASK_EXECUTION_ROLE: ${{ inputs.aws_ecs_task_execution_role }}
14421451
AWS_ECS_TASK_JSON_DEFINITION_FILE: ${{ inputs.aws_ecs_task_json_definition_file }}
14431452
AWS_ECS_TASK_NETWORK_MODE: ${{ inputs.aws_ecs_task_network_mode }}
14441453
AWS_ECS_TASK_CPU: ${{ inputs.aws_ecs_task_cpu }}
14451454
AWS_ECS_TASK_MEM: ${{ inputs.aws_ecs_task_mem }}
14461455
AWS_ECS_CONTAINER_CPU: ${{ inputs.aws_ecs_container_cpu }}
1447-
AWS_ECS_CONTAINER_MEM: ${{ inputs.aws_ecs_container_cpu }}
1456+
AWS_ECS_CONTAINER_MEM: ${{ inputs.aws_ecs_container_mem }}
14481457
AWS_ECS_NODE_COUNT: ${{ inputs.aws_ecs_node_count }}
14491458
AWS_ECS_APP_IMAGE: ${{ inputs.aws_ecs_app_image }}
14501459
AWS_ECS_SECURITY_GROUP_NAME: ${{ inputs.aws_ecs_security_group_name }}
14511460
AWS_ECS_ASSIGN_PUBLIC_IP: ${{ inputs.aws_ecs_assign_public_ip }}
14521461
AWS_ECS_CONTAINER_PORT: ${{ inputs.aws_ecs_container_port }}
14531462
AWS_ECS_LB_PORT: ${{ inputs.aws_ecs_lb_port }}
14541463
AWS_ECS_LB_REDIRECT_ENABLE: ${{ inputs.aws_ecs_lb_redirect_enable }}
1455-
AWS_ECS_LB_CONTAINER_PATH : ${{ inputs.aws_ecs_lb_container_path }}
1464+
AWS_ECS_LB_CONTAINER_PATH: ${{ inputs.aws_ecs_lb_container_path }}
14561465
AWS_ECS_LB_SSL_POLICY: ${{ inputs.aws_ecs_lb_ssl_policy }}
14571466
AWS_ECS_AUTOSCALING_ENABLE: ${{ inputs.aws_ecs_autoscaling_enable }}
14581467
AWS_ECS_AUTOSCALING_MAX_NODES: ${{ inputs.aws_ecs_autoscaling_max_nodes }}
14591468
AWS_ECS_AUTOSCALING_MIN_NODES: ${{ inputs.aws_ecs_autoscaling_min_nodes }}
14601469
AWS_ECS_AUTOSCALING_MAX_MEM: ${{ inputs.aws_ecs_autoscaling_max_mem }}
1461-
AWS_ECS_AUTOSCALING_MIN_MEM: ${{ inputs.aws_ecs_autoscaling_max_cpu }}
1462-
AWS_ECS_CLOUDWATCH_ENABLE : ${{ inputs.aws_ecs_cloudwatch_enable }}
1463-
AWS_ECS_CLOUDWATCH_LG_NAME : ${{ inputs.aws_ecs_cloudwatch_lg_name }}
1464-
AWS_ECS_CLOUDWATCH_SKIP_DESTROY : ${{ inputs.aws_ecs_cloudwatch_skip_destroy }}
1465-
AWS_ECS_CLOUDWATCH_RETENTION_DAYS : ${{ inputs.aws_ecs_cloudwatch_retention_days }}
1470+
AWS_ECS_AUTOSCALING_MAX_CPU: ${{ inputs.aws_ecs_autoscaling_max_cpu }}
1471+
AWS_ECS_CLOUDWATCH_ENABLE: ${{ inputs.aws_ecs_cloudwatch_enable }}
1472+
AWS_ECS_CLOUDWATCH_LG_NAME: ${{ inputs.aws_ecs_cloudwatch_lg_name }}
1473+
AWS_ECS_CLOUDWATCH_LOG_DRIVER: ${{ inputs.aws_ecs_cloudwatch_log_driver }}
1474+
AWS_ECS_FIRELENS_OUTPUT_TYPE: ${{ inputs.aws_ecs_firelens_output_type }}
1475+
AWS_ECS_FIRELENS_OUTPUT_OPTIONS: ${{ inputs.aws_ecs_firelens_output_options }}
1476+
AWS_ECS_CLOUDWATCH_SKIP_DESTROY: ${{ inputs.aws_ecs_cloudwatch_skip_destroy }}
1477+
AWS_ECS_CLOUDWATCH_RETENTION_DAYS: ${{ inputs.aws_ecs_cloudwatch_retention_days }}
14661478
AWS_ECS_ADDITIONAL_TAGS: ${{ inputs.aws_ecs_additional_tags }}
14671479

14681480
# ECR

operations/_scripts/generate/generate_vars_terraform.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ if [[ $(alpha_only "$AWS_ECS_ENABLE") == true ]]; then
341341
aws_ecs_autoscaling_max_cpu=$(generate_var aws_ecs_autoscaling_max_cpu $AWS_ECS_AUTOSCALING_MIN_MEM)
342342
aws_ecs_cloudwatch_enable=$(generate_var aws_ecs_cloudwatch_enable $AWS_ECS_CLOUDWATCH_ENABLE)
343343
aws_ecs_cloudwatch_lg_name=$(generate_var aws_ecs_cloudwatch_lg_name $AWS_ECS_CLOUDWATCH_LG_NAME)
344+
aws_ecs_cloudwatch_log_driver=$(generate_var aws_ecs_cloudwatch_log_driver $AWS_ECS_CLOUDWATCH_LOG_DRIVER)
345+
aws_ecs_firelens_output_type=$(generate_var aws_ecs_firelens_output_type $AWS_ECS_FIRELENS_OUTPUT_TYPE)
346+
aws_ecs_firelens_output_options=$(generate_var aws_ecs_firelens_output_options $AWS_ECS_FIRELENS_OUTPUT_OPTIONS)
344347
aws_ecs_cloudwatch_skip_destroy=$(generate_var aws_ecs_cloudwatch_skip_destroy $AWS_ECS_CLOUDWATCH_SKIP_DESTROY)
345348
aws_ecs_cloudwatch_retention_days=$(generate_var aws_ecs_cloudwatch_retention_days $AWS_ECS_CLOUDWATCH_RETENTION_DAYS)
346349
aws_ecs_additional_tags=$(generate_var aws_ecs_additional_tags $AWS_ECS_ADDITIONAL_TAGS)
@@ -689,6 +692,9 @@ $aws_ecs_autoscaling_max_mem
689692
$aws_ecs_autoscaling_max_cpu
690693
$aws_ecs_cloudwatch_enable
691694
$aws_ecs_cloudwatch_lg_name
695+
$aws_ecs_cloudwatch_log_driver
696+
$aws_ecs_firelens_output_type
697+
$aws_ecs_firelens_output_options
692698
$aws_ecs_cloudwatch_skip_destroy
693699
$aws_ecs_cloudwatch_retention_days
694700
$aws_ecs_additional_tags

operations/deployment/terraform/aws/aws_variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,24 @@ variable "aws_ecs_cloudwatch_lg_name" {
14871487
default = null
14881488
}
14891489

1490+
variable "aws_ecs_cloudwatch_log_driver" {
1491+
type = string
1492+
description = "Log driver to use for the ECS task."
1493+
default = "awslogs" # or "awsfirelens"
1494+
}
1495+
1496+
variable "aws_ecs_firelens_output_type" {
1497+
type = string
1498+
description = "The type of output for FireLens."
1499+
default = "otlp" # options: otlp, loki, cloudwatch, firehose, kinesis, es, splunk, http, kafka, syslog
1500+
}
1501+
1502+
variable "aws_ecs_firelens_output_options" {
1503+
type = string
1504+
description = "The options for the FireLens output."
1505+
default = "{}"
1506+
}
1507+
14901508
variable "aws_ecs_cloudwatch_skip_destroy" {
14911509
type = string
14921510
description = "Toggle deletion or not when destroying the stack."

operations/deployment/terraform/aws/bitovi_main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ module "aws_ecs" {
491491
aws_ecs_autoscaling_max_cpu = var.aws_ecs_autoscaling_max_cpu
492492
aws_ecs_cloudwatch_enable = var.aws_ecs_cloudwatch_enable
493493
aws_ecs_cloudwatch_lg_name = var.aws_ecs_cloudwatch_enable ? ( var.aws_ecs_cloudwatch_lg_name != null ? var.aws_ecs_cloudwatch_lg_name : "${var.aws_resource_identifier}-ecs-logs" ) : null
494+
aws_ecs_cloudwatch_log_driver = var.aws_ecs_cloudwatch_log_driver
495+
aws_ecs_firelens_output_type = var.aws_ecs_firelens_output_type
496+
aws_ecs_firelens_output_options = var.aws_ecs_firelens_output_options
494497
aws_ecs_cloudwatch_skip_destroy = var.aws_ecs_cloudwatch_skip_destroy
495498
aws_ecs_cloudwatch_retention_days = var.aws_ecs_cloudwatch_retention_days
496499
aws_region_current_name = module.vpc.aws_region_current_name

operations/deployment/terraform/modules/aws/ecs/aws_ecs.tf

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,64 @@ locals {
2626

2727
resource "aws_ecs_task_definition" "ecs_task" {
2828
count = length(local.aws_ecs_app_image)
29-
family = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
29+
family = var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}"
3030
network_mode = local.aws_ecs_task_network_mode[count.index]
3131
requires_compatibilities = [local.aws_ecs_task_type[count.index]]
3232
cpu = local.aws_ecs_task_cpu[count.index]
3333
memory = local.aws_ecs_task_mem[count.index]
3434
execution_role_arn = local.ecsTaskExecutionRole
35-
container_definitions = sensitive(jsonencode([
36-
{
37-
"image": local.aws_ecs_app_image[count.index],
38-
"cpu": local.aws_ecs_container_cpu[count.index],
39-
"memory": local.aws_ecs_container_mem[count.index],
40-
"name": var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}",
41-
"networkMode": "awsvpc",
42-
"portMappings": [
43-
{
44-
"name": "port-${local.aws_ecs_container_port[count.index]}",
45-
"containerPort": tonumber(local.aws_ecs_container_port[count.index]),
46-
"hostPort": tonumber(local.aws_ecs_container_port[count.index]),
47-
"protocol": "tcp",
48-
"appProtocol": "http"
49-
}
50-
],
51-
"environment": local.env_repo_vars
52-
"logConfiguration": var.aws_ecs_cloudwatch_enable ? {
53-
"logDriver": "awslogs",
54-
"options": {
55-
"awslogs-create-group": "true",
56-
"awslogs-region": var.aws_region_current_name,
57-
"awslogs-group": var.aws_ecs_cloudwatch_lg_name,
58-
"awslogs-stream-prefix": aws_ecs_cluster.cluster.name
35+
36+
container_definitions = sensitive(jsonencode(
37+
compact([ # compact removes null entries
38+
{
39+
"name": var.aws_ecs_task_name != "" ? local.aws_ecs_task_name[count.index] : "${local.aws_ecs_task_name[count.index]}${count.index}",
40+
"image": local.aws_ecs_app_image[count.index],
41+
"cpu": local.aws_ecs_container_cpu[count.index],
42+
"memory": local.aws_ecs_container_mem[count.index],
43+
"networkMode": "awsvpc",
44+
"portMappings": [
45+
{
46+
"containerPort": tonumber(local.aws_ecs_container_port[count.index]),
47+
"hostPort": tonumber(local.aws_ecs_container_port[count.index]),
48+
"protocol": "tcp"
49+
}
50+
],
51+
"environment": local.env_repo_vars,
52+
53+
# === Log configuration ===
54+
"logConfiguration": (
55+
var.aws_ecs_cloudwatch_enable && var.aws_ecs_cloudwatch_log_driver == "awslogs" ?
56+
{
57+
"logDriver": "awslogs",
58+
"options": {
59+
"awslogs-create-group": "true",
60+
"awslogs-region": var.aws_region_current_name,
61+
"awslogs-group": var.aws_ecs_cloudwatch_lg_name,
62+
"awslogs-stream-prefix": aws_ecs_cluster.cluster.name
63+
}
64+
} :
65+
var.aws_ecs_cloudwatch_enable && var.aws_ecs_cloudwatch_log_driver == "awsfirelens" ?
66+
{
67+
"logDriver": "awsfirelens",
68+
"options": merge(
69+
{ "Name": var.aws_ecs_firelens_output_type },
70+
jsondecode(var.aws_ecs_firelens_output_options)
71+
)
72+
} : null
73+
)
74+
},
75+
76+
# === FireLens sidecar ===
77+
var.aws_ecs_cloudwatch_enable && var.aws_ecs_cloudwatch_log_driver == "awsfirelens" ? {
78+
"name": "log_router",
79+
"image": "public.ecr.aws/aws-observability/aws-for-fluent-bit:latest",
80+
"essential": true,
81+
"firelensConfiguration": {
82+
"type": "fluentbit"
5983
}
6084
} : null
61-
}
62-
]))
85+
])
86+
))
6387
}
6488

6589
resource "aws_ecs_task_definition" "ecs_task_from_json" {

operations/deployment/terraform/modules/aws/ecs/aws_ecs_vars.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ variable "aws_ecs_autoscaling_min_nodes" {}
2525
variable "aws_ecs_autoscaling_max_mem" {}
2626
variable "aws_ecs_autoscaling_max_cpu" {}
2727
variable "aws_ecs_cloudwatch_enable" {}
28+
variable "aws_ecs_cloudwatch_log_driver" {}
29+
variable "aws_ecs_firelens_output_type" {}
30+
variable "aws_ecs_firelens_output_options" {}
2831
variable "aws_ecs_cloudwatch_lg_name" {}
2932
variable "aws_ecs_cloudwatch_skip_destroy" {}
3033
variable "aws_ecs_cloudwatch_retention_days" {}

0 commit comments

Comments
 (0)