Skip to content

Commit

Permalink
ecs-ec2 module:
Browse files Browse the repository at this point in the history
ECS task health check using netcat. Use custom config file path as parameter instead of base64.
User-provided custom OTEL config file.
Doc improvements.
  • Loading branch information
frank-ang committed Nov 10, 2023
1 parent d92ae36 commit 6c9c135
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 51 deletions.
4 changes: 3 additions & 1 deletion examples/ecs-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ provider "aws" {
}
module "otel_ecs_ec2_coralogix" {
source = "github.com/coralogix/terraform-coralogix-aws//modules/ecs-ec2"
source = "github.com/coralogix/terraform-coralogix-aws/modules/ecs-ec2"
ecs_cluster_name = var.ecs_cluster_name
image_version = var.image_version
memory = var.memory
coralogix_region = var.coralogix_region
default_application_name = var.default_application_name
default_subsystem_name = var.default_subsystem_name
private_key = var.private_key
otel_config_file = var.otel_config_file
metrics = var.metrics
}
```
Expand Down
33 changes: 28 additions & 5 deletions examples/ecs-ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,64 @@ variable "ecs_cluster_name" {
}

variable "image_version" {
description = "The Coralogix Open Telemetry Distribution Image Version/Tag. Defaults to \"latest\". See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags"
description = "The Coralogix Open Telemetry Distribution Image Version/Tag. See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags"
type = string
default = "latest"
}

variable "image" {
description = "The OpenTelemetry Collector Image to use. Defaults to \"coralogixrepo/coralogix-otel-collector\". Should accept default unless advised by Coralogix support."
type = string
default = "coralogixrepo/coralogix-otel-collector"
}

variable "memory" {
description = "The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value."
description = "The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value. Minimum \"256\" MiB. CPU Units will be allocated directly proportional to Memory."
type = number
default = 256
}

variable "coralogix_region" {
description = "The Coralogix location region, [Europe, Europe2, India, Singapore, US, US2]"
type = string
validation {
condition = can(regex("^(Europe|Europe2|India|Singapore|US|US2)$", var.coralogix_region))
error_message = "Must be one of [Europe, Europe2, India, Singapore, US, US2]"
}
}

variable "default_application_name" {
description = "The default Coralogix Application name."
type = string
validation {
condition = length(var.default_application_name) >= 1 && length(var.default_application_name) <= 64
error_message = "The Default Application Name length should be within 1 and 64 characters"
}
}

variable "default_subsystem_name" {
description = "The default Coralogix Subsystem name."
type = string
default = "default"
validation {
condition = length(var.default_subsystem_name) >= 1 && length(var.default_subsystem_name) <= 64
error_message = "The Default Subsystem Name length should be within 1 and 64 characters"
}
}

variable "private_key" {
description = "The Coralogix Send-Your-Data API key for your Coralogix account."
description = "The Send-Your-Data API key for your Coralogix account. See: https://coralogix.com/docs/send-your-data-api-key/"
type = string
sensitive = true
}

variable "metrics" {
type = bool
description = "If true, cadivisor will be deployed on each node to collect metrics"
description = "If true, collects ECS task resource usage metrics (such as CPU, memory, network, and disk) and publishes to Coralogix. See: https://github.com/coralogix/coralogix-otel-collector/tree/master/receiver/awsecscontainermetricsdreceiver"
default = false
}

variable "otel_config_file" {
type = string
description = "File path to a custom opentelemetry configuration file. Defaults to an embedded configuration. See https://opentelemetry.io/docs/collector/configuration/ and https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/coralogixexporter"
default = null
}
18 changes: 12 additions & 6 deletions modules/ecs-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module "ecs-ec2" {
source = "../../modules/ecs-ec2"
ecs_cluster_name = "ecs-cluster-name"
cdot_image_version = "latest"
memory = 256
memory = numeric MiB
coralogix_region = ["Europe"|"Europe2"|"India"|"Singapore"|"US"|"US2"|"Custom"]
default_application_name = "Coralogix Application Name"
default_subsystem_name = "Coralogix Subsystem Name"
private_key = var.private_key
otel_config_file = "[optional] file path to custom OTEL collector config file"
metrics = [true|false]
}
```
Expand All @@ -32,6 +34,10 @@ module "ecs-ec2" {
|------|---------|
| aws | 5.24.0 |

## Modules

No modules.

## Resources

| Name | Type |
Expand All @@ -48,11 +54,11 @@ module "ecs-ec2" {
| default\_subsystem\_name | The default Coralogix Subsystem name. | `string` | `"default"` | no |
| ecs\_cluster\_name | Name of the AWS ECS Cluster to deploy the Coralogix OTEL Collector. Supports Amazon EC2 instances only, not Fargate. | `string` | n/a | yes |
| image | The OpenTelemetry Collector Image to use. Defaults to "coralogixrepo/coralogix-otel-collector". Should accept default unless advised by Coralogix support. | `string` | `"coralogixrepo/coralogix-otel-collector"` | no |
| image\_version | The Coralogix Open Telemetry Distribution Image Version/Tag. Defaults to "latest". See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags | `string` | `"latest"` | no |
| memory | The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value. | `number` | `256` | no |
| metrics | If true, cadivisor will be deployed on each node to collect metrics | `bool` | `false` | no |
| otel\_config | The opentelemetry configuration as a base64 encoded string. Defaults to an embedded configuration. Should accept default unless advised by Coralogix support. | `string` | `null` | no |
| private\_key | The Coralogix Send-Your-Data API key for your Coralogix account. | `string` | n/a | yes |
| image\_version | The Coralogix Open Telemetry Distribution Image Version/Tag. See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags | `string` | n/a | yes |
| memory | The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value. Minimum "256" MiB. CPU Units will be allocated directly proportional to Memory. | `number` | `256` | no |
| metrics | If true, collects ECS task resource usage metrics (such as CPU, memory, network, and disk) and publishes to Coralogix. See: https://github.com/coralogix/coralogix-otel-collector/tree/master/receiver/awsecscontainermetricsdreceiver | `bool` | `false` | no |
| otel\_config\_file | File path to a custom opentelemetry configuration file. Defaults to an embedded configuration. See https://opentelemetry.io/docs/collector/configuration/ and https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/coralogixexporter | `string` | `null` | no |
| private\_key | The Send-Your-Data API key for your Coralogix account. See: https://coralogix.com/docs/send-your-data-api-key/ | `string` | n/a | yes |

## Outputs

Expand Down
71 changes: 41 additions & 30 deletions modules/ecs-ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ locals {
"US2" = "cx498.coralogix.com"
}
coralogix_region_endpoint = local.coralogix_region_endpoint_map[var.coralogix_region]
otel_config = coalesce(var.otel_config, (
var.metrics ? templatefile("${path.module}/otel_config_metrics.tftpl.yaml", {})
: templatefile("${path.module}/otel_config.tftpl.yaml", {})
))
otel_config_file = coalesce(var.otel_config_file,
(var.metrics ? "${path.module}/otel_config_metrics.tftpl.yaml" : "${path.module}/otel_config.tftpl.yaml")
)
otel_config = templatefile(local.otel_config_file, {})
}

resource "aws_ecs_task_definition" "coralogix_otel_agent" {
family = "coralogix-otel-agent"
cpu = max(var.memory, 256)
memory = var.memory
requires_compatibilities = ["EC2"]
volume {
name = "hostfs"
host_path = "/"
Expand All @@ -26,65 +27,75 @@ 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",
"requiresCompatibilities": ["EC2"],
"image" : "${var.image}:${var.image_version}",
"essential" : true,
"name" : "coralogix-otel-agent"
"networkMode" : "host"
"image" : "${var.image}:${var.image_version}"
"essential" : true
"portMappings": [
{
"containerPort" : 4317,
{
"containerPort" : 4317
"hostPort" : 4317
},
{
"containerPort" : 4318,
},
{
"containerPort" : 4318
"hostPort" : 4318
},
{
"containerPort" : 8888,
},
{
"containerPort" : 8888
"hostPort" : 8888
}
},
{
"containerPort" : 13133
"hostPort" : 13133
}
],
"privileged": true,
"mountPoints" : [
{
"sourceVolume" : "hostfs",
"containerPath" : "/hostfs",
"sourceVolume" : "hostfs"
"containerPath" : "/hostfs"
"readOnly" : true
},
{
"sourceVolume" : "docker-socket",
"sourceVolume" : "docker-socket"
"containerPath" : "/var/run/docker.sock"
}
],
"environment" : [
{
"name" : "CORALOGIX_DOMAIN",
"name" : "CORALOGIX_DOMAIN"
"value" : "${local.coralogix_region_endpoint}"
},
{
"name" : "PRIVATE_KEY",
"name" : "PRIVATE_KEY"
"value" : "${var.private_key}"
},
{
"name" : "APP_NAME",
"name" : "APP_NAME"
"value" : "${var.default_application_name}"
},
{
"name" : "SUB_SYS",
"name" : "SUB_SYS"
"value" : "${var.default_subsystem_name}"
},
{
"name" : "OTEL_CONFIG",
"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 = {
"ecs:taskDefinition:createdFrom" = "terraform"
}
}

resource "aws_ecs_service" "coralogix_otel_agent" {
Expand Down
13 changes: 6 additions & 7 deletions modules/ecs-ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ variable "ecs_cluster_name" {
}

variable "image_version" {
description = "The Coralogix Open Telemetry Distribution Image Version/Tag. Defaults to \"latest\". See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags"
description = "The Coralogix Open Telemetry Distribution Image Version/Tag. See: https://hub.docker.com/r/coralogixrepo/coralogix-otel-collector/tags"
type = string
default = "latest"
}

variable "image" {
Expand All @@ -16,7 +15,7 @@ variable "image" {
}

variable "memory" {
description = "The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value."
description = "The amount of memory (in MiB) used by the task. Note that your cluster must have sufficient memory available to support the given value. Minimum \"256\" MiB. CPU Units will be allocated directly proportional to Memory."
type = number
default = 256
}
Expand Down Expand Up @@ -50,19 +49,19 @@ variable "default_subsystem_name" {
}

variable "private_key" {
description = "The Coralogix Send-Your-Data API key for your Coralogix account."
description = "The Send-Your-Data API key for your Coralogix account. See: https://coralogix.com/docs/send-your-data-api-key/"
type = string
sensitive = true
}

variable "metrics" {
type = bool
description = "If true, cadivisor will be deployed on each node to collect metrics"
description = "If true, collects ECS task resource usage metrics (such as CPU, memory, network, and disk) and publishes to Coralogix. See: https://github.com/coralogix/coralogix-otel-collector/tree/master/receiver/awsecscontainermetricsdreceiver"
default = false
}

variable "otel_config" {
variable "otel_config_file" {
type = string
description = "The opentelemetry configuration as a base64 encoded string. Defaults to an embedded configuration. Should accept default unless advised by Coralogix support."
description = "File path to a custom opentelemetry configuration file. Defaults to an embedded configuration. See https://opentelemetry.io/docs/collector/configuration/ and https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/coralogixexporter"
default = null
}
15 changes: 13 additions & 2 deletions tests/ecs-ec2/ecs-ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ terraform {

# Configure the AWS Provider
provider "aws" {
region = "ap-southeast-1"
# region = "ap-southeast-1"
}

variable "private_key" {
Expand All @@ -18,13 +18,24 @@ variable "private_key" {
sensitive = true
}

# To test custom config file "$(pwd)/otel_config_custom.tftpl.yaml"
# Run ```terraform plan -var otel_config_file="$(pwd)/otel_config_custom.tftpl.yaml"```
# This config file sets all logs severity to WARNING. Verify in your Coralogix account.
variable "otel_config_file" {
description = "[Optional] Path to a custom opentelemetry configuration file"
type = string
default = null
}

module "ecs-ec2" {
source = "../../modules/ecs-ec2"
ecs_cluster_name = "test-lab-cluster"
image_version = "latest"
memory = 256
coralogix_region = "Singapore"
coralogix_region = "Singapore" # TODO set dynamically from region?
default_application_name = "ecs-ec2"
default_subsystem_name = "default"
private_key = var.private_key
otel_config_file = var.otel_config_file
metrics = true
}
Loading

0 comments on commit 6c9c135

Please sign in to comment.