ecs-cli compose service up with a load balancer

2020-07-22 19:06发布

问题:

I am trying to use ecs-cli compose to manage my services and tasks on Amazon ECS.

I'm unable to find a way using the service up command to create a new service with an application load balancer (even when that load balancer already exists).

This seems possible with service create, but the API is different from the service up API, and I'm not sure how to specify params in the same way with create. And it would generally be preferable to use just the up command for consistency. The documentation is pretty scattered, there's many different ways to do the same things, just wondering what best practice is here. Any suggestions greatly appreciated.

Worth noting, everything is working for me, so long as I have an existing task definition and I create my service through the Amazon AWS GUI while specifying the load balancer. So I'm thinking about moving all my compose config into a task-definition.json and use it directly with aws ecs cli.

I have a working docker-compose.yml file:

# docker-compose.yml

version: "3"

services:
  application:
    image: ${IMAGE_ARN}
    command: npm start
    ports:
      - "8000:8000"
  nginx:
    image: ${IMAGE_ARN}
    ports:
      - "80:80"

And an accompanying ecs-params.yml file:

# ecs-params.yml

version: 1

task_definition:
  task_role_arn: ${ROLE_ARN}
  task_execution_role: ${ROLE_ARN}
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 0.5GB
    cpu_limit: 256
  container_definitions:
    - name: application
    - name: nginx

run_params:
  network_configuration:
    awsvpc_configuration:
      assign_public_ip: ENABLED
      subnets:
        - ${SUBNET_1_ID}
        - ${SUBNET_2_ID}
      security_groups:
        - ${SECURITY_GROUP_ID}

The command that I run to bring the service up is:

ecs-cli compose service up \
--file docker-compose.yaml \
--ecs-params ecs-params.yaml \
--project-name service-name

Any way to specify the load balancer configuration through that command?

回答1:

It seems like latest ecs-cli version does support load balancer configuration with service up.

Have you tried providing --target-group-arn option?. Assuming you have already created ALB and Target Group to associate ECS service. Here is sample command I just tested.

ecs-cli compose --file docker-compose.yaml --project-name nginx \
--ecs-params ecs-params.yaml service up \
--target-group-arn "arn:aws:elasticloadbalancing:us-east-1:xxxxxxxx:targetgroup/awsvpc-nginx/2bf8921935c827bd" \
--container-name nginx --container-port 80

Note -

  1. target-group-arn, container-name and container-port options are mandatory for load balancer association and they have to be provided in command after service up.
  2. I see you are trying to use awsvpc mode for the tasks. I am not sure you are trying to bring up EC2 or Fargate type launch container.
  3. If you do want awsvpc mode then please make sure your load balancer target group has target created with type ip instead of instance.
  4. If you are on EC2 launch type but with awsvpc mode then please make sure EC2 AMI is Amazon-ECS Optimized AMI. If you are on Fargate type then your assign_public_ip should DISABLED.

Do let me know your feedback.

Reference - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-compose-service.html

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html