‫Okay, so let's practice
‫using Auto Scaling groups.
‫To do so, first of all,
‫please make sure to take your instances
‫and terminate them all
‫to have zero instances running in EC2.
‫Okay, so next we have to create
‫an Auto Scaling group.
‫So let's go into Auto Scaling groups
‫on the left hand side,
‫and we're going to create
‫our first Auto Scaling group.
‫So create Auto Scaling group,
‫let's name our Auto Scaling group DemoASG,
‫and we need to refer to a launch template,
‫so I'm going to create a launch template right now.
‫So when you create a launch template
‫you need to give it a name,
‫I will give it MyDemoTemplate,
‫as well as a description,
‫Template.
‫Okay, so we're going to scroll down
‫and specify different options.
‫So for the Amazon Machine Image I'm going to use,
‫I'm going to go into Quick Start,
‫Amazon Linux,
‫I would choose Amazon Linux 2,
‫in the version x86.
‫Okay, and we'll use a free tier eligible AMI,
‫so that's good.
‫Next for Instance type,
‫I'm going to select t2.micro
‫because it is free tier eligible,
‫so anything free tier eligible is good.
‫And I will scroll down in terms of Key pair,
‫I will include EC2 Tutorial,
‫so this is defining pretty much
‫how EC2 instances within my launch template
‫and therefore within my ASG,
‫should be launched.
‫Okay, so the options are very similar
‫to the ones you've seen
‫when you launch an EC2 instance on its own.
‫For subnets you don't include it
‫in the launch templates.
‫For Firewall security group
‫you can select an existing one,
‫for example, your launch-wizard-1.
‫You have 8 gigabytes of gp2 volume,
‫this is good.
‫And then for Advanced details,
‫I'm going to scroll all the way down,
‫and I will find my User data,
‫and for User data I'm going to input
‫the user data we used from before,
‫so all of it,
‫and this is going to allow a web server
‫to be created on every EC2 instance
‫as part of this ASG.
‫So we're good to go,
‫let's create this launch template,
‫it has been created,
‫called the MyDemoTemplate.
‫I'm going to refresh it
‫and select it right here.
‫So we have MyDemoTemplate Version 1.
‫We have the summary of it,
‫and we click on next to keep on going.
‫So we choose the instance launch options,
‫we have a VPC,
‫which is default VPC,
‫as well as the ability
‫to launch into three different subnets,
‫so eu-west-1a, 1b, and 1c.
‫Instance requirements.
‫So we have those provided by the templates
‫which is a t2.micro,
‫but we could, if we wanted to,
‫override them, okay?
‫And when you override them
‫you can specify instant attributes
‫or manually add instance types.
‫And you can have a mix of on demand
‫and spot instances,
‫as well as a mix of different instance types.
‫So this is definitely more advanced,
‫but for now we'll just keep it
‫as the instance type requirements
‫from the Launch template,
‫so we'll have it as t2.micro only.
‫Let's click on Next.
‫Then we can configure advanced options,
‫so Load balancing is optional,
‫so we can create an ASG
‫without a load balancer,
‫and there are a few use cases for that, okay?
‫But we already have an load balancer,
‫so we're going to Attach to an existing load balancer
‫and it will choose my-first-target-group
‫from my DemoALB application balancer.
‫So that the EC2 instances launched
‫as part of this ASG are linked
‫to my application load balancer directly.
‫For Health checks this is optional,
‫but again we have two kinds of health checks,
‫the first is the EC2 health check.
‫So if my EC2 instance fails
‫is going to be removed automatically
‫from my Auto Scaling group,
‫and we can also have an ELB type
‫of health check enabled,
‫and we'll leave it enabled.
‫So that if the ELB deems
‫that an instance is unhealthy,
‫then the ASG will terminate it automatically.
‫Okay, we will not enable any additional settings.
‫Click on Next.
‫So this is where you set your ASG Group size.
‫So Desired capacity is 1
‫Minimum capacity is 1,
‫and Maximum capacity is 1,
‫so that means that we have one instance
‫within the ASG,
‫but if we could specify 10 as max capacity,
‫which would allow us to have a varying size
‫of the capacity of the ASG between 1 and 10.
‫But I'll keep everything as 1 right now.
‫Scaling policies for now I will keep it as None,
‫but we'll have a look at them
‫in details later on.
‫Let's click on Next.
‫No notifications,
‫no tags,
‫we review everything,
‫and then we click on Create Auto Scaling Group.
‫Okay, so my Auto Scaling Group is created,
‫and what is going to do is
‫that it's going to create
‫one EC2 instance for me.
‫So the idea here is that if I click
‫on my Auto Scouting Group,
‫and we get some details,
‫we can see the desired min and max capacity,
‫as well as the Launch template
‫that was used for this ASG.
‫And then if I go into Activity
‫this is where the interesting stuff happens.
‫So when the ASG will decide
‫to create an EC2 instance,
‫it's going to appear in the Activity notifications,
‫so if I refresh this, very soon,
‫sorry, in the Activity history.
‫So if I refresh the Activity history,
‫then I can see that there is
‫a Launching a new instance right here,
‫because the Auto Scaling group
‫has a capacity of 0,
‫and we wanted 1 capacity,
‫so it's just increasing the number
‫of EC2 instances in our ASG,
‫to match the desired capacity.
‫So if I go to the tab Instance Management,
‫as we can see,
‫one EC2 instance was created by the ASG.
‫And so if we go to the Instances tab right here,
‫we can see that the EC2 instance
‫is currently Running and initializing.
‫So it's pretty cool.
‫Now the cool thing is that
‫because we had set up our ASG
‫to be linked to our Target Groups,
‫then if we go to the Target Groups
‫on the left hand side,
‫and find my-first-target-group,
‫and go to Targets,
‫my EC2 instance is being,
‫and I will just expand it,
‫my EC2 instance is being registered
‫into my ALB.
‫So right now it is shows that
‫as unhealthy because the object are failing,
‫because the EC2 instance is still bootstrapping.
‫But if after a bit it passes,
‫it will be shown as healthy,
‫and then will be registered into my ALB.
‫So let me pause the video to wait for that.
‫Okay, so my instance is now healthy,
‫and then if I go to my ALB and refresh,
‫I get the Hello World response,
‫that means that everything is functioning,
‫the instance was created by an ESG.
‫We can see it in the EC2 console,
‫it was registered into the target group
‫which is linked to our load balancer,
‫and therefore our load balancer is fully working.
‫Now, if the instance never gets healthy
‫it gets unhealthy,
‫then because we set up our ASG
‫to terminate unhealthy instances,
‫what you're going to see is
‫that the instance is going to be terminated,
‫and a new one will be created.
‫So you will see this mostly
‫in the Activity history.
‫If you do have this,
‫the reason why this is happening
‫is that your EC2 instance is misconfigured.
‫This could be either a security group issue
‫or it could be an EC2 user data script issue.
‫So please check those
‫before asking questions in the Q&A.
‫Okay, but these are the errors
‫that you should be having.
‫So now we have one EC2 instance
‫and it's in service.
‫And what we could do
‫is that we can experience the scaling
‫by editing the Auto Scaling group size.
‫So we want two instances,
‫and so we have to increase
‫the maximum capacity as well.
‫So we'll update this.
‫And now what we told the ASG
‫is that we want you to create one
‫or more EC2 instance for us.
‫So if you go to the Activity history
‫and then refresh it,
‫very soon is going to show a new activity
‫that it will be trying to be launching
‫an EC2 instance.
‫So here we go,
‫now it says Launching a new EC2 instance,
‫because you have changed the desire capacity
‫from 1 to 2,
‫and the actual number of instances
‫in our a ASG was only one.
‫So now, as you can see,
‫we have a second EC2 instance being created,
‫and after a while is going to be registered
‫into our target group and added.
‫So I will pause the video
‫for this process to happen.
‫Okay, so my instance is now healthy.
‫And so if I go to my ALB and refresh,
‫as we can see I got my two IPs
‫that are looping through my ALB.
‫So everything is working great.
‫And so one last thing,
‫so if we go to activities,
‫we can see the two instances
‫were launched successfully.
‫Now let's go through the opposite,
‫we're going to scale down our capacity
‫by putting the Desired capacity back to 1,
‫and update it,
‫and what this will do
‫is that it will say,
‫"hey, it looks like you have two instances,
‫but now you need only one,
‫so I'm going to pick one
‫of these two instances right here
‫and terminate them."
‫So as you can see here,
‫it says, "I'm terminating one of these instances
‫because of the change of the configuration
‫of the ASG,"
‫and is going to terminate the instance
‫and then deregister it from the target group.
‫And then we'll be back
‫with only one EC2 instance in our ASG.
‫So that's it for the lecture,
‫you've seen the whole power of ASG,
‫and I will see you in the next lecture
‫to talk about Automatic scaling.