Cloudformation ECS / fargate - Run two containers in one task

657 views Asked by At

I'm trying to run two containers in one task. The two containers must be resolvable using their DNS.

What I did ; I defined the two containers in the same task definition :

    MyTwoContainerTaskDefinition:
    Type: 'AWS::ECS::TaskDefinition'
    Properties:
      NetworkMode: awsvpc
      RuntimePlatform:
        OperatingSystemFamily: LINUX
      RequiresCompatibilities:
        - FARGATE
      ContainerDefinitions:
        - Name: container1
          ...
        - Name: container2
          ...
...

And then I use two (one for each container) ServiceDiscovery resources and two Service resources to permit the DNS resolution :

  Container1CloudmapDiscoveryservice:
    Type: AWS::ServiceDiscovery::Service
...
  Container1Service:
    Type: 'AWS::ECS::Service'
    Properties:
      ServiceName: container1
      DesiredCount: 1
      LaunchType: FARGATE
      TaskDefinition: !Ref MyTwoContainerTaskDefinition
      ServiceRegistries:
        - RegistryArn: !GetAtt Container1CloudmapDiscoveryservice.Arn
          Port: 7070
... 

And the same resources for container 2.

The deployment is working but when I go to AWS portal I have two tasks that are containing the two containers. I would like to have only one task containing my two containers.

Do you know if it's possible and what I'm missing ?

1

There are 1 answers

0
Seba On

Yes, it's possible to have multiple containers in one task definition. See here: AWS ECS start multiple containers in one task definition