When creating an AutoScalingGroup with attached AutoScalingPolicy in AWS using CloudFormation, you might get the error
the load balancer does not route traffic to the target group
The same CloudFormation script works well if the AutoScalingPolicy is excluded from the script. Also, using the ResourceLable as Output of the script without the AutoScalingPolicy, the output looks correct.
Assumption: there is an unresolved dependency between the AutoScalingPolicy and the LoadBalancer or the corresponding TargetGroup.
To ensure the dependencies are correctly resolved and the LoadBalancer as well as the TargetGroup are being created prior to the creation of the AutoScalingPolicy, use the DependsOn attribute in the AutoScalingPolicy, referencing the ELBListener:
myAutoScalingPolicy:
Type: AWS::AutoScaling::ScalingPolicy
DependsOn: myElbListener
Properties:
AutoScalingGroupName: !Ref myAutoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ALBRequestCountPerTarget
ResourceLabel: !Join
- '/'
- - !GetAtt myELB.LoadBalancerFullName
- !GetAtt myELBTargetGroup.TargetGroupFullName
TargetValue: '50'