Hello all, how are you doing today? I’m back as promised, with second installment for Deep Dive with EC2@AWS. I would like apologize to not posting anything new in past couple of weeks. So let’s begin, few weeks back, I posted a blog which describes services & features provided by EC2, we talked about Elastic IP & significance of it. Today we will look at some more features and capabilities of EC2 namely Load Balance, Auto Scaling and some others. If this is first time on the site, then I strongly recommend to first check out my earlier blog – What is AWS, Getting Started with AWS, Deep Dive with EC2@AWS (part 1) respectively, this will help you to better understand the contents in this blog post. This is going to be very long post, so take a cup of coffee and sit tight as you go through it 🙂
Login into your AWS console, Go to EC2 console. Today we are going to host our very own web application, from our EC2-instances without even logging into EC2-instance. Yes, take a moment to think about it :) You are probably wondering how we can host a website from an instance, without even logging into it right ? Well it’s quite simple actually, AWS has developed a feature to do so, and we are going to leverage that feature, it’s called bootstrapping. So are going to pass a bootstrapping script while deploying an instance. AWS will execute the script after the instance is provisioned and instance is powered on. Let’s start, go to EC2 console.
I know, I have already covered this part in earlier post. But we are going to pass our very own script which will be executed as soon as our instance is provisioned. Click on Launch Instance.
Select Amazon Linux AMI
Select General Purpose — t2.micro which is the only type available for free tier.
This is the most important step. Use the default values for Number of instances, Network, Subnet and Auto-Assign public IP. Now we can have additional security on our instances by checking the option “Protection against accidental termination”, by this we cannot directly terminate our instances, to do, so we have to go to settings and disable the Termination Protection option. This is another layer of security which will prevent accidental termination of our EC2 instances. You can go ahead and choose or not, for purpose of this demonstration I’m not gonna check it because I’ll be destroying this instance soon.
Okay we are still on Step 3, if you have already clicked on Next, don’t worry click on Previous to come back to Step 3: Configure instances. Now here Expand “Advanced Details” option. Under User Data select “As text” radio button. Enter following script in text field.
yum update all -y
yum install httpd -y
service httpd start
chkconfig httpd on
echo “<html><body><h1>This is a test page created for DeepDive@EC2 part-2</h1></body></html>” > /var/www/html/index.html
Copy-Paste the script in text box and click on “Next: Add Storage”.
Leave the default setting for storage and Click on “Next: Add Tags”.
Add the relevant tags if needed and click on Next.
Add the security group for HTTP, select HTTP from drop down and select “Review and Launch”.
Review the settings and once done click on “Launch”.
Select the key-pair or create new one and click on “Launch Instances”.
Go to EC2 dashboard, wait until Instance State is running. Once the instance is up and running, copy the Public DNS (IPv4) or IPv4 Public IP and paste it in a web browser with fingers crossed of course :)
Well, we are now hosting our very own website on an EC2 instance we deployed in AWS without even logging in the instance. Congratulations 😀
Up until now we were using Amazon AMI, now let’s create our very own AMI. Go to EC2 Dashboard and select the EC2 instance, we just deployed.
Select the instance, go to Action > Image > Create Image.
Enter Image Name and Image Description. Leave the Instance Volumes as default. Click on “Create Image”
Go AMI’s dashboard under Images. Wait until your AMI’s status is available. At this point you can go ahead and terminate the EC2 instance, since we are not going to need it again and it will save you some hours in free tier. I have terminated the instance in my account. Once the status is available. Go to LoadBalancers under Load Balancing.
Click on “Create LoadBalancer”.
Select “Classic Load Balancer” as Load Balancer Type.
Enter Load Balancer name, keep everything default. Since we are hosting HTTP application, so we will keep the listener port as default i.e. 80 on port Load Balancer and as well as on Instance.
Select the security group we created earlier or create a new one, whichever you choose to make sure, you expose port 80, so that HTTP traffic is allow thorough our Load Balancer.
In this step, AWS is just giving us warning that the LoadBalancer is not secured. As this is just a demo LoadBalancer, we can ignore the warning and proceed. Click on Next
Select HTTP for Ping Protocol, Ping Port 80 and Ping Path — /index.html. Under Advanced Details. Response Timeout denotes the amount to time LoadBalancer will wait until it gets a response from the instance. Interval is the time LoadBalancer will wait before pinging the instance again. Unhealthy threshold is the number of consecutive ping failed which will classify the instance as unhealthy. Healthy Threshold on the other hand is the number of consecutive ping successfully which will classify the instance as healthy. Configure these option and Click on next.
At this point you might see the instance we created earlier do not select it. I’ve already terminated the instance. If you see the instance that we created earlier, don’t add it to LoadBalancer yet. Check on Enable Cross-Zone Load Balancing & Click on Next.
Add the relevant tags and click on “Review and Create”.
Review the details and once done click on “Create”.
Go to LoadBalancer Dashboard and wait for few minutes until our LoadBalancer is ready.
We have created our very own LoadBalancer, now will create our Auto-Scaling group. Find “Launch Configuration” under “Auto Scaling”. Click on “Create Auto Scaling Group”.
Click on “Create Launch Configuration”.
Choose “My AMI’s” from side menu and select the AMI that we created earlier.
Select “General Purpose — t2.micro” and click on “Next: Configure Details”.
Enter name for Launch Configuration and click on “Next: Add Storage”.
Use the default EBS volumes for launch configuration. Click on Next.
Use the same security group that we created earlier or create new one but make sure to expose port 80 to allow HTTP traffic. Click on “Review”
Review the details for Launch Configuration and click on “Create Launch Configuration”.
Select or create a new key pair and click on “Create Launch Configuration”. This will create Launch Configuration and redirect us directly to create our Auto-Scaling Group.
Enter the name for “AutoScaling Group”. Choose the initial group size. Add at least 2 subnets, so that we can deploy instances in different subnet and distribute traffic in different subnet.
Check the option for “Receive traffic from one or more load balancers”. Select the classic LoadBalancer we created earlier. Keep rest of the options default and click on Next.
In this window, we can configure the scaling policy, select the minimum and maximum number of instances for the group. Here I’ve selected Average CPU Utilization and set the value to 60, this means that Auto-Scaling group will maintain 60% utilization of CPU and scale up and down as per traffic. You can choose a different type of Metric available, once done click on next.
You can set notification using SNS, but this is not mandatory, so we will just skip it for now.
Add the relevant tags for Auto-Scaling Group and click on review.
Review all the details configured for Auto-Scaling group, once done click on “Create Auto Scaling group”
Go to dashboard of Auto-Scaling group. Wait for few minutes until auto scaling group is provisioned.
Go to LoadBalancer Dashboard. Select the LoadBalancer, go to Instances tab and monitor status of the instances, wait until all the instances are InService status.
One All the instances have InService Status, go back to Description tab, copy the DNS name for the LoadBalancer and paste it in browser, if everything went well you should see the web page we saw earlier. Finger crossed 😀
And here we go, Congratulations we are now hosting a live website with capability to scale up and scale down. Well done guys, you have been great. Now Let go back to our AWS Console to conduct one final experiment.
Go to EC2 dashboard. You can see the instances that has been deployed via Auto Scaling group. Now let’s see what happens if we shut down one of the instances manually.
Select any one instance. Go to Actions > Instance State > Stop.
Click on “Yes, Stop” and wait for some time.
Go to LoadBalancer dashboard and check the status of the instances, one of the instance is now OutOfService status. Wait for couple of minutes and go back to EC2 Dashboard.
Yes, the instance we turned off is now terminated and another instance is now being provisioned, yes that is the power of Auto-Scaling Group, it will maintain the minimum number of instances at any given instance and it will destroy or terminate all the failed instances so that way you won’t charge for the instances that went down for any reason. And if you go back to LoadBalancer dashboard.
Now all the instances in LoadBalancer is now with InService status even the one Auto Scaling group just provisioned.
Well that’s it from me today. I know it’s been a really long blog, but we covered a lot of important features of EC2. Thank you for being patient throughout the post, you have been great audience. With that I’ll take my leave and you guys take care and very soon I’ll come back very soon with another new and exciting technology. Good bye :)