I’ve been feeling like I should document some of the stuff that I’ve learned, and while it would be really cool to start with something awesome, I’m going to go the more basic route first. So here’s the steps on setting up 5 instances behind a load balancer running httpd with static content.
- Create the load balancer (this seems to take a bit so I usually do it first)
- Create instances. There are a few steps here, I’ll only focus on one or two
- Attach the instances to the load balancer’s target group
- Go to the load balancers DNS
Create the Load Balancer
Log in to the AWS Console and select EC2. From there, on the lower left you’ll see Load Balancer. Once that’s selected, on the top bar you’ll click “Create Load Balancer”. Since this one is for http traffic only, I’m going to select the Application Load Balancer.
For the name, I specified 5-Instance-Balancer, it’s internet facing on a regular ipv4. The balancer is checking port 80 (default), and the VPC will be the default VPC that Amazon created for me. It’s on 172.31.0.0/16 and I selected us-east-1[a-f].
Since its running http, not https, it does tell you that it’s not secure. For this set up its fine.
In the Security Group configuration, I’ve previously set one up, but you could create a new one to specify what port(s) you want open – just port 80 in this example.
In step 4 you configure routing. This is pretty easy, you give it a name – in this case I’ll call in MyTargetGroup – and we’ll be monitoring instances on port 80 (these are the default settings). The protocol is left to HTTP and the path I modified it to be /index.html, which will be the only file on the web servers that we are testing with. There are some advanced health settings to change the threshold on whether a host is healthy or not, but in this example those were left to their defaults. After this step, just click through till it’s complete.
Create the Instances
Navigate to the EC2 section of the AWS console, and click Launch Instances. I selected the regular AWS AMI, t2.micro type, left everything up to this point default with two exceptions: We are adding 5 instances and under Advanced Details, there is a section to pass commands to the image as it boots up. This can be accessed after the instance boots up by going to http://169.254.169.254/latest/user-data. Here’s what I added.
yum install httpd -y
cat > /var/www/html/index.html << EOF
<H1>HI! You have reached the following host: `hostname`</H1>
Here is some random data `hostname | sha256sum`
chkconfig httpd on
What this does is install the httpd daemon, put some things into /var/www/html/index.html so it can be differentiated on the ELB, start uphttpd and enable it to start up in the event that the instance is rebooted.
After that’s done, all the other settings are default, except make sure that you add the security group to have port 80 open & launch them.
Go back to the main EC2 menu, and click on Target Groups. Select ‘MyTargetGroup’ and then under Action -> Register and deregister targets. The instances should be running, and you should see 5 of them, so select all of them, click ‘Add to registered’ and then save. It might take a few minutes for them to all show up as healthy, so give it some time.
Here’s a few screenshots of the instances html output. You’ll notice that it’s referencing the ELB and from there it’s directing traffic to the healthy instances.
That’s it! Later I might add one with an autoscaling group. Oh one thing I noticed that happened was the AWS put all the instances in the same AZ, which would be an issue in a production environment since you want to spread the instances across multiple for resiliency.