1 00:00:00,210 --> 00:00:02,020 Okay, so let's do a deep dive 2 00:00:02,020 --> 00:00:04,680 on EC2 spot instances. 3 00:00:04,680 --> 00:00:06,570 We know that with a spot instance, 4 00:00:06,570 --> 00:00:10,870 you can get a discount up to 90% compared to On-Demand. 5 00:00:10,870 --> 00:00:12,420 So that's huge cost saving. 6 00:00:12,420 --> 00:00:13,950 How does that work? 7 00:00:13,950 --> 00:00:17,190 We define a max spot price that we're willing to pay 8 00:00:17,190 --> 00:00:18,440 for that spot instance. 9 00:00:18,440 --> 00:00:23,080 And then as long as the instance is having a spot price 10 00:00:23,080 --> 00:00:25,910 that is less than the max price we're willing to pay, 11 00:00:25,910 --> 00:00:27,810 then we keep that instance. 12 00:00:27,810 --> 00:00:29,520 So that max spot price, I will show it to you 13 00:00:29,520 --> 00:00:30,520 in the next slide. 14 00:00:30,520 --> 00:00:33,880 But the hourly spot will vary based on offer and capacity, 15 00:00:33,880 --> 00:00:35,720 and so it will go up and it will go down. 16 00:00:35,720 --> 00:00:38,500 And if somehow the current spot price 17 00:00:38,500 --> 00:00:40,940 goes over the max price you have defined, 18 00:00:40,940 --> 00:00:42,510 then you have two options. 19 00:00:42,510 --> 00:00:43,910 And for these two options, 20 00:00:43,910 --> 00:00:46,120 you have a two minutes grace period. 21 00:00:46,120 --> 00:00:48,170 So that gives you a little bit of time to do these things. 22 00:00:48,170 --> 00:00:51,550 And the options can be either you're stopping your instance, 23 00:00:51,550 --> 00:00:54,020 that means that you shut down everything you were doing, 24 00:00:54,020 --> 00:00:55,450 then you stop your instance. 25 00:00:55,450 --> 00:00:58,293 And so if one day, the spot price goes below your max price, 26 00:00:58,293 --> 00:01:00,220 then you can restart your instance 27 00:01:00,220 --> 00:01:02,660 and continue where you left it off. 28 00:01:02,660 --> 00:01:05,720 Or if you don't need the state on your EC2 instance, 29 00:01:05,720 --> 00:01:08,400 you can just choose to terminate your instance 30 00:01:08,400 --> 00:01:09,250 and let it go. 31 00:01:09,250 --> 00:01:10,460 And then that would assume 32 00:01:10,460 --> 00:01:12,210 that anytime you would restart your work, 33 00:01:12,210 --> 00:01:15,000 you can start with a fresh new EC2 instance. 34 00:01:15,000 --> 00:01:17,650 So two strategies based on the type of workload you have, 35 00:01:17,650 --> 00:01:20,530 but you have a two minutes grace periods to do so. 36 00:01:20,530 --> 00:01:23,540 The other strategy, if you don't want your spot instance 37 00:01:23,540 --> 00:01:27,140 to be reclaimed by AWS is to use a spot block. 38 00:01:27,140 --> 00:01:30,410 A spot block is when you block a spot instance 39 00:01:30,410 --> 00:01:32,330 for a specified timeframe. 40 00:01:32,330 --> 00:01:35,000 This can be between one to six hours, 41 00:01:35,000 --> 00:01:38,410 and you get that block without any interruption, 42 00:01:38,410 --> 00:01:40,970 at least on paper, because in the documentation, 43 00:01:40,970 --> 00:01:43,540 but it's really rare, in rare situations, 44 00:01:43,540 --> 00:01:45,430 the instance may be reclaimed. 45 00:01:45,430 --> 00:01:47,850 But overall, when you consider a spot block 46 00:01:47,850 --> 00:01:49,880 think that the instance will not be reclaimed, 47 00:01:49,880 --> 00:01:52,290 that's the whole purpose of the spot block. 48 00:01:52,290 --> 00:01:54,510 So when do we use spot instances? 49 00:01:54,510 --> 00:01:56,530 Well, for batch jobs, data analysis, 50 00:01:56,530 --> 00:01:58,200 workloads that are resilient to failures, 51 00:01:58,200 --> 00:01:59,840 and as I said, they're not great 52 00:01:59,840 --> 00:02:02,410 for critical jobs or databases. 53 00:02:02,410 --> 00:02:05,610 Now let's have a look at the spot instance pricing. 54 00:02:05,610 --> 00:02:07,270 So this is a graph that I obtained directly 55 00:02:07,270 --> 00:02:09,250 from the AWS console. 56 00:02:09,250 --> 00:02:11,760 As you can see here, this gives us the price 57 00:02:11,760 --> 00:02:16,676 for a m4.large instance over three months in the spot zone. 58 00:02:16,676 --> 00:02:20,293 As you can see, you have six lines for US-East-1, 59 00:02:21,732 --> 00:02:23,360 the region and you have six availability zones, 60 00:02:23,360 --> 00:02:24,270 so six prices. 61 00:02:24,270 --> 00:02:27,200 So as you can see, the spot price does vary 62 00:02:27,200 --> 00:02:29,550 based on the AZ you're considering. 63 00:02:29,550 --> 00:02:33,660 As we can see, over three months, it has varied quite a bit. 64 00:02:33,660 --> 00:02:35,470 For example, if you look at that yellow line, 65 00:02:35,470 --> 00:02:38,900 it has varied between 0.4 to 0.45, 66 00:02:38,900 --> 00:02:43,720 all the way down to almost 0.35, let's say and so on. 67 00:02:43,720 --> 00:02:47,630 Let's assume that we are defining a user defined max price 68 00:02:47,630 --> 00:02:51,333 to be this black horizontal bar dotted line. 69 00:02:52,340 --> 00:02:56,300 We'd see is that if the yellow line is above 70 00:02:56,300 --> 00:02:59,380 the dotted line, then the current spot price is going 71 00:02:59,380 --> 00:03:02,090 to be more than the max price we have defined. 72 00:03:02,090 --> 00:03:03,770 In this instance, right here, 73 00:03:03,770 --> 00:03:05,640 we're going to lose our spot instance. 74 00:03:05,640 --> 00:03:07,790 So we have to terminate it or stop it. 75 00:03:07,790 --> 00:03:09,500 But if the price goes down, 76 00:03:09,500 --> 00:03:13,080 so if the yellow line goes down our dotted line, 77 00:03:13,080 --> 00:03:15,930 that means that we are willing to pay that price. 78 00:03:15,930 --> 00:03:18,810 We are gaining that spot instance, in this meantime. 79 00:03:18,810 --> 00:03:20,740 This is how we can optimize the cost 80 00:03:20,740 --> 00:03:23,170 of our workload very easily. 81 00:03:23,170 --> 00:03:28,160 As you can see, the On-Demand price is $0.10 per hour, 82 00:03:28,160 --> 00:03:29,350 so that's really, really cheap. 83 00:03:29,350 --> 00:03:32,610 And then the spot instance price is even cheaper. 84 00:03:32,610 --> 00:03:35,430 It's around 0.4, I would say. 85 00:03:35,430 --> 00:03:38,500 It's a 60% saving in that instance, 86 00:03:38,500 --> 00:03:40,990 for the spot instance pricing, so lots of savings. 87 00:03:40,990 --> 00:03:43,000 And obviously, if you were to define 88 00:03:43,000 --> 00:03:44,550 your user defined max price 89 00:03:44,550 --> 00:03:48,560 to be something like $0.05 per hour, 90 00:03:48,560 --> 00:03:51,110 then no in-spot instances will be reclaimed 91 00:03:51,110 --> 00:03:52,030 because you're always willing 92 00:03:52,030 --> 00:03:54,930 to pay more than the actual spot instance price. 93 00:03:54,930 --> 00:03:57,770 So no one can predict how the price goes over time. 94 00:03:57,770 --> 00:03:59,760 But as you can see, it can be rather stable. 95 00:03:59,760 --> 00:04:01,930 It can fluctuate a little bit, but overall, 96 00:04:01,930 --> 00:04:05,373 it still provides you huge savings compared to On-Demand. 97 00:04:06,670 --> 00:04:09,780 Next, how do you terminate a spot instance? 98 00:04:09,780 --> 00:04:13,380 So we have to first understand how a spot request works. 99 00:04:13,380 --> 00:04:15,630 For this, let's consider a spot request. 100 00:04:15,630 --> 00:04:16,920 With a spot request, 101 00:04:16,920 --> 00:04:19,940 you are defining how many instances you want, 102 00:04:19,940 --> 00:04:22,029 your maximum price you're going to pay, 103 00:04:22,029 --> 00:04:24,630 the launch specification, so the AMI and so on, 104 00:04:24,630 --> 00:04:27,280 and when your request is valid from and until, 105 00:04:27,280 --> 00:04:30,180 but it could be infinite, and then the request type. 106 00:04:30,180 --> 00:04:31,370 It's very important to understand 107 00:04:31,370 --> 00:04:32,940 there's two types of requests. 108 00:04:32,940 --> 00:04:35,820 You can do one-time request for spot instances 109 00:04:35,820 --> 00:04:38,930 or a persistent request for post instances. 110 00:04:38,930 --> 00:04:40,950 Let's have a look at the difference. 111 00:04:40,950 --> 00:04:42,710 If it's a one-time request, 112 00:04:42,710 --> 00:04:45,560 as soon as your spot request is fulfilled, 113 00:04:45,560 --> 00:04:47,630 your instances are going to be launched. 114 00:04:47,630 --> 00:04:50,490 And then your spot request will go away 115 00:04:50,490 --> 00:04:52,570 because it was a one-time request type. 116 00:04:52,570 --> 00:04:55,370 So in this case, your spot request is going to go away 117 00:04:55,370 --> 00:04:56,520 and you're fine. 118 00:04:56,520 --> 00:04:58,770 But if it's a persistent request type, 119 00:04:58,770 --> 00:05:02,620 that means that we want this number of instances to be valid 120 00:05:02,620 --> 00:05:06,810 as long as the spot request is valid from to valid until. 121 00:05:06,810 --> 00:05:10,830 That means that if somehow your instances do get stopped, 122 00:05:10,830 --> 00:05:12,980 or interrupted based on the spot price, 123 00:05:12,980 --> 00:05:16,340 then your spot requests will go back into action. 124 00:05:16,340 --> 00:05:18,350 When things can be validated 125 00:05:18,350 --> 00:05:21,220 we'll restart spot instances for you. 126 00:05:21,220 --> 00:05:25,200 So if somehow you stop a spot instance, in persistent mode, 127 00:05:25,200 --> 00:05:27,400 and your spot request is still active, 128 00:05:27,400 --> 00:05:30,090 your spot request automatically will be smart enough 129 00:05:30,090 --> 00:05:32,690 to restart a launch instance for you. 130 00:05:32,690 --> 00:05:35,300 And this is something you can also see in this diagram, 131 00:05:35,300 --> 00:05:36,920 but I won't spend too much time on it. 132 00:05:36,920 --> 00:05:38,230 So what you understand out of it 133 00:05:38,230 --> 00:05:42,600 is that if you want to cancel a spot request, 134 00:05:42,600 --> 00:05:44,670 it needs to be in the open state, 135 00:05:44,670 --> 00:05:46,600 the active state, or the disabled state. 136 00:05:46,600 --> 00:05:48,130 That means that basically, it's not failed, 137 00:05:48,130 --> 00:05:50,030 it's not cancelled or it's not closed. 138 00:05:51,164 --> 00:05:54,130 When you want to cancel a spot request, 139 00:05:54,130 --> 00:05:56,050 so if you take the spot request on the left hand side 140 00:05:56,050 --> 00:06:00,123 and you cancel it, it's not going to terminate any instances 141 00:06:00,123 --> 00:06:01,900 that you have launched from before. 142 00:06:01,900 --> 00:06:04,230 So it is still your responsibility 143 00:06:04,230 --> 00:06:05,980 to terminate these instances 144 00:06:05,980 --> 00:06:09,060 and not the responsibility of AWS. 145 00:06:09,060 --> 00:06:12,220 So as such, if you want to terminate spot instances 146 00:06:12,220 --> 00:06:14,390 for good and not have them relaunched, 147 00:06:14,390 --> 00:06:17,500 you need to first cancel the spot request, 148 00:06:17,500 --> 00:06:21,410 and then you terminate the associated spot instances. 149 00:06:21,410 --> 00:06:25,200 Because if you were to terminate the spot instances first, 150 00:06:25,200 --> 00:06:27,300 remember, it goes back into the spot request 151 00:06:27,300 --> 00:06:29,920 and a spot request says, okay, you wanted six instances, 152 00:06:29,920 --> 00:06:31,580 but I can see you have zero right now, 153 00:06:31,580 --> 00:06:33,960 I'm going to launch six instances for you. 154 00:06:33,960 --> 00:06:36,060 So again, the right order to cancel 155 00:06:36,060 --> 00:06:38,220 and to terminate spot instances is 156 00:06:38,220 --> 00:06:40,510 to first cancel the spot request to make sure 157 00:06:40,510 --> 00:06:44,010 that no new instances will be launched by AWS. 158 00:06:44,010 --> 00:06:47,200 Then you terminate the associated spot instances. 159 00:06:47,200 --> 00:06:48,200 I'm spending a bit of time on this 160 00:06:48,200 --> 00:06:51,290 because this is something that can come up in the exam. 161 00:06:51,290 --> 00:06:53,100 Okay, next spot fleets. 162 00:06:53,100 --> 00:06:55,780 So this is the ultimate way to save money. 163 00:06:55,780 --> 00:06:58,680 Spot fleets is going to be a way to define for you 164 00:06:58,680 --> 00:07:02,990 to get a set of spot instances, and optionally, 165 00:07:02,990 --> 00:07:05,650 On-Demand instances of what's called a fleet. 166 00:07:05,650 --> 00:07:08,380 And the spot fleet will try its best 167 00:07:08,380 --> 00:07:09,940 to meet the target capacity 168 00:07:09,940 --> 00:07:12,090 with the price constraints you defined. 169 00:07:12,090 --> 00:07:15,060 So it will launch from possible launch pools. 170 00:07:15,060 --> 00:07:17,330 It can have different instance types, 171 00:07:17,330 --> 00:07:20,260 different OS and availability zones. 172 00:07:20,260 --> 00:07:22,093 You're going to define multiple launch pools, 173 00:07:22,093 --> 00:07:24,210 a multiple instance size, multiple everything, 174 00:07:24,210 --> 00:07:27,353 and then the fleet will choose the best 175 00:07:27,353 --> 00:07:31,240 and most appropriate launch pool for you. 176 00:07:31,240 --> 00:07:34,490 When your spot fleet either reaches your budget 177 00:07:34,490 --> 00:07:36,470 or reaches the capacity you wanted, 178 00:07:36,470 --> 00:07:39,170 then it will stop launching instances. 179 00:07:39,170 --> 00:07:41,330 And so you define a strategy 180 00:07:41,330 --> 00:07:44,110 to allocate spot instances in your spot fleet. 181 00:07:44,110 --> 00:07:45,350 That's something to remember. 182 00:07:45,350 --> 00:07:47,500 The first one is lowest price, 183 00:07:47,500 --> 00:07:49,350 and that's going to be the most popular one 184 00:07:49,350 --> 00:07:50,570 going into the exam, 185 00:07:50,570 --> 00:07:54,420 which is that the spot fleets will launch instances 186 00:07:54,420 --> 00:07:57,320 from the pool that has the lowest price 187 00:07:57,320 --> 00:08:00,080 and that gives you a lot of cost optimization. 188 00:08:00,080 --> 00:08:02,640 This is a great option if you have a very short workload. 189 00:08:02,640 --> 00:08:05,470 It can also have a diversified way 190 00:08:05,470 --> 00:08:08,266 to launch your spot instances, in which case, 191 00:08:08,266 --> 00:08:11,680 the spot instances will be distributed 192 00:08:11,680 --> 00:08:14,730 across all the pools that you have defined from before, 193 00:08:14,730 --> 00:08:17,170 which is great for availability and long workloads 194 00:08:17,170 --> 00:08:18,910 because if one pool goes away, 195 00:08:18,910 --> 00:08:21,240 then your other pools are still active. 196 00:08:21,240 --> 00:08:23,730 And then finally, you have capacityOptimized 197 00:08:23,730 --> 00:08:26,980 to launch with the optimal capacity based on the number 198 00:08:26,980 --> 00:08:30,400 of instances and choose the right pool for that. 199 00:08:30,400 --> 00:08:32,610 The idea is that spot fleets can be complicated, 200 00:08:32,610 --> 00:08:35,610 but what you have to remember is that using the spot fleet, 201 00:08:35,610 --> 00:08:37,590 you're able to define multiple launch pools, 202 00:08:37,590 --> 00:08:38,570 multiple instance types 203 00:08:38,570 --> 00:08:40,429 until you're just interesting in real power. 204 00:08:40,429 --> 00:08:43,620 And then if you use the lowestPrice discounts, 205 00:08:43,620 --> 00:08:46,170 or the lowest price rate strategy for spot fleets, 206 00:08:46,170 --> 00:08:48,760 then the spot fleets will automatically request 207 00:08:48,760 --> 00:08:52,150 the spot instances with the lowest price for us. 208 00:08:52,150 --> 00:08:55,180 So spot fleet gives us an extra saving 209 00:08:55,180 --> 00:08:57,680 based on spot instances, because it's smart enough 210 00:08:57,680 --> 00:08:59,990 to choose the right spot instance pools 211 00:08:59,990 --> 00:09:03,090 to allow us to get the maximum amount of savings. 212 00:09:03,090 --> 00:09:05,370 This is the benefit of spot fleets. 213 00:09:05,370 --> 00:09:07,410 Again, you need to understand the difference here 214 00:09:07,410 --> 00:09:10,790 of doing a very simple spot instance request 215 00:09:10,790 --> 00:09:12,840 or you know exactly the type of instance you want 216 00:09:12,840 --> 00:09:15,810 and the AZ you want, to using the spot fleet and saying, 217 00:09:15,810 --> 00:09:17,330 okay, you can choose all these instance types 218 00:09:17,330 --> 00:09:19,990 and all these AZ and what I need from you 219 00:09:19,990 --> 00:09:23,270 is to give me the lowest price, for example. 220 00:09:23,270 --> 00:09:25,840 That's hopefully is giving you a lot of insights 221 00:09:25,840 --> 00:09:28,560 into how spot fleets and spot instances are different. 222 00:09:28,560 --> 00:09:29,560 I hope you like this lecture 223 00:09:29,560 --> 00:09:31,510 and I will see you in the next lecture.