I created an NLB and a fargate service.
Then i create a target group with "ip" of my ecs instance.
When i now add a fargate ip to my target group, it works, but how does the scaling work? Suppose ecs has to scale out, i will have to register another ip, but i want it to scale automatically.
Let us say one task is added. How does the network load balancer the new task ip without me manually adding it?
I do not get, how the link is between the nlb and the service of ecs. Does amazon does add targets implicitly?
2
Answers
Instead of manually registering the IP of your Fargate task with the target group, you are supposed to configure the ECS service with knowledge of the load balancer you want to use. The ECS service will then automatically register every task that it creates as part of deployments and auto-scaling.
You need to create a Network Target Group, Attached the Fargate Service to Network Target group. Then Scale the Fargate based on the CPU/Memory Metrics.
CDK Code Example:
https://github.com/go-fireball/cdk-examples/blob/main/nlb-fargate/lib/stacks/nlb-fargate-stack.ts