skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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

        const networkTargetGroup = new cdk.aws_elasticloadbalancingv2.NetworkTargetGroup(this, `${config.environment}-${config.appName}-net-target-group`, {
            targetGroupName: `${config.environment}-${config.appName}-api`,
            port: 80,
            vpc: vpc,
            deregistrationDelay: Duration.seconds(0),
            connectionTermination: true,
            protocol: Protocol.TCP_UDP,
            targetType: TargetType.IP,
            healthCheck:{
                protocol: Protocol.HTTP,
                path: '/api/health',
            }
        }) 
         ...
         ...
        fargateService.attachToNetworkTargetGroup(networkTargetGroup);
         ...
         ...
        const applicationScalingTarget = fargateService.autoScaleTaskCount({
            minCapacity: 2,
            maxCapacity: 5,
        }); 
         applicationScalingTarget.scaleOnCpuUtilization (`${config.environment}-${config.appName}-api-scaling`,{
            targetUtilizationPercent: 50,
        })
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search