-1

I need to run docker cmd in aws_ecs_task_definition I can directly run that in my local machine docker but unable to run that on task_defination

        docker run -it --rm \
    --name n8n \
    -p 5678:5678 \
    -e DB_TYPE=postgresdb \
    -e DB_POSTGRESDB_DATABASE=<POSTGRES_DATABASE> \
    -e DB_POSTGRESDB_HOST=<POSTGRES_HOST> \
    -e DB_POSTGRESDB_PORT=<POSTGRES_PORT> \
    -e DB_POSTGRESDB_USER=<POSTGRES_USER> \
    -e DB_POSTGRESDB_SCHEMA=<POSTGRES_SCHEMA> \
    -e DB_POSTGRESDB_PASSWORD=<POSTGRES_PASSWORD> \
    -v ~/.n8n:/home/node/.n8n \
    n8nio/n8n \
    n8n start

thats the cmd I need to run but can working fine locally but unable to from aws_ecs_task_definition

I tried to run that cmd from command inside container_definitions but unable to run that

edited

resource "aws_ecs_task_definition" "task-definition" {
  family                = "${var.PROJECT_NAME}-task-definition"
  container_definitions = jsonencode([
    {
      name      = "${var.PROJECT_NAME}-task-container"
      image     = "${var.IMAGE_PATH}"
      cpu       = 10
      memory    = 512
      essential = true
      environment = [
        {name: "DB_TYPE", value: "postgresdb"},
        {name: "DB_POSTGRESDB_DATABASE", value: "${var.DB_NAME}"},
        {name: "DB_POSTGRESDB_HOST", value: "${var.DB_NAME}"},
        {name: "DB_POSTGRESDB_DATABASE", value: "${aws_db_instance.rds.address}"},
        {name: "DB_POSTGRESDB_PORT", value: "5432"},
        {name: "DB_POSTGRESDB_USER", value: "${var.DB_USERNAME}"},
        {name: "DB_POSTGRESDB_PASSWORD", value: "${var.DB_PASSWORD}"},
      ]
      
      command   = [
        "docker", "run", 
        "-it", "--rm", 
        "--name", "${var.IMAGE_PATH}",  
        "-v", "~/.n8n:/home/node/.n8n", 
        "n8nio/n8n", 
        "n8n", "start",
        "n8n", "restart"
      ]

      portMappings = [
        {
          containerPort = 5678
          hostPort      = 5678
        }
      ]
    }
  ])

  depends_on = [
    aws_db_instance.rds
  ]
}

resource "aws_ecs_service" "service" {
  name            = "${var.PROJECT_NAME}-ecs-service"
  cluster         = aws_ecs_cluster.ecs-cluster.id
  task_definition = aws_ecs_task_definition.task-definition.arn
  desired_count   = 1
  iam_role        = aws_iam_role.ecs-service-role.arn
  depends_on      = [aws_iam_policy_attachment.ecs-service-attach]

  load_balancer {
    elb_name       = aws_elb.elb.name
    container_name = "${var.PROJECT_NAME}-task-container"
    container_port = 5678
  }

  
}
  • 1
    Sorry but what is "running command in aws_ecs_task_definition"? You mean exec to your running container which is in ECS task right? – Chuong Nguyen Nov 01 '22 at 16:17
  • yes, inside aws_ecs_task_definition I have container_definitions at their I try to pass this, and aws_ecs_task_definition was used by aws_ecs_service – user18163018 Nov 01 '22 at 16:23
  • @MarkoE in docs its says that write inside the commad attribute which I already tried, using that It gives me 503 error, for persistence of data I need to connect with db, otherwise its working for me – user18163018 Nov 01 '22 at 16:25
  • 1
    You have only showed the Docker command and there is no terraform code. So without that it's almost impossible to say what could be wrong. – Marko E Nov 01 '22 at 16:32

1 Answers1

1

The command in an ECS task definition doesn't take a docker command. It is the command that should be run inside the docker container that ECS is starting. ECS is a docker orchestration service. ECS runs the docker commands for you behind the scenes, you never give ECS a direct docker command to run.

Looking at the docker command you are running locally, the command part that is being executed inside the container is n8n start. So your command should be:

     command   = [
        "n8n", "start"
      ]

All those other docker command arguments, like the container name, volume mapping, environment variables, image ID, are all arguments that you have would elsewhere in the ECS task definition. It appears you have already specified all those arguments in your Task definition elsewhere, except for the volume mapping.

Mark B
  • 183,023
  • 24
  • 297
  • 295