copilot-cli icon indicating copy to clipboard operation
copilot-cli copied to clipboard

Custom listener rules for load-balanced service

Open benjaminpottier opened this issue 3 years ago • 1 comments

I'm not sure if there is a recommend way to do this already or this is a feature request, but we'd like to be able to add custom listener rules to our load-balanced service without having to edit the rules manually.

We're using a cloudfront distribution to serve our frontend site from S3 and we use the load-balanced service as an origin for our api. We have to add a custom rule to forward to our service using only the path and not the path and host.

Let me know if you need more info about what we're trying to accomplish.

benjaminpottier avatar Sep 02 '22 12:09 benjaminpottier

Hello! I wonder if you've tried to add the listener rule as an addon to the service? Something like:

# In copilot/<svc>/addons/template.yml
Parameters:
  # Required parameters by AWS Copilot.
  App:
    Type: String
  Env:
    Type: String
  Name:
    Type: String
  # Additional parameters defined in addons.parameters.yml
  TargetGroup:
    Type: String

TheCustomListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
        - Type: forward
          TargetGroupArn: !Ref TargetGroup
      Conditions:
        - Field: 'path-pattern'
          PathPatternConfig:
            Values: // your path
      ListenerArn: 
        Fn::ImportValue: !Sub '${App}-${Env}-HTTPSListenerArn' # To add the rule to the HTTP listener instead, write "!Sub ${App}-${Env}-HTTPListenerArn"
      Priority: // the priority

Lou1415926 avatar Sep 06 '22 19:09 Lou1415926

This issue is stale because it has been open 60 days with no response activity, and is tagged with pending/question. Remove the stale label, add a comment, or this will be closed in 14 days.

github-actions[bot] avatar Nov 27 '22 00:11 github-actions[bot]

This issue is closed due to inactivity. Feel free to reopen the issue if you have any follow-ups!

github-actions[bot] avatar Dec 12 '22 00:12 github-actions[bot]

@Lou1415926

Hello, I followed your code snippets, but I faced undefined of resource errors. It only works when i define Service resource in addon(addons.parameters.yml and addon yml file.)

How can I !Ref or !GetAtt to set ListenerArn value for HTTPSListener?

Should I have to hard-code real ARN? Is there best practice to follow?

housekorea avatar Jan 03 '23 13:01 housekorea

@housekorea

but I faced undefined of resource errors. It only works when i define Service resource in addon(addons.parameters.yml and addon yml file.)

This sounds unexpected! Can you share how you defined Service in addons.parameters.yml and how you use it in addon.yml?

How can I !Ref or !GetAtt to set ListenerArn value for HTTPSListener?

I've refined my previous answer a little so that HTTPSListener ARN isn't a parameter, but rather directly imported from the environment stack:

The "addons.yml" file might look like this:

# In copilot/<svc>/addons/template.yml
Parameters:
  # Required parameters by AWS Copilot.
  App:
    Type: String
  Env:
    Type: String
  Name:
    Type: String
  # Additional parameters defined in addons.parameters.yml
  TargetGroup:
    Type: String

TheCustomListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
        - Type: forward
          TargetGroupArn: !Ref TargetGroup
      Conditions:
        - Field: 'path-pattern'
          PathPatternConfig:
            Values: // your path
      ListenerArn: 
        Fn::ImportValue: !Sub '${App}-${Env}-HTTPSListenerArn' # To add the rule to the HTTP listener instead, write "!Sub ${App}-${Env}-HTTPListenerArn"
      Priority: // the priority

Lou1415926 avatar Jan 03 '23 20:01 Lou1415926

@Lou1415926

Thank you,

Unfortunately, I lost the code which did not work. But, your code snippet works.

After read again addon explanation page in copilot docs and searching what is addon in cloudformation context, I can understand and work well.

Your code snippet and answer were helpful to me!

housekorea avatar Jan 04 '23 11:01 housekorea