1 00:00:00,100 --> 00:00:01,190 ‫Now, let's talk about how 2 00:00:01,190 --> 00:00:02,660 ‫our lambda function integrates 3 00:00:02,660 --> 00:00:04,650 ‫with an Application Balancer. 4 00:00:04,650 --> 00:00:06,270 ‫So, for now, lambda functions, 5 00:00:06,270 --> 00:00:09,760 ‫they can be either invoked with CLI or the SDK, 6 00:00:09,760 --> 00:00:12,800 ‫But sometimes if you want to expose them to the internet, 7 00:00:12,800 --> 00:00:14,930 ‫you want to allow people to use them 8 00:00:14,930 --> 00:00:17,430 ‫through an HTTP or an HTTPS endpoint. 9 00:00:17,430 --> 00:00:19,960 ‫And so as such, you have two ways for doing this. 10 00:00:19,960 --> 00:00:22,150 ‫The first way is to use an Application Balancer, 11 00:00:22,150 --> 00:00:23,560 ‫or an API Gateway, as we'll see 12 00:00:23,560 --> 00:00:26,370 ‫in the next lecture, in the next section. 13 00:00:26,370 --> 00:00:28,700 ‫So in this lecture, we'll be focusing on the ALB. 14 00:00:28,700 --> 00:00:30,030 ‫And then for it to work, 15 00:00:30,030 --> 00:00:33,540 ‫you need to register the lambda function in a target group. 16 00:00:33,540 --> 00:00:36,560 ‫So your clients will be invoking and sending requests 17 00:00:36,560 --> 00:00:39,820 ‫in the form of HTTP or HTTPS to your ALB, 18 00:00:39,820 --> 00:00:42,320 ‫which will be invoking synchronously 19 00:00:42,320 --> 00:00:44,670 ‫your lambda function in a target group, 20 00:00:44,670 --> 00:00:46,540 ‫because synchronously, because we're waiting 21 00:00:46,540 --> 00:00:47,700 ‫for the lambda function to get back 22 00:00:47,700 --> 00:00:49,150 ‫to the application balancer, 23 00:00:49,150 --> 00:00:52,420 ‫which will in turn, return a response to the client. 24 00:00:52,420 --> 00:00:53,297 ‫So the question is, 25 00:00:53,297 --> 00:00:55,970 ‫"How does the load balancer convert 26 00:00:55,970 --> 00:00:59,200 ‫an HTTP request into a lambda invocation?" 27 00:00:59,200 --> 00:01:01,180 ‫So from the ALB to lambda, 28 00:01:01,180 --> 00:01:04,760 ‫the HTTP gets transformed into a JSON document. 29 00:01:04,760 --> 00:01:06,340 ‫So this is an example request payload 30 00:01:06,340 --> 00:01:08,450 ‫for your lambda function, and as we can see, 31 00:01:08,450 --> 00:01:10,510 ‫in the top of the document, 32 00:01:10,510 --> 00:01:11,970 ‫there is the ELB information, 33 00:01:11,970 --> 00:01:15,860 ‫so which ELB invokes and what is the target group. 34 00:01:15,860 --> 00:01:17,970 ‫Then we get some information around the HTTP method, 35 00:01:17,970 --> 00:01:21,320 ‫so it was a GET and the path was /lambda. 36 00:01:21,320 --> 00:01:24,640 ‫We get the query string parameters as key/value pairs, 37 00:01:24,640 --> 00:01:28,330 ‫so each query string will be appearing in the JSON document. 38 00:01:28,330 --> 00:01:30,610 ‫We'll get the headers as key/value pairs, 39 00:01:30,610 --> 00:01:33,050 ‫and we'll get the body for POST, PUT, 40 00:01:34,016 --> 00:01:36,930 ‫and the value is base 64 encoded, 41 00:01:36,930 --> 00:01:38,550 ‫so do you need to decode it or not. 42 00:01:38,550 --> 00:01:40,950 ‫And so, for this information, 43 00:01:40,950 --> 00:01:44,830 ‫there is a translation of the entire HTTP request to JSON. 44 00:01:44,830 --> 00:01:47,670 ‫So we should remember is that the query string parameters, 45 00:01:47,670 --> 00:01:50,290 ‫the headers, and the body, they're all converted. 46 00:01:50,290 --> 00:01:51,750 ‫And for the query string parameters 47 00:01:51,750 --> 00:01:54,310 ‫and the headers, they are key/value pairs. 48 00:01:54,310 --> 00:01:55,230 ‫So, similarly, 49 00:01:55,230 --> 00:01:58,650 ‫our lambda function should return something, a JSON document 50 00:01:58,650 --> 00:02:01,820 ‫and the ALB will convert this back into HTTP. 51 00:02:01,820 --> 00:02:03,150 ‫So if we look at the response 52 00:02:03,150 --> 00:02:05,190 ‫from the lambda function, it's very simple. 53 00:02:05,190 --> 00:02:08,370 ‫It needs to include a status code and a description, 54 00:02:08,370 --> 00:02:11,400 ‫as well as the response headers as key/value pairs, 55 00:02:11,400 --> 00:02:14,860 ‫and finally the body of the response and the flag, 56 00:02:14,860 --> 00:02:18,220 ‫whether or not it is base 64 encoded. 57 00:02:18,220 --> 00:02:19,790 ‫Okay, so we know how the lambda, 58 00:02:19,790 --> 00:02:23,230 ‫the ALB converts from HTTP to JSON and back, 59 00:02:23,230 --> 00:02:24,970 ‫but now let's talk about one last feature 60 00:02:24,970 --> 00:02:27,310 ‫of the ALB integration that can come up in the exam, 61 00:02:27,310 --> 00:02:29,130 ‫which is the multi-header values. 62 00:02:29,130 --> 00:02:33,100 ‫So, if we have our client talking to our ALB, 63 00:02:33,100 --> 00:02:34,800 ‫we can enable an ALB setting, 64 00:02:34,800 --> 00:02:37,140 ‫which is to have multi-header values. 65 00:02:37,140 --> 00:02:37,973 ‫What does that mean? 66 00:02:37,973 --> 00:02:39,340 ‫That means that if we pass in 67 00:02:39,340 --> 00:02:40,700 ‫multiple headers with the same value 68 00:02:40,700 --> 00:02:42,480 ‫or query strings with the same value, 69 00:02:42,480 --> 00:02:44,260 ‫so I can represent query strings easily. 70 00:02:44,260 --> 00:02:45,750 ‫So, in this example, 71 00:02:45,750 --> 00:02:50,240 ‫name=foo&name=bar have the same name, but different values. 72 00:02:50,240 --> 00:02:54,030 ‫Then we can enable the setting and both the headers 73 00:02:54,030 --> 00:02:56,210 ‫and the query string parameters will be converted 74 00:02:56,210 --> 00:02:58,950 ‫as an array into the lambda function. 75 00:02:58,950 --> 00:03:01,730 ‫So that means that when my ALB invokes my lambda function 76 00:03:01,730 --> 00:03:03,680 ‫for the queryStringParameters JSON, 77 00:03:03,680 --> 00:03:06,700 ‫what I will see is name, and instead of one value, 78 00:03:06,700 --> 00:03:08,140 ‫I will see an array of values. 79 00:03:08,140 --> 00:03:09,460 ‫So, foo and bar. 80 00:03:09,460 --> 00:03:11,640 ‫So both values are converted 81 00:03:11,640 --> 00:03:13,540 ‫and that's something the exam does test you on. 82 00:03:13,540 --> 00:03:14,437 ‫It will ask you, 83 00:03:14,437 --> 00:03:16,310 ‫"How do we support multi-header values?" 84 00:03:16,310 --> 00:03:18,390 ‫It's just a setting on the ALB 85 00:03:18,390 --> 00:03:19,720 ‫and this is what it does. 86 00:03:19,720 --> 00:03:20,553 ‫So in the next lecture, 87 00:03:20,553 --> 00:03:22,810 ‫we'll go ahead and practice the ALB and lambda.