Search code examples
amazon-web-servicesaws-cloudformationamazon-ecs

Problems with ecs service in cloudformation: The provided target group has target type instance, which is incompatible with the awsvpc network


I am creating an architecture with cloudformation, at the moment of creating the ECS service, the error appears that my balancer instance is incompatible with the awsvpc mode

I have tried several ways and none of them works for me, I have seen the aws guides and this everything corresponds accordingly, please if it is possible to go to the solution

"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {

    "LoadBalancerQA01": {
        "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
        "Properties": {
           "SecurityGroups": [
                {
                    "Ref": "SecurityGroupPublic01"
                }
            ],
            "Subnets": [
                {
                    "Ref": "SubnetPublicQATestUno"
                },
                {
                    "Ref": "SubnetPublicQATestDos"
                }
            ],
            "Name": "LoadBalancerQA01"
        }
    },
    "LoadBalancerListener": {
      "Type": "AWS::ElasticLoadBalancingV2::Listener",
      "Properties": {
        "DefaultActions": [{
          "Type": "forward",
          "TargetGroupArn": { "Ref": "TargetGroupQA" }
        }],
        "LoadBalancerArn": { "Ref": "LoadBalancerQA01" },
        "Port": 8080,
        "Protocol": "HTTP"
      }
    },
    "TargetGroupQA": {
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "Name": "TargetGroupQA",
        "Port": 8080,
        "Protocol": "HTTP",
        "VpcId": { "Ref": "VPCQA" }
      },
      "DependsOn": [ "LoadBalancerQA01" ]
    },
    "ClusterQA": {
        "Type": "AWS::ECS::Cluster",
        "Properties": {},
        "DependsOn": [
            "SubnetPrivateQATestUno",
            "SubnetPrivateQATestDos"
        ]
    },
    "TaskQA": {
        "Type": "AWS::ECS::TaskDefinition",
        "Properties": {
            "RequiresCompatibilities": ["FARGATE"],
            "Cpu" : "1024",
            "TaskRoleArn" : "arn:aws:iam::683574420318:role/ecsTaskExecutionRole",
            "ExecutionRoleArn" : "arn:aws:iam::683574420318:role/ecsTaskExecutionRole",
            "Memory": "2048",
            "NetworkMode" : "awsvpc",
             "ContainerDefinitions" : [{ 
                        "Image": "683574420318.dkr.ecr.us-west-1.amazonaws.com/mto:latest",
                        "Cpu": "1024",
                        "Memory": "2048",  
                        "Name":"ContenedorName",
                        "PortMappings":[{ "ContainerPort": 8080,"HostPort": 8080}]

            }]
        }
    },
    "ServiceQA": {
      "Type": "AWS::ECS::Service",
      "DependsOn": [ "LoadBalancerQA01" ],
      "Properties" : {
        "NetworkConfiguration" : {
              "AwsvpcConfiguration" : {
              "AssignPublicIp" : "ENABLED",
              "SecurityGroups" : [
                {
                    "Ref": "SecurityGroupPublic01"
                }
            ],"Subnets": [
                {
                    "Ref": "SubnetPublicQATestUno"
                },
                {
                    "Ref": "SubnetPublicQATestDos"
                }
            ]}
         },
        "Cluster": { "Ref": "ClusterQA" },
        "DesiredCount": "1",
        "LoadBalancers": [
          {
            "ContainerName": "ContenedorName",
            "ContainerPort": 8080,
            "TargetGroupArn": { "Ref": "TargetGroupQA" }
          }
        ],
        "TaskDefinition" : {"Ref":"TaskQA"}
    }      
}     

Solution

  • As far as i can see, you defined TargetGroup without TargetType, which means by default it's set to instance. ECS Service needs TargetType to be set as ip, this is only option supported by awsvpc. In your CloudFormation just add:

    "TargetType": "ip",
    

    And this should fix your problem. If something still is wrong, please provide error from CloudFormation console.