AWS Elastic Beanstalk makes redirect to HTTPS

by moondaddi on 2018-12-18


Elastic Load balancer and Elastic Beanstalk

AWS Elastic Load balancer automatically distributes incoming application traffic across multiple AWS EC2. It enables your application's falut tolerance level higher and seamlessly providing the required amount of load balancing capacity needed to distribute application traffics.

When you deploy your application whatever it's built with any language to Elastic Beanstalk, Elastic Beanstalk automatically performs all required process in deployment for your application. One of requirements is Elastic Load balancer in front of EC2 which is generated by Elastic Beanstalk.

Elastic Load balancer is listening the port 80 as default. And you can set 443 port for HTTPS.

Redirect to HTTPS

Once everything sets up, You can access your application in Elastic Beanstalk through HTTP and also HTTPS.

If you want all requests to port 80 to redirect to port 443, HTTPS, you can make ssl_redirect.config file in .ebextensions in your application root directory.

files:
    "/etc/httpd/conf.d/ssl_redirect.conf":
        mode: "000644"
        owner: root
        group: root
        content: |
            RewriteEngine On
            <If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
            RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
            </If>

How it works

During deployment of Elastic Beanstalk, it will check .ebextendsion directory if any *.config files there. If config file is found, Elastic Beanstalk will proceed it before applying the deployment of your application.

ssl_redirect.config file will be generated in path /etc/httpd/conf.d/ and this will works for apache server to check any none https request and redirect it to https.