-1

I'm using Elastic Beanstalk, and I installed my SSL certificate in the load balancer of an EC2 instance belonging to the EB. Every time the server is not healthy, the load balancer delete the instance and creates a new one, which means I'm gonna lose my redirection codes and SSL certificate that are set up inside the instance level.

So, I got to install my SSL certificate on the load balancer through AWS ACM. However, how can I redirect HTTP to HTTPS on the load balancer? If I still leave the redirection codes in the server, the load balancer is gonna remove the redirection codes when the instance is not healthy.

Is setting up redirection inside the instance the only way possible here?

UPDATE

I found some solution that is done under .ebextensions. I'm currently working on Apache server. What I found is the following:

I made a file httpd_redirect.conf under .ebextensions with the below contents.

files:
  "/etc/httpd/conf.d/httpd_redirect.conf" :
    mode: "000644"
    owner: root
    group: root
    content: |
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Jay
  • 189
  • 2
  • 8
  • You were linked to the right way to customize the webserver config (where this should be handled) [in your last question](https://serverfault.com/questions/920702/aws-elastic-beanstalk-removes-ec2-instance-creates-it-again-when-my-server-is-do): https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html – ceejayoz Jul 13 '18 at 21:10
  • (You can also handle this in your application itself, by writing code that detects HTTP requests and redirects to HTTPS. Again, you should never be manually customizing an individual Elastic Beanstalk instance.) – ceejayoz Jul 13 '18 at 21:11
  • @ceejayoz I checked the document you gave me, but I can't find anything explaining how to forward HTTP to HTTPS through Elastic Beanstalk. – Jay Jul 13 '18 at 21:39
  • The very first line states "they could be configuration files such as a replacement for httpd.conf to override specific settings that are defaulted by Elastic Beanstalk". That's exactly what you need here - a webserver config file customized to redirect HTTP to HTTPS. – ceejayoz Jul 14 '18 at 01:46

1 Answers1

2

I answered your last question as well. First, you need to use ebextension for any type of customization you want to write for your Beanstalk environment. Ebextensions are exactly built for that purpose.

Second, we don't know what solution stack you are using, so we can't provide snippets of ebextensions for you to route http to https. Each solution stack is different, some use nginx, others use apache, and one uses none. Without a descriptive explanation of your environment, no one will be able to help you.

Third, the previous answer I gave of writing ebextensions may be complicated for non-AWS users, but it should be within the realm of what you are able to accomplish. If not, I would suggest writing a bash script that does what you want, and then posting here and I can help you write ebextensions that can execute that script. The problem is, it doesn't seem like you want advice, but rather someone to code the solution for you.

Please provide all necessary information for us to help you, and be willing to learn and implement our suggestions. Otherwise, you won't be able to find much help on server fault.

Brandon
  • 408
  • 2
  • 11
  • Hi, thank you for the help. I'm currently working on Apache web server. After some research, I found a way setting up redirection config file under `.ebextensions`. Like you see the updated post, I made the file `httpd_redirect.config` under `.ebextensions`. After the load balancer creates a new instance, I see the file in the path, but the redirection is not working. Can you please take a look at that? – Jay Jul 16 '18 at 01:32