Redirect http to https with ALB via CloudFormation - Feature request

Hi all.

I was trying to redirect from http to https via CloudFormation in AWS. AWS says this feature has been released already [1]. The official documentation about it can be found here [2].

However, when deploying via CFN, I get the following error: "You cannot specify 'target group ARN' for actions of type 'redirect'"

After speaking with a CloudFormation support expert, turns out there is a problem :stuck_out_tongue: . You can only do that now from the console, not via CFN.

It is not possible to set the Listener type as “Redirect” at this stage, which is needed when redirecting to https. Only “forward” works.

There is already a feature request for this in place. If you guys could help [3]. :slight_smile:

[1] https://aws.amazon.com/about-aws/whats-new/2018/07/elastic-load-balancing-announces-support-for-redirects-and-fixed-responses-for-application-load-balancer/

[2] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-listener-redirectconfig.html

[3] https://forums.aws.amazon.com/thread.jspa?messageID=880333󖻍

UPDATE 2018-12-07:

Thanks to @daxlreod, turns out the AWS documentation is not quite correct, plus they need to get their staff better training.

All I needed to do was to have two listeners, like this:

"ALBListenerHTTP" : {

"Type" : "AWS::ElasticLoadBalancingV2::Listener",

"Properties" : {

"DefaultActions" : [{

"Type" : "redirect",

"RedirectConfig" : {

"Host" : "#{host}",

"Path" : "/#{path}",

"Port" : "443",

"Protocol" : "HTTPS",

"Query" : "#{query}",

"StatusCode" : "HTTP_302"

},

}],

"LoadBalancerArn" : { "Ref" : "ELBRESOURCE" },

"Port" : "80",

"Protocol" : "HTTP"

}

},

"ALBListener" : {

"Type" : "AWS::ElasticLoadBalancingV2::Listener",

"Properties" : {

"DefaultActions" : [{

"Type" : "forward",

"TargetGroupArn" : { "Ref" : "ALBTargetGroup" }

}],

"LoadBalancerArn" : { "Ref" : "ELBRESOURCE" },

"SslPolicy": "ELBSecurityPolicy-2016-08",

"Certificates": [

{

"CertificateArn": { "Ref" : "SSLCertificateId" }

}

],

"Port" : "443",

"Protocol" : "HTTPS"

}

},

That works! Thanks @daxlreod.


Related discussions:

This is why I use Troposphere, if CFN is lacking anything I can just throw some boto3 code on the end and my problems are solved.

Hmm interesting… will definitely have a look. I do it all via bash and json now.

You can do this with CloudFormation custom resources too (you may know, just incase others don’t!).

I just set up a listener with a default rule to redirect last week. What does your template look like?

Edit: This is a snippet from my template:

ListenerHTTP:
Type: “AWS::ElasticLoadBalancingV2::Listener”
Properties:
DefaultActions:
- RedirectConfig:
Host: ‘#{host}’
Path: ‘/#{path}’
Port: ‘443’
Protocol: HTTPS
StatusCode: HTTP_302
Type: redirect
LoadBalancerArn: !Ref ALB
Port: 80
Protocol: HTTP

I expected this to be the case as Troposphere supports it as well, thanks for the example.

Are you trying to specify a target group for your redirect? If so, that would be your problem. I set this up 8 days ago and it’s been working fine:

WebAppServerRedirectListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- RedirectConfig:
Host: ‘#{host}’
Path: ‘/#{path}’
Port: 443
Protocol: HTTPS
Query: ‘#{query}’
StatusCode: HTTP_301
Type: redirect
LoadBalancerArn: !Ref WebAppLoadBalancer
Port: 80
Protocol: HTTP

hmmmmmm really that, it is working for you? Even AWS support team confirmed it is not working yet.

I was indeed using the TargetGroup. After taking it out, I am doing like this:

"ALBListener" : {

"Type" : "AWS::ElasticLoadBalancingV2::Listener",

"Properties" : {

"DefaultActions" : [{

"Type" : "redirect",

"RedirectConfig" : {

"Host" : "#{host}",

"Path" : "/#{path}",

"Port" : "443",

"Protocol" : "HTTPS",

"Query" : "#{query}",

"StatusCode" : "HTTP_302"

}

}],

"LoadBalancerArn" : { "Ref" : "ELBRESOURCE" },

"SslPolicy": "ELBSecurityPolicy-2016-08",

"Certificates": [

{

"CertificateArn": { "Ref" : "SSLCertificateId" }

}

],

"Port" : "443",

"Protocol" : "HTTPS"

}

},,

Then I get the error: The redirect configuration is not valid because it creates a loop

Check my reply https://www.reddit.com/r/aws/comments/a3lqx5/redirect_http_to_https_with_alb_via/eb8ksh4

i see… it is actually working… My mistake was to use:

"Port" : "80",

"Protocol" : "HTTP"

Instead, it has to be port 80 and protocol HTTP (obviously).

Unfortunately, AWS support was not able to tell me that.

But then, traffic is not sent to my TargetGroup. So even though the redirect is working, my application servers do not receive any traffic… I would need the “forward” here as well…

… any tips?

You need two listeners, one for each protocol

oh shit.. that makes sense, actually.

Can you show me the code you used to get this going in Troposphere? I am quite new and can not get it! I feel like I am so close!!! I have searched high and low and this thread is the closest thing out there to getting it working. Thanks!!!