ECS Service attribute LoadBalancers

28 views
Skip to first unread message

Frank

unread,
Nov 17, 2022, 2:09:52 PM11/17/22
to cloudtools-dev
Hi,

I have some issue, I created a ECS Fargate Service and I should add the ALB to the "ECS Service". The issue is especifically with the attribute "LoadBalancers". 

So below in #1 you will see the error, in #2 you will see my Fargate stack, where the attribute "LoadBalancers" looks fine, well formatted and so for at first view, added just in case in #3 my ALB stack, and in #4 the class itself.

I don't know where the issue but there is something that troposphere doesn't like about it :-).

I picked that attribute format from some working template that I have converted (with cfn2py) to .py. This is how it looks: LoadBalancers=[{ "ContainerName": "product-service", "ContainerPort": 8001, "TargetGroupArn": Ref("TargetGroup") }]

What do you think I am missing in here?

Any advises are more than welcome, cheers.
-------code below-----

1) TypeError: <class 'troposphere.ecs.Service'>: SrvCustom002.LoadBalancers is <class 'dict'>, expected [<class 'troposphere.ecs.LoadBalancer'>]

2)
################
# Fargate stack
################

cluster = t.add_resource(Cluster("Cluster"))

task_definitiTypeError: <class 'troposphere.ecs.Service'>: SrvCustom002.LoadBalancers is <class 'dict'>, expected [<class 'troposphere.ecs.LoadBalancer'>]on = t.add_resource(
TaskDefinition(
"TaskDefinition",
RequiresCompatibilities=["FARGATE"],
Cpu="256",
Memory="512",
NetworkMode="awsvpc",
ExecutionRoleArn=varis.var_ExecutionRoleArn,
ContainerDefinitions=[
ContainerDefinition(
Name="td_viaplay002",
Image=varis.var_Image,
Essential=True,
PortMappings=[PortMapping(ContainerPort=8080)],
)
],
)
)

service = t.add_resource(
Service(
"SrvCustom002",
Cluster=Ref(cluster),
DesiredCount=1,
TaskDefinition=Ref(task_definition),
LaunchType="FARGATE",
LoadBalancers=[{
"ContainerName": "custom_node",
"ContainerPort": 8080,
"TargetGroupArn": Ref("TargetGroupEcs"),
"LoadBalancerName": Ref("ApplicationElasticLB")
}],
NetworkConfiguration=NetworkConfiguration(
AwsvpcConfiguration=AwsvpcConfiguration(Subnets=[Ref("SubnetA"),Ref("SubnetB")],
AssignPublicIp="ENABLED",
SecurityGroups=[Ref(fargateSecurityGroup)],)
),
#DependsOn="TargetGroupEcs",
)
)

3)
################
# ALB stack
################

ApplicationElasticLB = t.add_resource(
elb.LoadBalancer(
"ApplicationElasticLB",
Name="ApplicationElasticLB",
Scheme="internet-facing",
Subnets=[Ref(subnetA),Ref(subnetB)],
)
)

TargetGroupEcs = t.add_resource(
elb.TargetGroup(
"TargetGroupEcs",
HealthCheckIntervalSeconds="30",
HealthCheckProtocol="HTTP",
HealthCheckTimeoutSeconds="10",
HealthyThresholdCount="4",
Matcher=elb.Matcher(HttpCode="200"),
Name="tg-fargate",
Port="8080",
Protocol="HTTP",
UnhealthyThresholdCount="3",
VpcId=Ref(VPCResource),
)
)
Listener = t.add_resource(
elb.Listener(
"Listener",
Port="8080",
Protocol="HTTP",
LoadBalancerArn=Ref(ApplicationElasticLB),
DefaultActions=[
elb.Action(Type="forward", TargetGroupArn=Ref(TargetGroupEcs))
],
)
)
t.add_resource(
elb.ListenerRule(
"ListenerRuleEcs",
ListenerArn=Ref(Listener),
Conditions=[elb.Condition(Field="path-pattern", Values=["/"])],
Actions=[elb.ListenerRuleAction(Type="forward", TargetGroupArn=Ref(TargetGroupEcs))],
Priority="1",
)
)

4)
class LoadBalancer(AWSProperty):
    """
    `LoadBalancer <http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-taskset-loadbalancer.html>`__
    """

    props: PropsDictType = {
        "ContainerName": (str, False),
        "ContainerPort": (validate_network_port, False),
        "LoadBalancerName": (str, False),
        "TargetGroupArn": (str, False),
    }


Mark Peek

unread,
Nov 17, 2022, 6:29:18 PM11/17/22
to Frank, cloudtools-dev
This might be better to discuss as an issue on GitHub primarily to have better code formatting. But here goes:

You currently have a dictionary in the LoadBalancers list while you need a list of LoadBalancer objects like this:

service = t.add_resource(
    Service(
        "SrvCustom002",
        Cluster=Ref(cluster),
        DesiredCount=1,
        TaskDefinition=Ref(task_definition),
        LaunchType="FARGATE",
        LoadBalancers=[
            LoadBalancer(
                ContainerName="custom_node",
                ContainerPort=8080,
                TargetGroupArn=Ref("TargetGroupEcs"),
                LoadBalancerName=Ref("ApplicationElasticLB"),
            ),
        ],
        NetworkConfiguration=NetworkConfiguration(
            AwsvpcConfiguration=AwsvpcConfiguration(

                Subnets=[Ref("SubnetA"),Ref("SubnetB")],
                AssignPublicIp="ENABLED",
                SecurityGroups=[Ref(fargateSecurityGroup)],
            ),

        ),
        #DependsOn="TargetGroupEcs",
    )
)
--
You received this message because you are subscribed to the Google Groups "cloudtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloudtools-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloudtools-dev/74d649c2-9641-41ed-b57a-9260872efd99n%40googlegroups.com.

Frank

unread,
Nov 21, 2022, 6:19:24 AM11/21/22
to cloudtools-dev
Hi Mark,

Kind of you for the help, as you suggested I will raise an issue on Github for this case, cheers.

Reply all
Reply to author
Forward
0 new messages