How to add ALB Rule to existing Serverless template

amazon-cloudformation

So I am trying to add a rule for /synchrony/* which would point to the 'synchrony' target group.

Here is my existing template.

ConfluenceALB:
  Properties:
    Scheme: internal
    SecurityGroups:
    - Ref: ConfluenceAlbSg
    - Ref: ConfluenceAsgSg
    Subnets:
      - Fn::ImportValue: ${self:custom.${opt:stage}-VpcName, self:custom.${self:provider.stage}-VpcName}-PrivateSubnet1Id
      - Fn::ImportValue: ${self:custom.${opt:stage}-VpcName, self:custom.${self:provider.stage}-VpcName}-PrivateSubnet2Id
      - Fn::ImportValue: ${self:custom.${opt:stage}-VpcName, self:custom.${self:provider.stage}-VpcName}-PrivateSubnet3Id
    Tags:
    - Key: Name
      Value:
          Fn::Join: [ "-", [ Ref: "AWS::StackName", "confluencealb" ] ]
  Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"

ConfluenceAlbListener:
  Properties:
    Certificates:
      - CertificateArn: ${self:custom.${opt:stage}-SSLCertId, self:custom.${self:provider.stage}-SSLCertId}
    DefaultActions:
    - Type: forward
      TargetGroupArn:
        Ref: ConfluenceTargetGroup
    LoadBalancerArn:
      Ref: ConfluenceALB
    Port: 443
    Protocol: HTTPS
  Type: AWS::ElasticLoadBalancingV2::Listener

ConfluenceTargetGroup:
  Properties:
    HealthCheckIntervalSeconds: 60
    UnhealthyThresholdCount: 10
    HealthCheckPath: /
    Name: "confluence" 
    Port: 8080
    Protocol: HTTP
    VpcId:
      Fn::ImportValue: ${self:custom.${opt:stage}-VpcName, self:custom.${self:provider.stage}-VpcName}-VpcId
  Type: AWS::ElasticLoadBalancingV2::TargetGroup

SynchronyTargetGroup:
  Properties:
    Name: "synchrony" 
    Port: 8091
    Protocol: HTTP
    VpcId:
      Fn::ImportValue: ${self:custom.${opt:stage}-VpcName, self:custom.${self:provider.stage}-VpcName}-VpcId
  Type: AWS::ElasticLoadBalancingV2::TargetGroup

I'm not sure how to add this and AWS documentation (cloudformation) appears to be sparse. Do I add this under the listener block?

Best Answer

Have been investigating how this is done for a new project and came across this example: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ecs.html (as great as the AWS docs are, sometimes they're almost obfuscated!)

This the relevant section for you I think:

ECSALBListenerRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
DependsOn: ALBListener
Properties:
  Actions:
  - Type: forward
    TargetGroupArn: !Ref 'ECSTG'
  Conditions:
  - Field: path-pattern
    Values: [/]
  ListenerArn: !Ref 'ALBListener'
  Priority: 1

Whereby it refers to both the Listener resource 'ALBListener' and the Security Group 'ECSTG'. The example is about ECS but don't think it really matters for the answer you were after.