How to create an ECS Service with more than one Target Group
Tags: aws, targetgroup, fargate, docker
Published: 2024-05-03 06:00:00


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:

```json
{
    "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"
    }
}
```

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](https://raw.githubusercontent.com/FANMixco/federiconavarrete/master/img/favicons/favicon.png)](https://federiconavarrete.com)|[![LinkedIn](https://i.stack.imgur.com/idQWu.png)](https://www.linkedin.com/in/federiconavarrete)|[![YouTube](https://i.stack.imgur.com/CFPMR.png)](https://youtube.com/c/FedericoNavarrete)|[![Instagram](https://i.stack.imgur.com/PIfqY.png)](https://www.instagram.com/federico_the_consultant)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/cyber-prophets)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/sharing-your-stories)|


[![sponsor me](https://raw.githubusercontent.com/FANMixco/Xamarin-SearchBar/master/bmc-rezr5vpd.gif)](https://www.buymeacoffee.com/fanmixco)