Okay, so the last thing that we need
to do is create the auto-scaling group
for our Tomcat instance.
Right, app01.
We wanted to automatically scale out
and scale in based on the load.
We need total three things
to set up the complete auto-scaling group.
One is the AMI of this instance.
So, we need to create the AMI of this instance.
Second is the launch template.
The information about how to launch the instance.
That is launch template.
Third is the auto-scaling group itself.
Based on the launch template or information provided
from the launch template, it is going to launch the instance
from the AMI, which is specified in the launch template.
So once again, three thing.
AMI, launch template, auto-scaling group.
First thing we need to do is we need to create the AMI
of this instance.
So, let's say Action, Instance state...
Oh, sorry.
Image and Create image.
I'll give it a name.
vprofile-app-image,
and you can give some description,
and I will say Create image.
This is going to take some time.
The AMI is getting created, it is in the Pending status,
so this is going to take some time.
So, wait until the status of the AMI becomes available.
Once it is available to use,
we will create a launch template
and then we will move to the auto-scaling group.
Okay, so after waiting for five minutes or so,
the AMI is available now.
Now we will create launch template
which will be used in the auto-scaling group.
So, let's go to Launch Templates.
Okay, so here, we are at Launch Templates.
Now auto-scaling group is going to create
or delete instances, or maintain the number
of instances based on the information that we provide to it,
which we will do next.
But if it's launching the instance,
how will it know which AMI to use,
which security group to use, which key pair to use?
All that information is provided by the launch template.
So in the auto-scaling group, we specify,
this is the launch template you need to use
when you want to launch an instance.
So, let's click on Create launch template.
Let's give it a name.
vprofile-app-LC.
Description. I'm going to give the same thing.
Okay, Application and OS Images.
AMI, basically, so we created already the AMI,
so we're going to say My AMI,
and you should be able to see your AMI over here.
If you have multiple AMIs,
make sure you select the right one that we just created
and come down.
It is almost similar to launching an instance
because it's the same thing.
Launch template will be used to launch the instance.
So instance type, we will keep it as a t2.micro, free tier.
Key pair.
The same key pair that we used to launch the instance,
vprofile-prod-key.
Security group. Select an existing security group.
vprofile app security group.
Coming down.
Resource tags.
Let's give tag, Name.
Oh, I need to do proper case.
Name.
Value. I'm going to put vprofile-app.
So here, we don't give app01, 02,
because auto-scaling group is going to maintain the number
of instance.
So here, the number of the instance does not matter, really.
So give this tag to the Instances,
and also, I want to give it to the Volumes.
So, the volumes of this instance
will also should have this tag,
also, let's give it to the Network interfaces.
So it's easy to identify when you have tags.
We can give one more tag over here.
I will say Project.
vprofile.
Same for the volumes, network interface.
Okay, come in the Advanced details. Let's come down.
Now we have created an IAM role
that we attach to our app01 instance.
When this instance gets launched,
we want these instances also to have that IAM role.
So I'll say instance IAM profile
and find your role, s3 role.
Select that,
and that is all we need.
And we can just say Create launch template.
Okay, so the launch template is created,
and we're going to specify this launch template
in the auto-scaling group.
Now let's go to the auto-scaling group.
Click on Create Auto Scaling group.
Let's give auto scaling group a name.
vprofile-app-ASG,
and let's select the launch template.
Scroll down and go to next.
Okay, so let's keep the VPC, the default VPC,
and let's select all the zones that we have in this VPC.
I'm going to select on all the zones,
so that means you can launch the instance in any of the zone
or you can select specific zones also.
And then going next,
so load balancer, we have a load balancer.
We want our instance to be in the load balancer
after they are launched.
So we can select here,
"Attach to an existing load balancer."
So we basically select here the target group,
not the load balancer.
So once the instance is launched by the auto-scaling group,
it is going to add it under this target group.
So, let's select our target group.
Coming down to the health check,
so auto-scaling group, we'll do a basic health check,
which is the instance health check two by two,
whether the instance is running or not, that's all.
It is not going to check
whether the service in the instance is running or not.
So for us, there is the Tomcat service.
If you want to check the status
if the Tomcat service is also running or not,
there is no direct method over here,
but we have this method
which is looking at the load balancer health check.
Now our load balancer will do the health check
of the instance on port 8080 at /loginurl.
So basically, it's checking the Tomcat service is running
or not.
So based on that health check,
auto-scaling group can decide
whether this instance is healthy or unhealthy.
So, that's what this check mark means.
"Turn on Elastic Load Balancing health checks."
And it's also recommended.
Let's come down. Let's go to Next.
Now here, we need to decide the capacity.
How many instance you want for the sake of cost.
Keep one as the desired instance
and minimum instance as one.
Maximum capacity, you can increase
just for the sake of learning.
Anyways, the auto-scaling group is not going
to launch more instances
because our instance will not have any load.
So, this is to maintain the capacity.
If we want to scale out or scale in based on the policies,
you need to go for Scaling policies.
So by default, it's none.
We are going to say tracking scaling policy.
So, set the scaling policy.
And you have different metrics over here,
CPU utilization is the most common one,
and network out for web application.
These are two very popular metrics for the web application,
http, https-based.
Any application you have,
CPU utilization and network out are the most used one.
We'll stick to CPU utilization
that if CPU utilization goes more than 50 overall.
So in one single instance,
if the one single instance CPU utilization goes more than 50
then it is going to decide to launch extra instance,
and maximum, it can launch four instance.
That's what we have specified.
Okay, that simple it is.
Sure, there are many complicated policies also
that you can set
with customized policy, but we are not getting into that.
There is a scale-in protection option
that if you want the instance to not get terminated
if it's unhealthy, then you can put this check mark on.
We don't want this.
This is usually used, you know,
auto-scaling group will terminate the instance
if it's unhealthy and launch a replacement for it.
But you might need to troubleshoot
and log into the instance,
and see what was the actual problem why it went unhealthy.
In such cases, you need to enable scale-in protection.
So, auto-scaling group will not terminate
the unhealthy instance,
but it won't divert any traffic to it.
It will launch a replacement,
but it won't terminate the unhealthy instance.
That's what this check means. Let's not put this check mark.
And let's go Next.
Notification.
Now if you follow the initial AWS setup lectures,
you must have created a billing alarm.
So, you can use that SNS Topic over here.
If you don't see anything in this dropdown,
that means you did not create the billing alarm
and you don't have any topic to send email to.
So, auto-scaling group can send these four events.
Launch, Terminate, Fail to launch, Fail to terminate
to the topic that you have provided.
I'm going to keep that,
but if you don't have this, it's okay.
You can remove this and go Next.
Tag. Let's add some tag.
Name.
Let's say vprofile-app.
And I will say Project,
name as vprofile,
and we'll go Next.
So, carefully review all the settings
that you have selected.
If you are good with all the settings that you selected,
you can say Create Auto Scaling group
and wait for almost 10 minutes or so
for it to launch the instance, add in the target groups.
It's going to take some time.
So, wait until all those things are completed
and then we'll take a look what actually happened.
-: Okay, so I was waiting for like more than 10 minutes,
and in this time, auto-scaling group has launched instance.
It has launched two instances, the desired capacity changed
to two and then it deleted the instance.
So, it was figuring out how to maintain the capacity,
and I was just waiting and watching
until it comes to one single instance.
So you also need to do the same thing, wait for some time,
and in this time, it is going to launch instance,
terminate instance, and it will take 10 to 15 minutes
to become completely stable.
Now in the Instances section, if you go,
and let's search with app.
Okay, vprofile-hyphen-app.
So we have vprofile-app01,
this instance we created earlier to create the AMI.
We don't need this one, so you should terminate this one.
So you can say Instance state, Terminate instance.
Terminate.
And this is also going to take some time.
The instance will get terminated.
It's going to remove from the target group,
so this process takes some time.
So we have vprofile-app, this is in Running state.
We have one more vprofile-app,
but you see it's in the Terminated state.
Now if you go to the Target Groups.
In the Targets, you can see instances.
Now app01, this one will become unhealthy,
and we can remove it later from the target group.
And this is the instance that was created
by the auto-scaling group.
Now if you have multiple instances in your target group,
they are healthy,
you don't want to terminate the other instances.
So you need to do one thing
before you check the vprofile application from browser,
before that you need to enable one setting.
This is specifically for vprofile application.
So, click on your target group, vprofile-app-tg,
that's the name of my target group,
and come to the Attributes section.
Okay, once again. Target Group, Attributes, click on Edit.
Scroll down, and you have to enable this setting Stickiness.
Now stickiness means if the user is forwarded
to an instance, let's say we name that instance as app01,
and we have one more instance, app02.
Now the user is routed to app01.
That user will always get routed to app01
for a period of, we can specify the time here, one day.
Right, if there is no stickiness,
when the user refresh or try to log in,
the load balancer might route the request
to app02 then app01, so it may...
Not it may. It will do that if there is no stickiness.
By default, the algorithm is round-robin.
So first request goes to first instance,
the second goes to the second,
and third goes to the first instance again,
the fourth goes to the second instance.
So, it's round-robin by default.
But that will be problem for vprofile application-specific.
Okay? This is only for vprofile application.
I'm not saying you need to do this for every application
that you see in real time.
Okay, so enable Stickiness and Save changes.
You can read more about stickiness online.
Now there is a complete process
how it stores a cookie in the user's browser.
And based on that cookie, it is routed to the instance,
so on and so forth.
But that will be out of scope of this course,
so I'm sticking to only this much.
Okay, so come back to target groups while we are doing this,
if you go to the target group targets.
Okay, we just have only one single instance now.
The old instance is gone.