Skip to content

Commit

Permalink
ECS Enhancements (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-yurchenko authored Jan 2, 2024
1 parent b1874eb commit 3820bc4
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ override.tf.json
# Ignore CLI configuration files
.terraformrc
terraform.rc
.idea/
.vscode/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down Expand Up @@ -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.
Apache 2 Licensed. See [LICENSE](https://github.com/coralogix/terraform-coralogix-aws/tree/master/LICENSE) for full details.
165 changes: 95 additions & 70 deletions modules/ecs-ec2/main.tf
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"]
Expand All @@ -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
Expand All @@ -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
)
}
12 changes: 12 additions & 0 deletions modules/ecs-ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion modules/ecs-ec2/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit 3820bc4

Please sign in to comment.