From 3820bc436b064180d9740dec56f5315b649d5389 Mon Sep 17 00:00:00 2001 From: Anton Yurchenko Date: Tue, 2 Jan 2024 10:55:02 -0500 Subject: [PATCH] ECS Enhancements (#123) --- .gitignore | 2 + CHANGELOG.md | 7 ++ README.md | 4 +- modules/ecs-ec2/main.tf | 165 ++++++++++++++++++++--------------- modules/ecs-ec2/variables.tf | 12 +++ modules/ecs-ec2/versions.tf | 7 +- 6 files changed, 125 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 397af322..59f19bbc 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ override.tf.json # Ignore CLI configuration files .terraformrc terraform.rc +.idea/ +.vscode/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 942e2a4c..1dc3377e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## v1.0.76 +### 💡 Enhancements 💡 +#### **ecs-ec2** +- Use unique resource names - this will allow the deployment of the service multiple times on the same cluster (for configuration tests for example) and to maintain separate definitions within the same account/region +- [optionally] Allow tagging +- [optionally] Reuse task definition for multiple service deployments + ## v1.0.75 ### 🧰 Bug fixes 🧰 #### **coralogix-aws-shipper** diff --git a/README.md b/README.md index 32b44cda..08f36f09 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,8 @@ module "ecs-ec2" { api_key = var.api_key otel_config_file = "[optional] file path to custom OTEL collector config file" metrics = [true|false] + tags = {} # optional + task_definition_arn = "[optional] arn" } ``` @@ -207,4 +209,4 @@ Module is maintained by [Coralogix](https://github.com/coralogix). ## License -Apache 2 Licensed. See [LICENSE](https://github.com/coralogix/terraform-coralogix-aws/tree/master/LICENSE) for full details. \ No newline at end of file +Apache 2 Licensed. See [LICENSE](https://github.com/coralogix/terraform-coralogix-aws/tree/master/LICENSE) for full details. diff --git a/modules/ecs-ec2/main.tf b/modules/ecs-ec2/main.tf index 0a5f5691..b9ae4045 100644 --- a/modules/ecs-ec2/main.tf +++ b/modules/ecs-ec2/main.tf @@ -1,4 +1,11 @@ locals { + name = "coralogix-otel-agent" + tags = merge( + { + "ecs:taskDefinition:createdFrom" = "terraform" + }, + var.tags + ) coralogix_region_domain_map = { "europe" = "coralogix.com" "europe2" = "eu2.coralogix.com" @@ -15,8 +22,18 @@ locals { otel_config = templatefile(local.otel_config_file, {}) } +resource "random_string" "id" { + length = 7 + lower = true + numeric = true + upper = false + special = false +} + resource "aws_ecs_task_definition" "coralogix_otel_agent" { - family = "coralogix-otel-agent" + count = var.task_definition_arn == null ? 1 : 0 + + family = "${local.name}-${random_string.id.result}" cpu = max(var.memory, 256) memory = var.memory requires_compatibilities = ["EC2"] @@ -28,82 +45,84 @@ resource "aws_ecs_task_definition" "coralogix_otel_agent" { name = "docker-socket" host_path = "/var/run/docker.sock" } - tags = { - "ecs:taskDefinition:createdFrom" = "terraform" - } - container_definitions = jsonencode( - [{ - "name" : "coralogix-otel-agent" - "networkMode" : "host" - "image" : "${var.image}:${var.image_version}" - "essential" : true - "portMappings" : [ - { - "containerPort" : 4317 - "hostPort" : 4317 - }, - { - "containerPort" : 4318 - "hostPort" : 4318 - }, - { - "containerPort" : 8888 - "hostPort" : 8888 - }, - { - "containerPort" : 13133 - "hostPort" : 13133 - } - ], - "privileged" : true, - "mountPoints" : [ - { - "sourceVolume" : "hostfs" - "containerPath" : "/hostfs" - "readOnly" : true - }, - { - "sourceVolume" : "docker-socket" - "containerPath" : "/var/run/docker.sock" - } - ], - "environment" : [ - { - "name" : "CORALOGIX_DOMAIN" - "value" : "${local.coralogix_domain}" - }, - { - "name" : "PRIVATE_KEY" - "value" : "${var.api_key}" - }, - { - "name" : "APP_NAME" - "value" : "${var.default_application_name}" - }, - { - "name" : "SUB_SYS" - "value" : "${var.default_subsystem_name}" - }, - { - "name" : "OTEL_CONFIG" - "value" : "${local.otel_config}" - } - ], - "healthCheck" : { - "command" : ["CMD-SHELL", "nc -vz localhost 13133 || exit 1"] - "startPeriod" : 30 - "interval" : 30 - "timeout" : 5 - "retries" : 3 + tags = merge( + { + Name = "${local.name}-${random_string.id.result}" + }, + var.tags + ) + container_definitions = jsonencode([{ + name : local.name + networkMode : "host" + image : "${var.image}:${var.image_version}" + essential : true + portMappings : [ + { + containerPort : 4317 + hostPort : 4317 + }, + { + containerPort : 4318 + hostPort : 4318 + }, + { + containerPort : 8888 + hostPort : 8888 + }, + { + containerPort : 13133 + hostPort : 13133 + } + ], + privileged : true, + mountPoints : [ + { + sourceVolume : "hostfs" + containerPath : "/hostfs" + readOnly : true + }, + { + sourceVolume : "docker-socket" + containerPath : "/var/run/docker.sock" } + ], + environment : [ + { + name : "CORALOGIX_DOMAIN" + value : local.coralogix_domain + }, + { + name : "PRIVATE_KEY" + value : var.api_key + }, + { + name : "APP_NAME" + value : var.default_application_name + }, + { + name : "SUB_SYS" + value : var.default_subsystem_name + }, + { + name : "OTEL_CONFIG" + value : local.otel_config + } + ], + healthCheck : { + command : ["CMD-SHELL", "nc -vz localhost 13133 || exit 1"] + startPeriod : 30 + interval : 30 + timeout : 5 + retries : 3 + } }]) } resource "aws_ecs_service" "coralogix_otel_agent" { - name = "coralogix-otel-agent" + name = "${local.name}-${random_string.id.result}" cluster = var.ecs_cluster_name launch_type = "EC2" - task_definition = aws_ecs_task_definition.coralogix_otel_agent.arn + task_definition = var.task_definition_arn == null ? aws_ecs_task_definition.coralogix_otel_agent.arn : var.task_definition_arn scheduling_strategy = "DAEMON" deployment_maximum_percent = 100 deployment_minimum_healthy_percent = 0 @@ -118,4 +137,10 @@ resource "aws_ecs_service" "coralogix_otel_agent" { enabled = false } enable_ecs_managed_tags = true + tags = merge( + { + Name = "${local.name}-${random_string.id.result}" + }, + var.tags + ) } diff --git a/modules/ecs-ec2/variables.tf b/modules/ecs-ec2/variables.tf index f0908043..1ab4a799 100644 --- a/modules/ecs-ec2/variables.tf +++ b/modules/ecs-ec2/variables.tf @@ -70,3 +70,15 @@ variable "otel_config_file" { description = "File path to a custom opentelemetry configuration file. Defaults to an embedded configuration." default = null } + +variable "tags" { + type = map(string) + description = "Resource tags" + default = null +} + +variable "task_definition_arn" { + type = string + description = "Existing Coralogix OTEL task definition ARN" + default = null +} diff --git a/modules/ecs-ec2/versions.tf b/modules/ecs-ec2/versions.tf index c38282ed..2e1df118 100644 --- a/modules/ecs-ec2/versions.tf +++ b/modules/ecs-ec2/versions.tf @@ -4,7 +4,12 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = ">= 4.15.1" + version = ">= 5.24.0" + } + + random = { + source = "hashicorp/random" + version = ">= 3.6.0" } } }