Deep Dive with EC2@AWS (part 2)

        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.

ec2-part2-01

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.

ec2-part2-02

Select Amazon Linux AMI

ec2-part2-03

Select General Purpose — t2.micro which is the only type available for free tier.

ec2-part2-04

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.

ec2-part2-05

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.

#!/bin/bash
sudo su
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”.

ec2-part2-06

Leave the default setting for storage and Click on “Next: Add Tags”.

ec2-part2-07

Add the relevant tags if needed and click on Next.

ec2-part2-08

Add the security group for HTTP, select HTTP from drop down and select “Review and Launch”.

ec2-part2-09

Review the settings and once done click on “Launch”.

ec2-part2-10

Select the key-pair or create new one and click on “Launch Instances”.

ec2-part2-11

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 :)

ec2-part2-12

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.

ec2-part2-13

Select the instance, go to Action > Image > Create Image.

ec2-part2-14

Enter Image Name and Image Description. Leave the Instance Volumes as default. Click on “Create Image”

ec2-part2-15

ec2-part2-16

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.

ec2-part2-17

Click on “Create LoadBalancer”.

ec2-part2-18

Select “Classic Load Balancer” as Load Balancer Type.

ec2-part2-19

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.

ec2-part2-20

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.

ec2-part2-21

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

ec2-part2-22

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.

ec2-part2-23

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.

ec2-part2-24

Add the relevant tags and click on “Review and Create”.

ec2-part2-25

Review the details and once done click on “Create”.

ec2-part2-26

ec2-part2-27

Go to LoadBalancer Dashboard and wait for few minutes until our LoadBalancer is ready.

ec2-part2-28

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”.

ec2-part2-29

Click on “Create Launch Configuration”.

ec2-part2-30

Choose “My AMI’s” from side menu and select the AMI that we created earlier.

ec2-part2-31

Select “General Purpose — t2.micro” and click on “Next: Configure Details”.

ec2-part2-32

Enter name for Launch Configuration and click on “Next: Add Storage”.

ec2-part2-33

Use the default EBS volumes for launch configuration. Click on Next.

ec2-part2-34

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”

ec2-part2-35

Review the details for Launch Configuration and click on “Create Launch Configuration”.

ec2-part2-36

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.

ec2-part2-37

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.

ec2-part2-38

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.

ec2-part2-39

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.

ec2-part2-40

You can set notification using SNS, but this is not mandatory, so we will just skip it for now.

ec2-part2-41

Add the relevant tags for Auto-Scaling Group and click on review.

ec2-part2-42

Review all the details configured for Auto-Scaling group, once done click on “Create Auto Scaling group”

ec2-part2-43

ec2-part2-44

Go to dashboard of Auto-Scaling group. Wait for few minutes until auto scaling group is provisioned.

ec2-part2-45

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.

ec2-part2-46

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 😀

ec2-part2-47

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.

ec2-part2-48

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.

ec2-part2-49

Select any one instance. Go to Actions > Instance State > Stop.

ec2-part2-50.PNG

Click on “Yes, Stop” and wait for some time.

ec2-part2-51

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.

ec2-part2-52

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.

ec2-part2-53

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 :)

One thought on “Deep Dive with EC2@AWS (part 2)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s