question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ecs-patterns]: allow use of existing load balancer in ApplicationMultipleTargetGroupsEc2Service

See original GitHub issue

This is a 🚀 Feature Request

Description

Would like to be able to use an existing load balancer with ApplicationMultipleTargetGroupsEc2Service.

Load balancer pricing includes a fee per hour of load balancer running, which currently means about $18 per month. Running many services, each with its own balancer, can quickly add up.

Additionally, creating a separate load balancer for each service means we can’t run multiple services under the same DNS name. So it’s not possible to call different microservices depending on the path, for example (probably a common pattern?). Or group multiple subdomains on the same load balancer.

Use Case

const service1 = new ApplicationMultipleTargetGroupsEc2Service(this, "Service1",{
    ...
    targetGroups: [
             {
                containerPort: 80,
                listener: "listener",
                **pathPattern: "service1",**
                priority: 1,
                protocol: Protocol.TCP,
              },
});
const service2 = new ApplicationMultipleTargetGroupsEc2Service(this, "Service2",{ 
    targetGroups: [
              {
                containerPort: 80,
                listener: "listener",
                **pathPattern: "service2",**
                priority: 2,
                protocol: Protocol.TCP,
              },
});

I would like that that two gets deployed to (Use the same LoadBalancer, instead of creating one for Each Service):

  • myalb.amazonaws.com/service1
  • myalb.amazonaws.com/service2

Proposed Solution

Not have a workaround. Would need to skip ECS Patterns for this.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pmaouicommented, Dec 15, 2020

@ggallotti If you use 1 port to 1 service, ApplicationMultipleTargetGroupsEc2Service doesn’t look like the way to go as you would need only one target group per service. From what I learned, ECS Patterns are suitable for fairly generic needs and doesn’t support usage of existing resources like load balancers.
To use an existing ALB, I would recommend to import its listener and use the Ec2Service class . You can create many services with many target groups and linked them to one listener.

Here an example to attach one service to one target group to an existing listener:

const yourALBListener = ApplicationListener.fromLookup(this, 'appListener', {
    listenerArn: 'the:arn:of-your-elb-listener-here',
})

const service1 = new Ec2Service(this, 'Service1', {
    ...
})

const service1Target = service1.loadBalancerTarget({
    containerName: 'your-container-name-from-service1',
    containerPort: 80,
    protocol: Protocol.TCP,
})

const group1 = new ApplicationTargetGroup(this, 'DashboardTargetGroup', {
    vpc: YourVpc,
    port: 80,
    targetType: TargetType.IP,
})

const group1Rule = new ApplicationListenerRule(this, 'group1rule', {
    pathPattern: 'service1',
    listener: yourALBListener,
    priority: 10,
})

service1Target.attachToApplicationTargetGroup(group1)
group1Rule.addTargetGroup(group1)

Of course, I would be interested if someone is aware of a more concise approach.

0reactions
ggallotticommented, Dec 11, 2020

Thanks for the code @poupougnac. Seems very simple and clean. Yes, I think that for now I would need to Skip ECS Patterns for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

class ApplicationMultipleTargetGroupsEc2Service (construct)
One application load balancer with one listener and two target groups. declare const cluster: ecs.Cluster; const loadBalancedEc2Service = new ecsPatterns.
Read more >
awsecspatterns - Go Packages
A Fargate service running on an ECS cluster fronted by an application load balancer. Example: var cluster cluster loadBalancedFargateService := ecsPatterns.
Read more >
aws-cdk.aws-ecs-patterns - Python package | Snyk
ApplicationMultipleTargetGroupsEc2Service. # One application load balancer with one listener and two target groups. # cluster: ecs.
Read more >
ecs-patterns 1.182.0 javadoc (software.amazon.awscdk)
ApplicationMultipleTargetGroupsEc2Service. An EC2 service running on an ECS cluster fronted by an application load balancer.
Read more >
ApplicationLoadBalancedFargat...
I want to have the load balancer, target groups and target use that port as ... hostPort: servicePort}], }); const albFargateService = new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found