'How do you bind an external config file saved locally to an ECS task definition?

Context:

  • I have an application running on EC2, elastically scaled in an ECS cluster
  • I'm looking to deploy a Prometheus & Grafana instances on the same cluster to scrape/visualize metrics from the application running on multiple EC2s
  • all deployed with Terraform

Issue:

  • as part of the task definition for Prometheus, I want to provide a prometheus.yml config file that is saved locally
  • prometheus.yml is located at './prometheus/prometheus.yml' relative to the terraform ecs task definition resource
  • but the Prometheus server won't start because task definition cannot find the yml file, with the error:
error loading config from \"/etc/prometheus/prometheus.yml\": couldn't load configuration (--config.file=\"/etc/prometheus/prometheus.yml\"): 
open /etc/prometheus/prometheus.yml: no such file or directory

Basically if I'm just running the Prometheus docker image, I want to do the equivalent of:

docker run \
    -p 9090:9090 \
    -v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

How do I put the source path /prometheus/prometheus.yml to the task definition? Thanks in advance for your help.

This is my Terraform resource for aws_ecs_task_definition:

resource "aws_ecs_task_definition" "prometheus" {
  family                = var.name
  network_mode          = "awsvpc"
  container_definitions = data.template_file.prometheus.rendered

  task_role_arn = aws_iam_role.prometheus.arn

  volume {
    name      = "config"
    host_path = "/etc/prometheus"
  }

  volume {
    name      = "data"
    host_path = "/var/lib/prometheus"
  }

  volume {
    name      = "grafana"
    host_path = "/var/lib/grafana"
  }

  volume {
    name      = "alertmanager"
    host_path = "/etc/alertmanager"
  }
}

data "template_file" "prometheus" {
  template = file("${path.module}/files/prometheus_task.json")

  vars = {
    aws_region = var.region
  }
}

and this is the json for container definition:

[
  {
    "command": [
      "--config.file=/etc/prometheus/prometheus.yml"
    ],
    "cpu": 0,
    "entryPoint": null,
    "environment": [],
    "essential": true,
    "image": "prom/prometheus:v2.14.0",
    "user": "0:0",
    "memory": 1024,
    "memoryReservation": null,
    "mountPoints": [
      {
        "containerPath": "/etc/prometheus",
        "readOnly": null,
        "sourceVolume": "config"
      },
      {
        "containerPath": "/prometheus/data",
        "readOnly": null,
        "sourceVolume": "data"
      }
    ],
    "name": "prometheus",
    "portMappings": [
      {
        "containerPort": 9090,
        "hostPort": 9090,
        "protocol": "tcp"
      }
    ],
    "volumesFrom": [],
    "logConfiguration": {
      "logDriver": "awslogs",
      "secretOptions": null,
      "options": {
        "awslogs-group": "odp-prometheus-logs",
        "awslogs-region": "us-gov-west-1",
        "awslogs-stream-prefix": "prometheus"
      }
    }
  },
  {
    "command": [
      "--directory",
      "/etc/prometheus/"
    ],
    "cpu": 0,
    "entryPoint": [],
    "environment": [
      {
        "name": "AWS_DEFAULT_REGION",
        "value": "${aws_region}"
      }
    ],
    "essential": true,
    "image": "3h4x/prometheus-ecs-sd:v0.0.1",
    "memory": 256,
    "memoryReservation": null,
    "mountPoints": [
      {
        "containerPath": "/etc/prometheus",
        "readOnly": null,
        "sourceVolume": "config"
      }
    ],
    "name": "prometheus-ecs-discovery",
    "volumesFrom": [],
    "logConfiguration": {
      "logDriver": "awslogs",
      "secretOptions": null,
      "options": {
        "awslogs-group": "odp-prometheus-logs",
        "awslogs-region": "us-gov-west-1",
        "awslogs-stream-prefix": "prometheus-ecs-discovery"
      }
    }
  },
  {
    "command": [],
    "cpu": 0,
    "entryPoint": [],
    "environment": [],
    "essential": true,
    "portMappings": [
      {
        "containerPort": 3000,
        "hostPort": 3000,
        "protocol": "tcp"
      }
    ],
    "image": "grafana/grafana:8.3.0",
    "user": "0",
    "memory": 1024,
    "memoryReservation": null,
    "mountPoints": [
      {
        "containerPath": "/var/lib/grafana",
        "readOnly": null,
        "sourceVolume": "grafana"
      }
    ],
    "name": "grafana",
    "volumesFrom": [],
    "logConfiguration": {
      "logDriver": "awslogs",
      "secretOptions": null,
      "options": {
        "awslogs-group": "odp-prometheus-logs",
        "awslogs-region": "us-gov-west-1",
        "awslogs-stream-prefix": "grafana"
      }
    }
  },
  {
    "command": [],
    "cpu": 100,
    "entryPoint": [],
    "environment": [],
    "essential": true,
    "portMappings": [
      {
        "containerPort": 9093,
        "hostPort": 9093,
        "protocol": "tcp"
      }
    ],
    "image": "prom/alertmanager:v0.19.0",
    "memory": 64,
    "memoryReservation": null,
    "mountPoints": [
      {
        "containerPath": "/etc/alertmanager",
        "readOnly": null,
        "sourceVolume": "alertmanager"
      }
    ],
    "name": "alertmanager",
    "volumesFrom": [],
    "logConfiguration": {
      "logDriver": "awslogs",
      "secretOptions": null,
      "options": {
        "awslogs-group": "odp-prometheus-logs",
        "awslogs-region": "us-gov-west-1",
        "awslogs-stream-prefix": "alert-manager"
      }
    }
  }
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source