Load balancing is a technique commonly used by high-traffic Web sites and Web applications to share traffic across multiple hosts, thereby ensuring quick response times and rapid adaptation to traffic peaks and troughs. The Elastic Load Balancing service from Amazon Web Services (AWS) with Secure Sockets Layer (SSL) support makes it easy to add secure load balancing for applications running on AWS.
In a horizontally scaled web server environment, no individual server should care about SSL. Your customers and the rest of the world should interact with your system through HTTPS, but your web servers only need to respond to HTTP, to allow for per-server health check monitoring.
Your load balancer is the bridge between your pool of resources and the outside world, so your load balancer should handle SSL. Luckily, AWS makes this really easy.
This guide walks you through the process of configuring and testing an Elastic Load Balancer with an SSL certificate for a application running on AWS.
Step 1: Identify your application instance in the AWS EC2 Console
The first step is to identify your application instance and collect various important bits of information, such as the instance ID, DNS name, public IP address, availability zone and Virtual Private Cloud (VPC) ID. To do this:
- Log in to the AWS EC2 console.
- From the “Services” menu, select the “EC2” service.
- From the EC2 Dashboard, select the “Instances -> Instances” menu item.
- Search for and select your application instance from the list of available instances.
- From the instance details pane, note the instance ID, DNS name, public IP address, availability zone and VPC ID. You will need these details in subsequent steps.
Next, create an Elastic Load Balancer as follows:
- From the EC2 Dashboard, select the “Load Balancing -> Load Balancers” menu item.
- Click the “Create Load Balancer” button.
- On the “Select Load Balancer type” page, select the “Application Load Balancer” option and click “Create”.
On the subsequent “Configure Load Balancer” page:
Enter a name for the load balancer and specify the scheme as “Internet facing”.- In the “Listeners” section, ensure that there is an HTTP listener on load balancer port 80. Click the “Add listener” button and add a second HTTPS listener on load balancer port 443. This configures the load balancer to handle both HTTP and HTTPS requests from clients.
- In the “Availability Zones” section, select the same VPC as the one used by your application instance and select a subnet from each availability zone.
- Click the “Next: Configure Security Settings” button to proceed.
Within the AWS Certificate Manager, on the “Request a certificate” page, enter your domain name. Click “Review and request” to review and confirm the request.
An email will now be sent to the registered owner of the domain with instructions to confirm the certificate request by validating the domain. The domain will appear in the AWS Certificate Manager with status set to “Pending validation”.
Once the domain has been validated, the certificate will be issued and will appear in the AWS Certificate Manager with status set to “Issued”.
Back on the “Configure Security Settings” page for the load balancer, the newly-issued certificate should now appear in the list of available certificates. Select it and click the “Next: Configure Security Group” button to proceed.
- On the “Configure Security Group” page:
Select the option to “Create a new security group”.- Add a security rule to allow inbound traffic on port 80 (the HTTP port) with source “Anywhere”.
- Add a second security rule to allow inbound traffic on port 443 (the HTTPS port) with source “Anywhere”.
- Click the “Next: Configure Routing” button to proceed.
On the “Configure Routing” page:
- In the “Target group” section, create a new target group and assign it a name. Ensure that the protocol is set to “HTTP”, the port to “80” and the target type to “instance”. With this configuration, traffic between the load balancer and the instance will be transmitted using HTTP, even for HTTPS requests made by the client to the load balancer.
- In the “Health checks” section, define the protocol as “HTTP” and the path to “/”.
- Click the “Next: Register Targets” button to proceed.
On the “Review” page, review the details of the configured load balancer. Click “Create” to confirm the configuration and deploy the load balancer.
Step 3: Point your domain name to the load balancer’s IP address
Once the deployment is complete, perform the following steps:
Visit the EC2 Dashboard and note the DNS name for the configured load balancer.- Using the DNS name, obtain the corresponding public IP address, using a tool like ping or nslookup or a service like DNS Lookup.
- Update your domain’s DNS settings by adding an A record that points to the public IP address of the load balancer. To do this, you will usually need to log in to your domain name provider’s management console and make the necessary changes.
NOTE: Once you make the necessary changes, it can take up to 48 hours for the change to propagate across other DNS servers. |
Browsing to https://DOMAIN should result in the load balancer displaying the secure welcome page of the application. Clicking the padlock icon in the browser address bar should display the details of the domain and SSL certificate.
How should I configure my ELB health check when using NameVirtualHosts and redirecting to www?
Question AWS Forum – You could alternatively specify the path part of the URL that you want the ELB to request and ignore that path by adding another RewriteCond:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{REQUEST_URI} !^/health-check$ RewriteRule ^ http://www.example.com/$1 [R=301,L]
Normal users who hit that URL will not be redirected. You could also use the same technique to detect the User-Agent of the ELB.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker RewriteRule ^ http://www.example.com/$1 [R=301,L]
Normal users who spoof their User-Agent will not be redirected. Or the internal IP address of the ELB.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{REMOTE_ADDR} !^10\. RewriteRule ^ http://www.example.com/$1 [R=301,L]
For this option to work, you will require either mod_rpaf (for Apache 2.2) or mod_remoteip (for Apache 2.4) to modify the REMOTE_ADDR variable to contain the correct part of the contents of the X-Forwarded-For header. As long as you set that up correctly, it shouldn’t be possible for a normal user to avoid the redirect response.