diff --git a/README.md b/README.md index 49719e3..a31d57f 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,18 @@ custom: my-task-with-load-balancers: image: 123456789369.dkr.ecr.eu-west-1.amazonaws.com/my-image loadBalancers: - - port: 8080 - arn: ${self:custom.httpTargetGroupArn} - - port 8443 - arn: ${self:custom.httpsTargetGroupArn} + # You can use the arn of an existing target group already associated with a load balancer + - hostPort: 8080 + containerPort: 8080 + targetGroup: ${self:custom.httpTargetGroupArn} + # If you're using an ELB/target group that you're building in the Resources section of + # serverless.yml, you may run into an issue where the elb and default listener rule are + # not created prior to the target group/service. In this scenario you can specify the + # logical name of the resource so that the appropriate dependencies can be set. + - hostPort: 8443 + containerPort: 8443 + targetGroup: ${self:custom.httpsTargetGroupArn} + elbResource: "my-task-elb" ``` Advanced usage diff --git a/lib/index.js b/lib/index.js index dd06102..52e7ea1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -174,14 +174,21 @@ class ServerlessFargateTasks { // Configure Load Balancers if defined. if(options.tasks[identifier].hasOwnProperty('loadBalancers')) { let loadBalancers = []; + let dependsOn = []; options.tasks[identifier]['loadBalancers'].forEach(item => { const loadBalancer = Object.assign({ 'ContainerName': name, 'ContainerPort': item.containerPort, - 'TargetGroupArn': item.arn + 'TargetGroupArn': item.targetGroup }); loadBalancers.push(loadBalancer); + if(item.hasOwnProperty('elbResource')) { + dependsOn.push(item.elbResource) + } }); + if(dependsOn.length > 0) { + service['DependsOn'] = dependsOn; + } service['Properties']['LoadBalancers'] = loadBalancers; } template['Resources'][normalizedIdentifier + 'Service'] = service