DEV Community

Cover image for How to create an ECS Service with more than one Target Group
Federico Navarrete
Federico Navarrete

Posted on • Edited on • Originally published at supernovaic.blogspot.com

How to create an ECS Service with more than one Target Group

You might have used the AWS GUI to create ECS services and all went smoothly until you could not attach more than one target group. That's why you came to this blog. At the present time, it is a limitation that AWS doesn't plan to address which can be annoying when you plan to use an NLB and an ALB, at the same time.

When do you do that, you might be wondering? When you have services like Derby DB or SQLite, and you want to retain the same IP. At the present time, the only way to associate multiple target groups to a service is by using the AWS CLI. The aws ecs create-service command must be used with a JSON file as input.

Pre-requisites

  • The Task Definition must already exist. You need to get its ARN like: aws:ecs:eu-west-2:21334324232:task-definition/app:1
  • The Target Group for the HTTPS port must exist. It must be of type IP and must be associated with an ALB.
  • The Target Group for the TCP (1532 port, for instance) must exist. It must be of type IP and must be associated with the NLB.

Execution

Configure a JSON with your own logic and the two target groups:



{
    "serviceName": "My-Service-Name",
    "cluster": "My-Cluster",
    "taskDefinition": "aws:ecs:eu-west-2:21334324232:task-definition/app:1",
    "loadBalancers": [
        {
            "targetGroupArn": "arn:aws:elasticloadbalancing:eu-west-2:21334324232:targetgroup/ecs-defaul-app/d4as56da4s56as",
            "containerName": "my-app",
            "containerPort": 443
        },
        {
            "targetGroupArn": "arn:aws:elasticloadbalancing:eu-west-2:21334324232:targetgroup/ecs-default-app-db/4das564565eda",
            "containerName": "my-app",
            "containerPort": 1539
        }
    ],
    "serviceRegistries": [],
    "desiredCount": 1,
    "launchType": "FARGATE",
    "platformVersion": "LATEST",
    "deploymentConfiguration": {
        "deploymentCircuitBreaker": {
            "enable": false,
            "rollback": false
        },
        "maximumPercent": 200,
        "minimumHealthyPercent": 100
    },
    "networkConfiguration": {
        "awsvpcConfiguration": {
            "subnets": [
                "subnet-4das64a645a6sa454"
            ],
            "securityGroups": [
                "sg-1d2asd1a3s21a35"
            ],
            "assignPublicIp": "DISABLED"
        }
    },
    "healthCheckGracePeriodSeconds": 300,
    "schedulingStrategy": "REPLICA",
    "deploymentController": {
        "type": "ECS"
    }
}


Enter fullscreen mode Exit fullscreen mode

The most important configurations:

  • serviceName: Your service name.
  • cluster: The cluster name.
  • taskDefinition: Your repo name.
  • loadBalancers: Your target groups.
  • awsvpcConfiguration: Where you choose your subnets and security groups. Run the following command in the AWS CLI:

aws ecs create-service --profile My-Profile --service-name "My-Service-Name" --cli-input-json file://My-JSON.json

Where:

  • --profile is the AWS profile that contains your credentials.
  • --service-name is the new name for your service.
  • --client-input-json is the AWS JSON logic.

And that's all that you need!

Follow me on:

Personal LinkedIn YouTube Instagram Cyber Prophets Sharing Your Stories
Personal LinkedIn YouTube Instagram RedCircle Podcast RedCircle Podcast

sponsor me

Top comments (0)