1 00:00:00,490 --> 00:00:01,840 ‫So for this hands-on, let's go 2 00:00:01,840 --> 00:00:05,150 ‫into the lambda-config-demo and we're going to have a look 3 00:00:05,150 --> 00:00:08,270 ‫at the different options that impact our Lambda function. 4 00:00:08,270 --> 00:00:11,470 ‫So if we go into the configuration, our Lambda function, 5 00:00:11,470 --> 00:00:14,230 ‫the first thing we see is that under general configuration 6 00:00:14,230 --> 00:00:15,630 ‫we can edit the basic settings. 7 00:00:15,630 --> 00:00:17,790 ‫So we've seen them before, but now let's have a look again, 8 00:00:17,790 --> 00:00:20,750 ‫so the memory can be anywhere between one and 28 gigabytes 9 00:00:20,750 --> 00:00:25,240 ‫to 10,240 megabytes in total, and so the more memory we add, 10 00:00:25,240 --> 00:00:28,310 ‫the more CPU we're going to have. 11 00:00:28,310 --> 00:00:29,890 ‫So as you can see here, 12 00:00:29,890 --> 00:00:32,100 ‫we can have as much memory as we want, okay. 13 00:00:32,100 --> 00:00:35,710 ‫And the CPU is proportionally located to the memory. 14 00:00:35,710 --> 00:00:38,550 ‫And the idea is that if you have maybe 10,200, 15 00:00:38,550 --> 00:00:40,000 ‫so the maximum amount of memory, 16 00:00:40,000 --> 00:00:41,820 ‫then you're going to pay a lot more 17 00:00:41,820 --> 00:00:44,770 ‫for your Lambda function then if you had the minimum. 18 00:00:44,770 --> 00:00:45,750 ‫So the idea for you is 19 00:00:45,750 --> 00:00:49,330 ‫to really monitor the Lambda function memory usage, 20 00:00:49,330 --> 00:00:51,420 ‫and make sure you set your memory accordingly 21 00:00:51,420 --> 00:00:53,660 ‫so that you have enough memory to perform what you need 22 00:00:53,660 --> 00:00:56,250 ‫but not too much so that you don't get over billed. 23 00:00:56,250 --> 00:00:58,000 ‫And the very popular exam question is 24 00:00:58,000 --> 00:01:01,480 ‫that if you need a faster CPU or more CPU cores 25 00:01:01,480 --> 00:01:03,360 ‫for your Lambda function, the setting 26 00:01:03,360 --> 00:01:05,370 ‫to do it is by modifying the memory. 27 00:01:05,370 --> 00:01:08,170 ‫There is no way to change the CPU independently 28 00:01:08,170 --> 00:01:10,580 ‫from the memory in your Lambda function today. 29 00:01:10,580 --> 00:01:12,170 ‫Okay, in terms of timeout, 30 00:01:12,170 --> 00:01:13,930 ‫this is how long your Lambda function will run 31 00:01:13,930 --> 00:01:15,960 ‫before throwing an error and run out. 32 00:01:15,960 --> 00:01:17,030 ‫It's set to three seconds 33 00:01:17,030 --> 00:01:19,090 ‫and we're going to have a play with it in a second, 34 00:01:19,090 --> 00:01:22,040 ‫so let's leave the timeout to three seconds 35 00:01:22,040 --> 00:01:26,910 ‫and let's have a Lambda function do some work. 36 00:01:26,910 --> 00:01:29,170 ‫So let's keep this in the function, 37 00:01:29,170 --> 00:01:34,170 ‫and I'm going to import the time library at the very top, 38 00:01:34,360 --> 00:01:36,110 ‫and then we're going to time.sleep2 39 00:01:37,130 --> 00:01:38,700 ‫and this is to simulate some work. 40 00:01:38,700 --> 00:01:41,190 ‫So by sleeping the Lambda function for two seconds, 41 00:01:41,190 --> 00:01:43,220 ‫we are saying that the Lambda function does some work 42 00:01:43,220 --> 00:01:46,150 ‫and after two seconds it's going to return the results. 43 00:01:46,150 --> 00:01:49,630 ‫So let's deploy this and test this. 44 00:01:49,630 --> 00:01:53,080 ‫As we can see, after two seconds we get the response prod, 45 00:01:53,080 --> 00:01:57,740 ‫so the duration was 2000 milliseconds, approximately. 46 00:01:57,740 --> 00:02:00,500 ‫But what happens if we make the Lambda function 47 00:02:00,500 --> 00:02:01,390 ‫sleep five seconds? 48 00:02:01,390 --> 00:02:04,050 ‫So now the Lambda function is doing a lot of work, 49 00:02:04,050 --> 00:02:06,360 ‫maybe more than what we expected from before. 50 00:02:06,360 --> 00:02:09,060 ‫So let's deploy the changes and click on test. 51 00:02:09,060 --> 00:02:10,030 ‫Now what's going to happen is 52 00:02:10,030 --> 00:02:11,720 ‫that the Lambda function will fail. 53 00:02:11,720 --> 00:02:13,530 ‫Why? Because it will timeout. 54 00:02:13,530 --> 00:02:14,990 ‫So we got an error message here saying, 55 00:02:14,990 --> 00:02:18,590 ‫Hey the tasks timed out after three seconds and this is 56 00:02:18,590 --> 00:02:21,080 ‫because, well under the configuration tabs, 57 00:02:21,080 --> 00:02:24,140 ‫our Lambda function right here, we did decide 58 00:02:24,140 --> 00:02:25,990 ‫for the timeout to be three seconds. 59 00:02:25,990 --> 00:02:29,110 ‫But now if we change the timeout to be six seconds, okay, 60 00:02:29,110 --> 00:02:31,160 ‫because we know that our Lambda function is doing more work 61 00:02:31,160 --> 00:02:34,560 ‫than expected and then we test our Lambda function again. 62 00:02:34,560 --> 00:02:37,550 ‫Well, this time the execution results should be successful 63 00:02:37,550 --> 00:02:39,600 ‫after five seconds because we don't hit the timeout, 64 00:02:39,600 --> 00:02:42,240 ‫and then yes, we get the response properly 65 00:02:42,240 --> 00:02:45,770 ‫and the duration is 5,000 milliseconds. 66 00:02:45,770 --> 00:02:47,800 ‫So we really see the impact of timeout 67 00:02:47,800 --> 00:02:49,710 ‫on the function itself. 68 00:02:49,710 --> 00:02:51,750 ‫And so something you may ask me is, "Hey, Stefan, 69 00:02:51,750 --> 00:02:53,540 ‫why don't we just always set the timeout to 70 00:02:53,540 --> 00:02:56,700 ‫be something very large, like the 15 minutes or 10 minutes?" 71 00:02:56,700 --> 00:02:59,340 ‫Well, if you do so and imagine your Lambda function 72 00:02:59,340 --> 00:03:02,650 ‫gets stuck and you know that on average Lambda function 73 00:03:02,650 --> 00:03:05,110 ‫takes no more than 10 seconds to execute, 74 00:03:05,110 --> 00:03:08,540 ‫then you may not go fast enough into an error case 75 00:03:08,540 --> 00:03:10,080 ‫and therefore, maybe you retry something 76 00:03:10,080 --> 00:03:11,680 ‫or get your function unstuck. 77 00:03:11,680 --> 00:03:13,780 ‫So it's up to you to set a timeout 78 00:03:13,780 --> 00:03:16,390 ‫to something that you think is reasonable for a function 79 00:03:16,390 --> 00:03:18,690 ‫because you want it to fail when something 80 00:03:18,690 --> 00:03:21,750 ‫within your function goes wrong and analyze that error case, 81 00:03:21,750 --> 00:03:24,480 ‫instead of just waiting 15 minutes for your function 82 00:03:24,480 --> 00:03:27,220 ‫to timeout and maybe get an error, so it's up to you. 83 00:03:27,220 --> 00:03:29,410 ‫It depends on use case and what your function does 84 00:03:29,410 --> 00:03:31,880 ‫but set the timeout accordingly. 85 00:03:31,880 --> 00:03:34,880 ‫Last thing to optimize your Lambda function performance 86 00:03:34,880 --> 00:03:39,070 ‫is around where you set the initialization of your function. 87 00:03:39,070 --> 00:03:43,460 ‫So if you're connecting to a DB, connect to DB function. 88 00:03:43,460 --> 00:03:46,520 ‫You want it to have outside of your function handler. 89 00:03:46,520 --> 00:03:48,900 ‫Well, let's say for example that you put it inside, 90 00:03:48,900 --> 00:03:52,730 ‫okay, and that your function connect to DB 91 00:03:52,730 --> 00:03:55,243 ‫right now does time.sleep3 92 00:03:57,880 --> 00:04:01,980 ‫Okay, so connecting to the DB takes about three seconds. 93 00:04:01,980 --> 00:04:04,550 ‫If we have this function right here 94 00:04:04,550 --> 00:04:07,450 ‫where we connect to the database within the Lambda handler, 95 00:04:07,450 --> 00:04:10,870 ‫that means that every time we invoke our function 96 00:04:10,870 --> 00:04:13,840 ‫this function connects to DB is going to be run it's going 97 00:04:13,840 --> 00:04:15,650 ‫to take three seconds because it takes a long time 98 00:04:15,650 --> 00:04:17,820 ‫to connect your database and then is going 99 00:04:17,820 --> 00:04:19,430 ‫to return the results you have. 100 00:04:19,430 --> 00:04:22,480 ‫So if we test it, this is going to take 3 seconds. 101 00:04:22,480 --> 00:04:24,480 ‫So we wait,, one, two, and three. 102 00:04:24,480 --> 00:04:25,313 ‫Okay, great. 103 00:04:25,313 --> 00:04:26,430 ‫We want to run it again. 104 00:04:26,430 --> 00:04:28,360 ‫So one, two, and three. 105 00:04:28,360 --> 00:04:31,070 ‫So as you can see, every time we run our function, 106 00:04:31,070 --> 00:04:33,230 ‫it lasts three seconds because we are connecting 107 00:04:33,230 --> 00:04:36,240 ‫to database every single time, okay? 108 00:04:36,240 --> 00:04:37,980 ‫But the optimization that we saw 109 00:04:37,980 --> 00:04:40,530 ‫and something the exam will ask you is that instead 110 00:04:40,530 --> 00:04:42,080 ‫of doing the connection to the database 111 00:04:42,080 --> 00:04:45,770 ‫within the Lambda handler, you do it outside of it. 112 00:04:45,770 --> 00:04:48,190 ‫So let's deploy this and see the difference. 113 00:04:48,190 --> 00:04:49,860 ‫So now we connect to the database 114 00:04:49,860 --> 00:04:52,950 ‫before doing the Lambda handler. 115 00:04:52,950 --> 00:04:57,370 ‫So we'll test it, one, two, three, this takes a lot of time 116 00:04:57,370 --> 00:05:00,570 ‫and this is not working because we need to.... 117 00:05:00,570 --> 00:05:02,450 ‫Yes, so if you go to the results it says that connect 118 00:05:02,450 --> 00:05:05,870 ‫to DB is not defined so I need to define my function connect 119 00:05:05,870 --> 00:05:07,480 ‫to DB before invoking it. 120 00:05:07,480 --> 00:05:08,950 ‫That makes a lot of sense. 121 00:05:08,950 --> 00:05:12,770 ‫So let's just move this function definition up. 122 00:05:12,770 --> 00:05:15,930 ‫So we're going to deploy the changes, test again. 123 00:05:15,930 --> 00:05:18,060 ‫So one, two, three 124 00:05:18,060 --> 00:05:20,920 ‫and this is how long it takes for my function to initialize. 125 00:05:20,920 --> 00:05:21,970 ‫So as we can see here, 126 00:05:21,970 --> 00:05:24,170 ‫the duration of the function was one millisecond 127 00:05:24,170 --> 00:05:28,510 ‫but the INET duration was 3000 millisecond, 128 00:05:28,510 --> 00:05:29,420 ‫so this is the time it took 129 00:05:29,420 --> 00:05:32,300 ‫for my function to run the very, very first time. 130 00:05:32,300 --> 00:05:34,170 ‫But now if I test my function again, 131 00:05:34,170 --> 00:05:36,250 ‫it takes less than one millisecond 132 00:05:36,250 --> 00:05:39,500 ‫because this part of the code is not run again. 133 00:05:39,500 --> 00:05:41,360 ‫So we can test, test, test, 134 00:05:41,360 --> 00:05:43,360 ‫and now my function is much quicker 135 00:05:43,360 --> 00:05:45,830 ‫because we've done the database initialization again 136 00:05:45,830 --> 00:05:47,380 ‫outside of the function handler. 137 00:05:47,380 --> 00:05:48,730 ‫So hopefully this is a good demo. 138 00:05:48,730 --> 00:05:50,340 ‫Imagine that instead of here, instead of sleeping, 139 00:05:50,340 --> 00:05:51,890 ‫you actually connect to the database 140 00:05:51,890 --> 00:05:53,540 ‫and you get a database object out of it 141 00:05:53,540 --> 00:05:56,590 ‫that you can use within your Lambda handler. 142 00:05:56,590 --> 00:05:57,820 ‫So that's it for this lecture. 143 00:05:57,820 --> 00:05:58,740 ‫I hope you liked it, 144 00:05:58,740 --> 00:06:00,690 ‫and I will see you in the next lecture.