I have a pre-existing load balancer on AWS, and I would like to spin a stack with an ECS service that is targeted by that load balancer.
I found this solution that suggests using an ApplicationLoadBalancedFargateService; however, that construct only works creating a new listener, while I want to use the existing listener (or should I want a new listener?).
Reading the source code here and here, I tried to use the same functions for the looked-up listener, but apparently cdk "Cannot add default Target Groups to imported ApplicationListener" (from cdk errors).
My best attempt at the moment:
// cdk-stack.js inside the constructor
let service = new ecs.FargateService(this, id + '-service', {
serviceName: id + '-service',
cluster: cluster,
taskDefinition: taskDefinition,
securityGroups: [thisSecurityGroup],
});
const loadBalancer = elbv2.ApplicationLoadBalancer.fromLookup(this, 'bip-lb', { loadBalancerArn: loadBalancerArn });
const listener = elbv2.ApplicationListener.fromLookup(this, 'https-listener', {
loadBalancerArn: loadBalancer.loadBalancerArn,
listenerProtocol: elbv2.ApplicationProtocol.HTTPS,
listenerPort: 443,
});
const targetGroup = new elbv2.ApplicationTargetGroup(this, id + '-target-group', {
targetGroupName: id + '-target-group',
});
listener.addTargetGroups(id + '-target-group', {targetGroups: [targetGroup]});
targetGroup.addTarget(service);
Add a priority
prop to listener.addTargetGroups
. Under the hood, the method creates a new ApplicationListenerRule construct. The rule requires an explicit priority
.
For this reason, the CDK throws the observed error when addTargetGroups
does not have a priority
prop defined..