ApplicationLoadBalancedFargateService with load balancer, target groups, targets on non-standard port
I have an ECS service that exposes port 8080. I want to have the load balancer, target groups and target use that port as opposed to port 80. Here is a snippet of my code:
const servicePort = 8888;
const metricsPort = 8888;
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef');
const repository = ecr.Repository.fromRepositoryName(this, 'cloud-config-server', 'cloud-config-server');
taskDefinition.addContainer('Config', {
image: ecs.ContainerImage.fromEcrRepository(repository),
portMappings: [{containerPort : servicePort, hostPort: servicePort}],
});
const albFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'AlbConfigService', {
cluster,
publicLoadBalancer : false,
taskDefinition: taskDefinition,
desiredCount: 1,
});
const applicationTargetGroup = new elbv2.ApplicationTargetGroup(this, 'AlbConfigServiceTargetGroup', {
targetType: elbv2.TargetType.IP,
protocol: elbv2.ApplicationProtocol.HTTP,
port: servicePort,
vpc,
healthCheck: {path: "/CloudConfigServer/actuator/env/profile", port: String(servicePort)}
});
const addApplicationTargetGroupsProps: elbv2.AddApplicationTargetGroupsProps = {
targetGroups: [applicationTargetGroup],
};
albFargateService.loadBalancer.addListener('alb-listener', {
protocol: elbv2.ApplicationProtocol.HTTP, port: servicePort, defaultTargetGroups: [applicationTargetGroup]}
);
}
}
This does not work. The health check is taking place on port 80 with the default URL of "/" which fails, and the tasks are constantly recycled. A target group on port 8080, with the appropriate health check, is added, but it has no targets.
What is the recommended way to achieve load balancing on a port other than 80?
thanks
Hello there,
As ApplicationLoadBalancedFargateService
is a high level construct, when using that we don't need to configure target group port and listener port (default will be 80 or 443). As long as we provide the container port (service port) in task definition, ApplicationLoadBalancedFargateService
will take care of the port and connection configured correctly from ALB to ECS tasks.
So in your case, you don't need to specify applicationTargetGroup
, addApplicationTargetGroupsProps
, and albFargateService.loadBalancer.addListener
. ApplicationLoadBalancedFargateService
has already done that on your behalf.
You can reference to the doc to get a better understanding.
Let me know if you have any follow up questions.
Relevant questions
how to create multiple ingress port on Amazon EKS using single Application Load Balancer
asked 2 months agoApplicationLoadBalancedFargateService with load balancer, target groups, targets on non-standard port
Accepted Answerasked 25 days agoHow do I set up dynamic port mapping for Amazon ECS?
asked 9 months agoBlue/Green on Fargate Load Balancer swap Target Group after deploy
asked a year agoLoad balancer HTTPS to EC2 instance through HTTP
asked 3 years agoCreate ECS service using existing load balancer with existing target group
asked 4 months agoLoad balancer page in AWS console doesn't load
Accepted Answerasked 2 months agoMy Network Load Balancer is not enforcing the target Security group
asked 3 years agoECS fails to remove a task from the load balancer target group?
asked a month agoCannot create ECS service with existing load balancer
asked 3 years ago
thanks, i'll give that a try