1 00:00:00,500 --> 00:00:01,890 Okay, so let's practice 2 00:00:01,890 --> 00:00:03,010 using Auto Scaling groups. 3 00:00:03,010 --> 00:00:04,410 To do so, first of all, 4 00:00:04,410 --> 00:00:06,600 please make sure to take your instances 5 00:00:06,600 --> 00:00:07,660 and terminate them all 6 00:00:07,660 --> 00:00:11,760 to have zero instances running in EC2. 7 00:00:11,760 --> 00:00:13,200 Okay, so next we have to create 8 00:00:13,200 --> 00:00:14,300 an Auto Scaling group. 9 00:00:14,300 --> 00:00:16,020 So let's go into Auto Scaling groups 10 00:00:16,020 --> 00:00:17,380 on the left hand side, 11 00:00:17,380 --> 00:00:18,350 and we're going to create 12 00:00:18,350 --> 00:00:19,500 our first Auto Scaling group. 13 00:00:19,500 --> 00:00:21,060 So create Auto Scaling group, 14 00:00:21,060 --> 00:00:24,320 let's name our Auto Scaling group DemoASG, 15 00:00:24,320 --> 00:00:26,360 and we need to refer to a launch template, 16 00:00:26,360 --> 00:00:29,223 so I'm going to create a launch template right now. 17 00:00:30,680 --> 00:00:32,378 So when you create a launch template 18 00:00:32,378 --> 00:00:33,563 you need to give it a name, 19 00:00:33,563 --> 00:00:35,063 I will give it MyDemoTemplate, 20 00:00:36,880 --> 00:00:38,713 as well as a description, 21 00:00:39,780 --> 00:00:40,820 Template. 22 00:00:40,820 --> 00:00:44,050 Okay, so we're going to scroll down 23 00:00:44,050 --> 00:00:45,940 and specify different options. 24 00:00:45,940 --> 00:00:49,221 So for the Amazon Machine Image I'm going to use, 25 00:00:49,221 --> 00:00:50,700 I'm going to go into Quick Start, 26 00:00:50,700 --> 00:00:51,533 Amazon Linux, 27 00:00:51,533 --> 00:00:53,020 I would choose Amazon Linux 2, 28 00:00:53,020 --> 00:00:54,263 in the version x86. 29 00:00:55,443 --> 00:00:58,690 Okay, and we'll use a free tier eligible AMI, 30 00:00:58,690 --> 00:00:59,900 so that's good. 31 00:00:59,900 --> 00:01:01,000 Next for Instance type, 32 00:01:01,000 --> 00:01:03,010 I'm going to select t2.micro 33 00:01:03,010 --> 00:01:04,410 because it is free tier eligible, 34 00:01:04,410 --> 00:01:06,830 so anything free tier eligible is good. 35 00:01:06,830 --> 00:01:08,770 And I will scroll down in terms of Key pair, 36 00:01:08,770 --> 00:01:11,010 I will include EC2 Tutorial, 37 00:01:11,010 --> 00:01:12,880 so this is defining pretty much 38 00:01:12,880 --> 00:01:15,897 how EC2 instances within my launch template 39 00:01:15,897 --> 00:01:18,350 and therefore within my ASG, 40 00:01:18,350 --> 00:01:19,183 should be launched. 41 00:01:19,183 --> 00:01:20,780 Okay, so the options are very similar 42 00:01:20,780 --> 00:01:22,000 to the ones you've seen 43 00:01:22,000 --> 00:01:24,493 when you launch an EC2 instance on its own. 44 00:01:25,620 --> 00:01:27,580 For subnets you don't include it 45 00:01:27,580 --> 00:01:28,940 in the launch templates. 46 00:01:28,940 --> 00:01:30,050 For Firewall security group 47 00:01:30,050 --> 00:01:31,220 you can select an existing one, 48 00:01:31,220 --> 00:01:34,240 for example, your launch-wizard-1. 49 00:01:34,240 --> 00:01:36,750 You have 8 gigabytes of gp2 volume, 50 00:01:36,750 --> 00:01:37,900 this is good. 51 00:01:37,900 --> 00:01:40,210 And then for Advanced details, 52 00:01:40,210 --> 00:01:42,540 I'm going to scroll all the way down, 53 00:01:42,540 --> 00:01:44,800 and I will find my User data, 54 00:01:44,800 --> 00:01:47,130 and for User data I'm going to input 55 00:01:47,130 --> 00:01:49,160 the user data we used from before, 56 00:01:49,160 --> 00:01:49,993 so all of it, 57 00:01:49,993 --> 00:01:52,780 and this is going to allow a web server 58 00:01:52,780 --> 00:01:54,770 to be created on every EC2 instance 59 00:01:54,770 --> 00:01:56,503 as part of this ASG. 60 00:01:57,640 --> 00:01:58,473 So we're good to go, 61 00:01:58,473 --> 00:01:59,880 let's create this launch template, 62 00:01:59,880 --> 00:02:00,713 it has been created, 63 00:02:00,713 --> 00:02:02,439 called the MyDemoTemplate. 64 00:02:02,439 --> 00:02:03,710 I'm going to refresh it 65 00:02:03,710 --> 00:02:04,960 and select it right here. 66 00:02:04,960 --> 00:02:07,970 So we have MyDemoTemplate Version 1. 67 00:02:07,970 --> 00:02:09,509 We have the summary of it, 68 00:02:09,509 --> 00:02:12,520 and we click on next to keep on going. 69 00:02:12,520 --> 00:02:14,580 So we choose the instance launch options, 70 00:02:14,580 --> 00:02:15,810 we have a VPC, 71 00:02:15,810 --> 00:02:17,020 which is default VPC, 72 00:02:17,020 --> 00:02:18,440 as well as the ability 73 00:02:18,440 --> 00:02:19,960 to launch into three different subnets, 74 00:02:19,960 --> 00:02:23,063 so eu-west-1a, 1b, and 1c. 75 00:02:24,320 --> 00:02:25,153 Instance requirements. 76 00:02:25,153 --> 00:02:27,980 So we have those provided by the templates 77 00:02:27,980 --> 00:02:29,610 which is a t2.micro, 78 00:02:29,610 --> 00:02:31,370 but we could, if we wanted to, 79 00:02:31,370 --> 00:02:33,250 override them, okay? 80 00:02:33,250 --> 00:02:34,480 And when you override them 81 00:02:34,480 --> 00:02:36,240 you can specify instant attributes 82 00:02:36,240 --> 00:02:38,200 or manually add instance types. 83 00:02:38,200 --> 00:02:40,080 And you can have a mix of on demand 84 00:02:40,080 --> 00:02:41,812 and spot instances, 85 00:02:41,812 --> 00:02:44,680 as well as a mix of different instance types. 86 00:02:44,680 --> 00:02:46,390 So this is definitely more advanced, 87 00:02:46,390 --> 00:02:48,450 but for now we'll just keep it 88 00:02:48,450 --> 00:02:50,490 as the instance type requirements 89 00:02:50,490 --> 00:02:52,130 from the Launch template, 90 00:02:52,130 --> 00:02:55,113 so we'll have it as t2.micro only. 91 00:02:56,020 --> 00:02:57,360 Let's click on Next. 92 00:02:57,360 --> 00:02:59,120 Then we can configure advanced options, 93 00:02:59,120 --> 00:03:00,630 so Load balancing is optional, 94 00:03:00,630 --> 00:03:02,380 so we can create an ASG 95 00:03:02,380 --> 00:03:03,890 without a load balancer, 96 00:03:03,890 --> 00:03:06,030 and there are a few use cases for that, okay? 97 00:03:06,030 --> 00:03:08,720 But we already have an load balancer, 98 00:03:08,720 --> 00:03:11,150 so we're going to Attach to an existing load balancer 99 00:03:11,150 --> 00:03:13,320 and it will choose my-first-target-group 100 00:03:13,320 --> 00:03:16,010 from my DemoALB application balancer. 101 00:03:16,010 --> 00:03:18,400 So that the EC2 instances launched 102 00:03:18,400 --> 00:03:21,030 as part of this ASG are linked 103 00:03:21,030 --> 00:03:24,950 to my application load balancer directly. 104 00:03:24,950 --> 00:03:26,100 For Health checks this is optional, 105 00:03:26,100 --> 00:03:28,390 but again we have two kinds of health checks, 106 00:03:28,390 --> 00:03:29,730 the first is the EC2 health check. 107 00:03:29,730 --> 00:03:31,860 So if my EC2 instance fails 108 00:03:31,860 --> 00:03:33,550 is going to be removed automatically 109 00:03:33,550 --> 00:03:35,330 from my Auto Scaling group, 110 00:03:35,330 --> 00:03:37,220 and we can also have an ELB type 111 00:03:37,220 --> 00:03:38,370 of health check enabled, 112 00:03:38,370 --> 00:03:39,790 and we'll leave it enabled. 113 00:03:39,790 --> 00:03:42,130 So that if the ELB deems 114 00:03:42,130 --> 00:03:44,310 that an instance is unhealthy, 115 00:03:44,310 --> 00:03:47,233 then the ASG will terminate it automatically. 116 00:03:48,950 --> 00:03:52,170 Okay, we will not enable any additional settings. 117 00:03:52,170 --> 00:03:53,150 Click on Next. 118 00:03:53,150 --> 00:03:55,150 So this is where you set your ASG Group size. 119 00:03:55,150 --> 00:03:57,233 So Desired capacity is 1 120 00:03:57,233 --> 00:03:58,066 Minimum capacity is 1, 121 00:03:58,066 --> 00:03:59,260 and Maximum capacity is 1, 122 00:03:59,260 --> 00:04:00,510 so that means that we have one instance 123 00:04:00,510 --> 00:04:01,660 within the ASG, 124 00:04:01,660 --> 00:04:04,390 but if we could specify 10 as max capacity, 125 00:04:04,390 --> 00:04:06,930 which would allow us to have a varying size 126 00:04:06,930 --> 00:04:10,800 of the capacity of the ASG between 1 and 10. 127 00:04:10,800 --> 00:04:13,140 But I'll keep everything as 1 right now. 128 00:04:13,140 --> 00:04:15,560 Scaling policies for now I will keep it as None, 129 00:04:15,560 --> 00:04:16,519 but we'll have a look at them 130 00:04:16,519 --> 00:04:18,440 in details later on. 131 00:04:18,440 --> 00:04:20,279 Let's click on Next. 132 00:04:20,279 --> 00:04:21,820 No notifications, 133 00:04:21,820 --> 00:04:22,960 no tags, 134 00:04:22,960 --> 00:04:24,250 we review everything, 135 00:04:24,250 --> 00:04:27,383 and then we click on Create Auto Scaling Group. 136 00:04:29,420 --> 00:04:32,970 Okay, so my Auto Scaling Group is created, 137 00:04:32,970 --> 00:04:34,100 and what is going to do is 138 00:04:34,100 --> 00:04:35,500 that it's going to create 139 00:04:35,500 --> 00:04:38,410 one EC2 instance for me. 140 00:04:38,410 --> 00:04:40,430 So the idea here is that if I click 141 00:04:40,430 --> 00:04:41,740 on my Auto Scouting Group, 142 00:04:41,740 --> 00:04:43,100 and we get some details, 143 00:04:43,100 --> 00:04:45,930 we can see the desired min and max capacity, 144 00:04:45,930 --> 00:04:47,330 as well as the Launch template 145 00:04:47,330 --> 00:04:49,573 that was used for this ASG. 146 00:04:50,500 --> 00:04:52,500 And then if I go into Activity 147 00:04:52,500 --> 00:04:55,130 this is where the interesting stuff happens. 148 00:04:55,130 --> 00:04:57,510 So when the ASG will decide 149 00:04:57,510 --> 00:04:59,750 to create an EC2 instance, 150 00:04:59,750 --> 00:05:03,160 it's going to appear in the Activity notifications, 151 00:05:03,160 --> 00:05:05,710 so if I refresh this, very soon, 152 00:05:05,710 --> 00:05:07,720 sorry, in the Activity history. 153 00:05:07,720 --> 00:05:09,980 So if I refresh the Activity history, 154 00:05:09,980 --> 00:05:11,860 then I can see that there is 155 00:05:11,860 --> 00:05:14,180 a Launching a new instance right here, 156 00:05:14,180 --> 00:05:16,260 because the Auto Scaling group 157 00:05:16,260 --> 00:05:17,380 has a capacity of 0, 158 00:05:17,380 --> 00:05:19,100 and we wanted 1 capacity, 159 00:05:19,100 --> 00:05:21,120 so it's just increasing the number 160 00:05:21,120 --> 00:05:23,080 of EC2 instances in our ASG, 161 00:05:23,080 --> 00:05:25,400 to match the desired capacity. 162 00:05:25,400 --> 00:05:27,570 So if I go to the tab Instance Management, 163 00:05:27,570 --> 00:05:28,660 as we can see, 164 00:05:28,660 --> 00:05:31,930 one EC2 instance was created by the ASG. 165 00:05:31,930 --> 00:05:34,620 And so if we go to the Instances tab right here, 166 00:05:34,620 --> 00:05:36,660 we can see that the EC2 instance 167 00:05:36,660 --> 00:05:39,500 is currently Running and initializing. 168 00:05:39,500 --> 00:05:40,780 So it's pretty cool. 169 00:05:40,780 --> 00:05:41,613 Now the cool thing is that 170 00:05:41,613 --> 00:05:44,220 because we had set up our ASG 171 00:05:44,220 --> 00:05:46,290 to be linked to our Target Groups, 172 00:05:46,290 --> 00:05:48,610 then if we go to the Target Groups 173 00:05:48,610 --> 00:05:49,713 on the left hand side, 174 00:05:51,810 --> 00:05:53,800 and find my-first-target-group, 175 00:05:53,800 --> 00:05:55,240 and go to Targets, 176 00:05:55,240 --> 00:05:57,190 my EC2 instance is being, 177 00:05:57,190 --> 00:05:58,520 and I will just expand it, 178 00:05:58,520 --> 00:06:00,630 my EC2 instance is being registered 179 00:06:00,630 --> 00:06:02,500 into my ALB. 180 00:06:02,500 --> 00:06:03,710 So right now it is shows that 181 00:06:03,710 --> 00:06:06,170 as unhealthy because the object are failing, 182 00:06:06,170 --> 00:06:09,620 because the EC2 instance is still bootstrapping. 183 00:06:09,620 --> 00:06:12,830 But if after a bit it passes, 184 00:06:12,830 --> 00:06:14,490 it will be shown as healthy, 185 00:06:14,490 --> 00:06:17,180 and then will be registered into my ALB. 186 00:06:17,180 --> 00:06:19,330 So let me pause the video to wait for that. 187 00:06:20,330 --> 00:06:23,230 Okay, so my instance is now healthy, 188 00:06:23,230 --> 00:06:26,530 and then if I go to my ALB and refresh, 189 00:06:26,530 --> 00:06:28,340 I get the Hello World response, 190 00:06:28,340 --> 00:06:30,120 that means that everything is functioning, 191 00:06:30,120 --> 00:06:32,910 the instance was created by an ESG. 192 00:06:32,910 --> 00:06:35,010 We can see it in the EC2 console, 193 00:06:35,010 --> 00:06:36,930 it was registered into the target group 194 00:06:36,930 --> 00:06:38,370 which is linked to our load balancer, 195 00:06:38,370 --> 00:06:41,550 and therefore our load balancer is fully working. 196 00:06:41,550 --> 00:06:43,660 Now, if the instance never gets healthy 197 00:06:43,660 --> 00:06:45,060 it gets unhealthy, 198 00:06:45,060 --> 00:06:46,930 then because we set up our ASG 199 00:06:46,930 --> 00:06:48,510 to terminate unhealthy instances, 200 00:06:48,510 --> 00:06:49,343 what you're going to see is 201 00:06:49,343 --> 00:06:51,730 that the instance is going to be terminated, 202 00:06:51,730 --> 00:06:53,490 and a new one will be created. 203 00:06:53,490 --> 00:06:55,060 So you will see this mostly 204 00:06:55,060 --> 00:06:56,560 in the Activity history. 205 00:06:56,560 --> 00:06:58,000 If you do have this, 206 00:06:58,000 --> 00:06:59,680 the reason why this is happening 207 00:06:59,680 --> 00:07:02,740 is that your EC2 instance is misconfigured. 208 00:07:02,740 --> 00:07:05,570 This could be either a security group issue 209 00:07:05,570 --> 00:07:08,040 or it could be an EC2 user data script issue. 210 00:07:08,040 --> 00:07:09,320 So please check those 211 00:07:09,320 --> 00:07:11,500 before asking questions in the Q&A. 212 00:07:11,500 --> 00:07:13,020 Okay, but these are the errors 213 00:07:13,020 --> 00:07:14,290 that you should be having. 214 00:07:14,290 --> 00:07:17,410 So now we have one EC2 instance 215 00:07:17,410 --> 00:07:18,830 and it's in service. 216 00:07:18,830 --> 00:07:19,880 And what we could do 217 00:07:19,880 --> 00:07:21,810 is that we can experience the scaling 218 00:07:21,810 --> 00:07:24,110 by editing the Auto Scaling group size. 219 00:07:24,110 --> 00:07:26,210 So we want two instances, 220 00:07:26,210 --> 00:07:27,060 and so we have to increase 221 00:07:27,060 --> 00:07:28,840 the maximum capacity as well. 222 00:07:28,840 --> 00:07:30,620 So we'll update this. 223 00:07:30,620 --> 00:07:32,890 And now what we told the ASG 224 00:07:32,890 --> 00:07:34,960 is that we want you to create one 225 00:07:34,960 --> 00:07:37,140 or more EC2 instance for us. 226 00:07:37,140 --> 00:07:39,870 So if you go to the Activity history 227 00:07:39,870 --> 00:07:41,593 and then refresh it, 228 00:07:42,770 --> 00:07:45,830 very soon is going to show a new activity 229 00:07:45,830 --> 00:07:47,790 that it will be trying to be launching 230 00:07:47,790 --> 00:07:48,730 an EC2 instance. 231 00:07:48,730 --> 00:07:49,690 So here we go, 232 00:07:49,690 --> 00:07:51,920 now it says Launching a new EC2 instance, 233 00:07:51,920 --> 00:07:54,310 because you have changed the desire capacity 234 00:07:54,310 --> 00:07:55,550 from 1 to 2, 235 00:07:55,550 --> 00:07:57,660 and the actual number of instances 236 00:07:57,660 --> 00:07:59,820 in our a ASG was only one. 237 00:07:59,820 --> 00:08:00,920 So now, as you can see, 238 00:08:00,920 --> 00:08:03,710 we have a second EC2 instance being created, 239 00:08:03,710 --> 00:08:06,290 and after a while is going to be registered 240 00:08:06,290 --> 00:08:09,170 into our target group and added. 241 00:08:09,170 --> 00:08:10,050 So I will pause the video 242 00:08:10,050 --> 00:08:11,453 for this process to happen. 243 00:08:12,320 --> 00:08:14,740 Okay, so my instance is now healthy. 244 00:08:14,740 --> 00:08:16,586 And so if I go to my ALB and refresh, 245 00:08:16,586 --> 00:08:20,030 as we can see I got my two IPs 246 00:08:20,030 --> 00:08:21,370 that are looping through my ALB. 247 00:08:21,370 --> 00:08:23,400 So everything is working great. 248 00:08:23,400 --> 00:08:24,580 And so one last thing, 249 00:08:24,580 --> 00:08:25,470 so if we go to activities, 250 00:08:25,470 --> 00:08:27,550 we can see the two instances 251 00:08:27,550 --> 00:08:28,800 were launched successfully. 252 00:08:28,800 --> 00:08:30,190 Now let's go through the opposite, 253 00:08:30,190 --> 00:08:32,970 we're going to scale down our capacity 254 00:08:32,970 --> 00:08:35,830 by putting the Desired capacity back to 1, 255 00:08:35,830 --> 00:08:36,799 and update it, 256 00:08:36,799 --> 00:08:37,720 and what this will do 257 00:08:37,720 --> 00:08:38,707 is that it will say, 258 00:08:38,707 --> 00:08:41,350 "hey, it looks like you have two instances, 259 00:08:41,350 --> 00:08:43,380 but now you need only one, 260 00:08:43,380 --> 00:08:45,000 so I'm going to pick one 261 00:08:45,000 --> 00:08:46,740 of these two instances right here 262 00:08:46,740 --> 00:08:48,290 and terminate them." 263 00:08:48,290 --> 00:08:49,680 So as you can see here, 264 00:08:49,680 --> 00:08:51,840 it says, "I'm terminating one of these instances 265 00:08:51,840 --> 00:08:54,650 because of the change of the configuration 266 00:08:54,650 --> 00:08:55,610 of the ASG," 267 00:08:55,610 --> 00:08:57,310 and is going to terminate the instance 268 00:08:57,310 --> 00:08:59,640 and then deregister it from the target group. 269 00:08:59,640 --> 00:09:00,473 And then we'll be back 270 00:09:00,473 --> 00:09:02,930 with only one EC2 instance in our ASG. 271 00:09:02,930 --> 00:09:04,120 So that's it for the lecture, 272 00:09:04,120 --> 00:09:06,220 you've seen the whole power of ASG, 273 00:09:06,220 --> 00:09:07,770 and I will see you in the next lecture 274 00:09:07,770 --> 00:09:09,493 to talk about Automatic scaling.