1 00:00:00,090 --> 00:00:01,380 ‫So before we go ahead 2 00:00:01,380 --> 00:00:03,450 ‫and practice creating an auto scaling group, 3 00:00:03,450 --> 00:00:05,790 ‫you need to take your first two instances 4 00:00:05,790 --> 00:00:07,940 ‫and we're actually going to terminate them. 5 00:00:08,820 --> 00:00:10,110 ‫Okay, so now this is done, 6 00:00:10,110 --> 00:00:12,900 ‫we can go ahead and create an auto scaling group. 7 00:00:12,900 --> 00:00:16,020 ‫For this, on the bottom left, click on Auto Scaling Group 8 00:00:16,020 --> 00:00:18,450 ‫and we will create an auto scaling group. 9 00:00:18,450 --> 00:00:21,090 ‫So I'll call this one DemoASG, 10 00:00:21,090 --> 00:00:23,730 ‫and we need to create a launch template. 11 00:00:23,730 --> 00:00:24,660 ‫So currently we have none. 12 00:00:24,660 --> 00:00:27,033 ‫So let's create a launch template, 13 00:00:29,340 --> 00:00:32,700 ‫and I will call this one DemoLaunchTemplate. 14 00:00:32,700 --> 00:00:35,490 ‫And this template is being used to tell 15 00:00:35,490 --> 00:00:39,300 ‫to the ASG how to create EC2 instances within it. 16 00:00:39,300 --> 00:00:41,250 ‫So this will look very, very similar 17 00:00:41,250 --> 00:00:44,010 ‫to what we have when we create EC2 instances. 18 00:00:44,010 --> 00:00:47,010 ‫As you can see here, I can choose, for example, 19 00:00:47,010 --> 00:00:50,940 ‫a quick start Amazon Linux for getting Amazon Linux 2 20 00:00:50,940 --> 00:00:53,640 ‫as the base of my EC2 instance. 21 00:00:53,640 --> 00:00:56,160 ‫Then we have an instant type that we can include, 22 00:00:56,160 --> 00:00:58,620 ‫for example, t2.micro. 23 00:00:58,620 --> 00:01:00,900 ‫For key pair, we will not include it in the launch templates 24 00:01:00,900 --> 00:01:03,480 ‫or we can just say that, no, we don't need one, 25 00:01:03,480 --> 00:01:04,950 ‫so this is good enough. 26 00:01:04,950 --> 00:01:06,570 ‫For subnets, so we'll not include this 27 00:01:06,570 --> 00:01:07,740 ‫in the launch templates. 28 00:01:07,740 --> 00:01:09,840 ‫For security group, we can select 29 00:01:09,840 --> 00:01:12,030 ‫a security group that's already existing. 30 00:01:12,030 --> 00:01:13,863 ‫For example, my launch-wizard-1, 31 00:01:16,620 --> 00:01:18,390 ‫under advanced network configuration 32 00:01:18,390 --> 00:01:19,650 ‫we don't need to do anything. 33 00:01:19,650 --> 00:01:22,740 ‫For EBS volumes for storage we don't need to do anything. 34 00:01:22,740 --> 00:01:24,720 ‫And then for advanced details 35 00:01:24,720 --> 00:01:27,540 ‫we want these instances to start with some user data. 36 00:01:27,540 --> 00:01:29,670 ‫And so we scroll all the way down 37 00:01:29,670 --> 00:01:34,110 ‫and here we copy and paste the user data. 38 00:01:34,110 --> 00:01:36,360 ‫Okay, so let's create this launch template. 39 00:01:36,360 --> 00:01:38,490 ‫As you can see, thanks to this launch template 40 00:01:38,490 --> 00:01:41,310 ‫we launch EC2 instances just like before. 41 00:01:41,310 --> 00:01:42,630 ‫So let's refresh this 42 00:01:42,630 --> 00:01:46,110 ‫and then click on the DemoLaunchTemplate of version 1. 43 00:01:46,110 --> 00:01:49,380 ‫So here it describes what is going to happen, the type 44 00:01:49,380 --> 00:01:51,900 ‫of instance we're going to have, the security groups 45 00:01:51,900 --> 00:01:53,190 ‫and so on. 46 00:01:53,190 --> 00:01:54,570 ‫So let's click on next. 47 00:01:54,570 --> 00:01:57,840 ‫Next we need to choose where to launch these instances. 48 00:01:57,840 --> 00:01:59,280 ‫So we have our VPC, 49 00:01:59,280 --> 00:02:01,290 ‫and then we can select multiple 50 00:02:01,290 --> 00:02:02,700 ‫availability zones and subnets. 51 00:02:02,700 --> 00:02:04,950 ‫So we select three of them. 52 00:02:04,950 --> 00:02:08,010 ‫And for instance type requirements, we can use the one 53 00:02:08,010 --> 00:02:10,680 ‫from the launch template or if you wanted to override them 54 00:02:10,680 --> 00:02:11,970 ‫but we don't need to actually. 55 00:02:11,970 --> 00:02:13,120 ‫So let's click on next. 56 00:02:14,550 --> 00:02:15,840 ‫Next we have load balancers. 57 00:02:15,840 --> 00:02:19,090 ‫So we want to attach to an existing load balancer 58 00:02:19,980 --> 00:02:22,650 ‫and it's going to be a application balancer. 59 00:02:22,650 --> 00:02:26,130 ‫And to do this, we're going to tell the ASG, 60 00:02:26,130 --> 00:02:29,640 ‫the auto scanning group, that every instance created should 61 00:02:29,640 --> 00:02:34,410 ‫be registered within my demo target group for my ALB. 62 00:02:34,410 --> 00:02:36,360 ‫So therefore all these instances will be 63 00:02:36,360 --> 00:02:37,650 ‫under the target group, 64 00:02:37,650 --> 00:02:39,060 ‫and then the load balancer will be able 65 00:02:39,060 --> 00:02:41,220 ‫to direct traffic to them. 66 00:02:41,220 --> 00:02:44,670 ‫So the health check can be EC2 and also ELB. 67 00:02:44,670 --> 00:02:45,870 ‫So we're good. 68 00:02:45,870 --> 00:02:48,453 ‫And then we can click on next. 69 00:02:49,710 --> 00:02:53,190 ‫Now here comes the scaling of the auto scaling group. 70 00:02:53,190 --> 00:02:56,190 ‫So the desired capacity is how many instances you want 71 00:02:56,190 --> 00:02:57,023 ‫at any time. 72 00:02:57,023 --> 00:02:58,620 ‫For example, we want two EC2 instances 73 00:02:58,620 --> 00:03:00,780 ‫to have some sort of load balancing. 74 00:03:00,780 --> 00:03:02,100 ‫The minimum capacity is one, 75 00:03:02,100 --> 00:03:04,680 ‫meaning we want at least one instance, 76 00:03:04,680 --> 00:03:08,490 ‫and the maximum for them is we want at most four instances. 77 00:03:08,490 --> 00:03:10,050 ‫But the desired is what matters the most 78 00:03:10,050 --> 00:03:13,560 ‫because this is the actual capacity that we're going to get. 79 00:03:13,560 --> 00:03:16,170 ‫Okay, so then do we want scaling policies? 80 00:03:16,170 --> 00:03:18,510 ‫This is too advanced, but you of course can set 81 00:03:18,510 --> 00:03:21,090 ‫scaling policies on a auto scaling group. 82 00:03:21,090 --> 00:03:22,470 ‫That's the whole point of it 83 00:03:22,470 --> 00:03:27,420 ‫which allows you to resize your ASG on demand. 84 00:03:27,420 --> 00:03:30,180 ‫If there is much more demand than is going to 85 00:03:30,180 --> 00:03:33,570 ‫have more instances, if there is less requests, 86 00:03:33,570 --> 00:03:37,113 ‫less demand than is going to be less instances. 87 00:03:38,160 --> 00:03:39,630 ‫So click on next. 88 00:03:39,630 --> 00:03:42,240 ‫Next, we don't need notifications, we don't need tags. 89 00:03:42,240 --> 00:03:43,500 ‫We can review everything. 90 00:03:43,500 --> 00:03:44,333 ‫It looks good. 91 00:03:44,333 --> 00:03:46,443 ‫And let's create our auto scaling group. 92 00:03:47,820 --> 00:03:51,720 ‫So now our auto scaling group is being created 93 00:03:51,720 --> 00:03:54,630 ‫and as you can see, the state is updating capacity 94 00:03:54,630 --> 00:03:58,650 ‫because we have zero instances in our ASG, but we want to, 95 00:03:58,650 --> 00:04:01,650 ‫so I can click on it to get a bit more details. 96 00:04:01,650 --> 00:04:03,783 ‫So let's go under activity. 97 00:04:04,710 --> 00:04:07,500 ‫And in here we have two activity history. 98 00:04:07,500 --> 00:04:11,490 ‫That is we are launching two new EC2 instances 99 00:04:11,490 --> 00:04:15,450 ‫because well, the desired capacity went from zero to two. 100 00:04:15,450 --> 00:04:16,950 ‫And so if we have a look 101 00:04:16,950 --> 00:04:20,670 ‫under the instance management tab, as you can see now, 102 00:04:20,670 --> 00:04:24,300 ‫2 EC2 instances are independent states. 103 00:04:24,300 --> 00:04:28,380 ‫So if I go under EC2 and look at my EC2 instances, 104 00:04:28,380 --> 00:04:31,980 ‫in that UI, we also see that two instances are running 105 00:04:31,980 --> 00:04:35,640 ‫and these have been created by my auto scaling group. 106 00:04:35,640 --> 00:04:38,370 ‫So the benefit is that now they are fully managed 107 00:04:38,370 --> 00:04:40,170 ‫by my auto scaling group, 108 00:04:40,170 --> 00:04:43,470 ‫and let's go see for example, in my target group as well. 109 00:04:43,470 --> 00:04:46,230 ‫So if I go to my target group on the left hand side 110 00:04:46,230 --> 00:04:50,190 ‫and look at my demo-tg-alb right here, scroll down. 111 00:04:50,190 --> 00:04:52,740 ‫As you can see now we have two total targets 112 00:04:52,740 --> 00:04:55,380 ‫and these are the targets created 113 00:04:55,380 --> 00:04:57,210 ‫by our auto scanning group. 114 00:04:57,210 --> 00:05:00,210 ‫So again, thanks to the integration that we've defined 115 00:05:00,210 --> 00:05:01,650 ‫between the auto scanning group 116 00:05:01,650 --> 00:05:06,090 ‫and the load balancer, we are able to have automatically 117 00:05:06,090 --> 00:05:09,360 ‫these new EC2 instances registered as targets 118 00:05:09,360 --> 00:05:11,370 ‫in our target group. 119 00:05:11,370 --> 00:05:13,260 ‫So currently they're unhealthy, 120 00:05:13,260 --> 00:05:16,020 ‫this is because the instance hasn't started all the way yet 121 00:05:16,020 --> 00:05:18,670 ‫so let's wait a little bit until they become healthy. 122 00:05:20,580 --> 00:05:23,580 ‫So to speed up the check from unhealthy to healthy, 123 00:05:23,580 --> 00:05:26,640 ‫you can go under health checks of your target group, 124 00:05:26,640 --> 00:05:29,070 ‫and here you can edit these settings. 125 00:05:29,070 --> 00:05:30,690 ‫And under other settings, we can say 126 00:05:30,690 --> 00:05:35,040 ‫that the healthy threshold is going to be 2 127 00:05:35,040 --> 00:05:38,670 ‫and the interval is going to be 5 seconds. 128 00:05:38,670 --> 00:05:41,910 ‫This is going to make the thing much quicker. 129 00:05:41,910 --> 00:05:44,160 ‫So of course the timeout needs to be 2 seconds, 130 00:05:44,160 --> 00:05:46,860 ‫something less than the interval itself. 131 00:05:46,860 --> 00:05:48,333 ‫So let's save our changes. 132 00:05:49,620 --> 00:05:52,020 ‫And now the health check change settings have changed. 133 00:05:52,020 --> 00:05:54,060 ‫Let's see, if I go back into my targets 134 00:05:54,060 --> 00:05:57,000 ‫and refresh, now both my instances are healthy. 135 00:05:57,000 --> 00:06:00,900 ‫We just made the health check happen faster and more often. 136 00:06:00,900 --> 00:06:03,120 ‫So now both my instances are healthy, 137 00:06:03,120 --> 00:06:08,120 ‫and so therefore if I go under my load balancer right here 138 00:06:08,280 --> 00:06:10,020 ‫and I look at the DNS name 139 00:06:10,020 --> 00:06:12,930 ‫and open it in a new tab, I get a hello world 140 00:06:12,930 --> 00:06:14,910 ‫from both my instances. 141 00:06:14,910 --> 00:06:18,540 ‫And this is cool because these two instances were created 142 00:06:18,540 --> 00:06:20,490 ‫by the auto scaling group. 143 00:06:20,490 --> 00:06:22,590 ‫So because now we have an auto scaling group 144 00:06:22,590 --> 00:06:24,450 ‫we can actually do some cool stuff. 145 00:06:24,450 --> 00:06:27,480 ‫So if we take one of these instances, for example 146 00:06:27,480 --> 00:06:29,280 ‫and we can for example terminate it, 147 00:06:29,280 --> 00:06:30,790 ‫so I'm going to click on it 148 00:06:33,810 --> 00:06:35,880 ‫and I'm under the instance itself 149 00:06:35,880 --> 00:06:38,853 ‫I will do instance date and then terminate instance. 150 00:06:41,670 --> 00:06:43,800 ‫Now it's been successfully terminated. 151 00:06:43,800 --> 00:06:45,030 ‫So what's going to happen is that 152 00:06:45,030 --> 00:06:47,850 ‫because this instance is being shutting down 153 00:06:47,850 --> 00:06:50,460 ‫and terminated, well, my auto scaling group 154 00:06:50,460 --> 00:06:53,520 ‫is going to detect that, guess what, one of these instance 155 00:06:53,520 --> 00:06:56,310 ‫is not in service anymore, it's being terminated. 156 00:06:56,310 --> 00:06:59,070 ‫And so therefore, because we have an auto scaling group 157 00:06:59,070 --> 00:07:02,730 ‫with a desired capacity of two instances, 158 00:07:02,730 --> 00:07:05,670 ‫automatically a new instance should appear. 159 00:07:05,670 --> 00:07:07,680 ‫So let's observe this behavior 160 00:07:07,680 --> 00:07:11,610 ‫by having here the activity history. 161 00:07:11,610 --> 00:07:12,600 ‫And as you can see, 162 00:07:12,600 --> 00:07:15,870 ‫in progress was terminating EC2 instance. 163 00:07:15,870 --> 00:07:17,970 ‫And so an instance was taken out of service 164 00:07:17,970 --> 00:07:20,730 ‫because, well, it's been terminated. 165 00:07:20,730 --> 00:07:23,430 ‫And then we have a new activity saying, 166 00:07:23,430 --> 00:07:26,340 ‫hey, an instance was launched in response 167 00:07:26,340 --> 00:07:28,830 ‫to an unhealthy instance needing to be replaced. 168 00:07:28,830 --> 00:07:30,900 ‫So it's very cool because the auto scaling group 169 00:07:30,900 --> 00:07:33,720 ‫can automatically detect unhealthy instances 170 00:07:33,720 --> 00:07:36,390 ‫and create new one for replacements. 171 00:07:36,390 --> 00:07:39,330 ‫So if we go here now, there's one instance in pending state 172 00:07:39,330 --> 00:07:42,660 ‫which is being started, one instance being terminated 173 00:07:42,660 --> 00:07:44,610 ‫and one instance in service. 174 00:07:44,610 --> 00:07:47,790 ‫And this is the whole power of auto scaling groups. 175 00:07:47,790 --> 00:07:49,290 ‫Of course you can go to the next level 176 00:07:49,290 --> 00:07:50,550 ‫but for now we know enough 177 00:07:50,550 --> 00:07:52,620 ‫which is around automatic scaling to actually 178 00:07:52,620 --> 00:07:55,680 ‫define scaling policies to automatically increase 179 00:07:55,680 --> 00:07:57,960 ‫or decrease the desired capacity over time, 180 00:07:57,960 --> 00:08:00,120 ‫based on our load and so on. 181 00:08:00,120 --> 00:08:02,580 ‫But here you've seen the basics 182 00:08:02,580 --> 00:08:05,310 ‫and the major features of auto scaling groups, 183 00:08:05,310 --> 00:08:06,810 ‫and you could play around 184 00:08:06,810 --> 00:08:09,570 ‫by editing the desired capacity yourself 185 00:08:09,570 --> 00:08:12,000 ‫to set it to one, for example, to terminate instances 186 00:08:12,000 --> 00:08:14,790 ‫and only keep one of them, or to set it to four 187 00:08:14,790 --> 00:08:17,820 ‫and see the auto scaling group creates multiple instances 188 00:08:17,820 --> 00:08:20,670 ‫that will be registered with our load balancer. 189 00:08:20,670 --> 00:08:23,400 ‫And so therefore the traffic is going to be distributed 190 00:08:23,400 --> 00:08:24,980 ‫between four instances. 191 00:08:24,980 --> 00:08:26,160 ‫So I hope you liked it 192 00:08:26,160 --> 00:08:28,110 ‫and I will see you in the next lecture.