Skip to content

Instantly share code, notes, and snippets.

@ns-mkusper
Last active January 28, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ns-mkusper/2117f91153ef56994d009af8457e2b49 to your computer and use it in GitHub Desktop.
Save ns-mkusper/2117f91153ef56994d009af8457e2b49 to your computer and use it in GitHub Desktop.
Add HTTP->HTTPS redirect for ALB's
for alb in $(aws elbv2 describe-load-balancers --query 'LoadBalancers[].LoadBalancerArn[]');do
DEFAULT_PORT=$(aws elbv2 describe-listeners --load-balancer-arn $alb --query 'Listeners[].{Port:Port}')
if [ "$DEFAULT_PORT" == "443" ];then
echo "ALB $alb is listening on port $DEFAULT_PORT"
echo "Adding listener on $alb to redirect $NEW_PORT to $DEFAULT_PORT"
aws elbv2 create-listener --load-balancer-arn $alb --protocol HTTP --port 80 --default-actions '[
{
"RedirectConfig": {
"Protocol": "HTTPS",
"Host": "#{host}",
"Query": "#{query}",
"Path": "/#{path}",
"Port": "443",
"StatusCode": "HTTP_301"
},
"Type": "redirect",
"Order": 1
}
]'
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment