Reputation: 15066
I'm using an ALB in my Elastic Beanstalk environment. It works (on 80 and 443) but I want to implement a redirect rule in the cloudformation template.
I was able to create the rule in the console:
If PATH is / Redirect to HTTPS://#{host}:443/app?#{query}
How can I do this for an ALB in Elastic Beanstalk in CloudFormation?
Upvotes: 8
Views: 1822
Reputation: 3361
I want to elaborate on Chris Pollard's answer.
After spending some energy on this, I ended up keeping the default listener on port 80, and attaching two listener rules. One for the HTTPS redirect, and one to allow healtchecks without the HTTP redirect. Notice the priority. Rules are evaluated in ascending priority order, starting with 1. The default rule of a Listener is evaluated last.
This allows me to enable HTTPS redirect on my Elastic Beanstalk load balancer while maintaining healtchecks on port 80 without the redirect.
Resources:
ALBListenerRuleHealthcheck:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn:
Ref: AWSEBV2LoadBalancerTargetGroup
Conditions:
- Field: path-pattern
PathPatternConfig:
Values:
- /api/healthcheck
ListenerArn:
Ref: AWSEBV2LoadBalancerListener
Priority: 1
ALBListenerRuleRedirect:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: redirect
RedirectConfig:
Protocol: "HTTPS"
Host: "#{host}"
Query: "#{query}"
Path: "/#{path}"
Port: 443
StatusCode: "HTTP_301"
Conditions:
- Field: path-pattern
PathPatternConfig:
Values:
- /*
ListenerArn:
Ref: AWSEBV2LoadBalancerListener
Priority: 2
Upvotes: 0
Reputation: 1780
You can add an EB extension that is a cloudformation snippet. It would look something like this:
albRedirect:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: redirect
RedirectConfig:
Protocol: HTTPS
Host: '#{host}'
Query: '#{query}'
Path: '/#{path}'
Port: '443'
StatusCode: HTTP_301
LoadBalancerArn: !Ref AWSEBV2LoadBalancer
Port: 80
Protocol: HTTP
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-resources.html
Upvotes: 6