Search code examples
amazon-web-servicesterraformamazon-ecsecs-taskdefinitionn8n

How can run cmd in aws_ecs_task_definition terraform aws


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
  }

  
}

Solution

  • 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.