1 00:00:00,540 --> 00:00:02,160 ‫Okay, so let's go ahead 2 00:00:02,160 --> 00:00:05,400 ‫and create a Lambda function that is going to be integrated 3 00:00:05,400 --> 00:00:06,900 ‫with our load balancer. 4 00:00:06,900 --> 00:00:08,460 ‫So for this I'm going to create a Lambda function, 5 00:00:08,460 --> 00:00:10,650 ‫call it Lambda-alb, 6 00:00:10,650 --> 00:00:14,250 ‫and for the runtime I will choose Python version three, 7 00:00:14,250 --> 00:00:16,833 ‫and then I will just click on create function. 8 00:00:18,150 --> 00:00:18,983 ‫Now, in the meantime, 9 00:00:18,983 --> 00:00:20,820 ‫I also have to create a load balancer 10 00:00:20,820 --> 00:00:22,560 ‫for my Lambda function. 11 00:00:22,560 --> 00:00:24,630 ‫So therefore, let's go ahead and create a load balancer, 12 00:00:24,630 --> 00:00:27,183 ‫and we'll choose an application, load balancer. 13 00:00:28,140 --> 00:00:29,340 ‫Let's go through the create process, 14 00:00:29,340 --> 00:00:33,333 ‫so I'll call this one demo-Lambda-alb. 15 00:00:34,710 --> 00:00:37,650 ‫It's internet facing of IPv4. 16 00:00:37,650 --> 00:00:41,433 ‫I'm going to deploy this in three availability zones, 17 00:00:43,740 --> 00:00:45,000 ‫and for security group, 18 00:00:45,000 --> 00:00:47,640 ‫I'm going to just create a new security group 19 00:00:47,640 --> 00:00:48,780 ‫for my ALB, 20 00:00:48,780 --> 00:00:50,230 ‫just to make sure I have one. 21 00:00:51,623 --> 00:00:53,206 ‫So DemoLambdaALBSG. 22 00:00:56,790 --> 00:00:58,290 ‫And for the inbound rule, 23 00:00:58,290 --> 00:01:00,993 ‫I'm going to just allow HTTP, 24 00:01:01,920 --> 00:01:04,443 ‫coming from anywhere IPv4. 25 00:01:05,310 --> 00:01:07,053 ‫Let's create the security group. 26 00:01:07,920 --> 00:01:08,753 ‫Perfect, 27 00:01:08,753 --> 00:01:11,610 ‫and let's assign this security group 28 00:01:11,610 --> 00:01:13,443 ‫into our load balancer. 29 00:01:14,760 --> 00:01:17,760 ‫Okay, next we allow to listen 30 00:01:17,760 --> 00:01:20,850 ‫on port 80 for the HTTP protocol. 31 00:01:20,850 --> 00:01:23,814 ‫And the default action is to send to a target group, 32 00:01:23,814 --> 00:01:26,220 ‫so we have to create a target group 33 00:01:26,220 --> 00:01:28,620 ‫that will contain our Lambda function. 34 00:01:28,620 --> 00:01:30,570 ‫So therefore, when we create this target group, 35 00:01:30,570 --> 00:01:32,853 ‫we're going to choose Lambda function, 36 00:01:34,525 --> 00:01:36,900 ‫and this one will be demo-tg-lambda. 37 00:01:36,900 --> 00:01:38,340 ‫And this target group is accessible 38 00:01:38,340 --> 00:01:40,203 ‫to application load balancers only. 39 00:01:41,100 --> 00:01:44,190 ‫So let's go ahead and click on next. 40 00:01:44,190 --> 00:01:46,770 ‫Next, we need to choose a Lambda function, 41 00:01:46,770 --> 00:01:50,340 ‫so we'll choose the Lambda-alb we just created, 42 00:01:50,340 --> 00:01:52,863 ‫and we click on create target group, 43 00:01:55,560 --> 00:01:56,640 ‫which has now been created, 44 00:01:56,640 --> 00:01:58,380 ‫so if I refresh this, 45 00:01:58,380 --> 00:02:01,852 ‫I can choose demo-tg-lambda as my target. 46 00:02:01,852 --> 00:02:03,090 ‫So this is good. 47 00:02:03,090 --> 00:02:05,514 ‫Now we can go ahead and create our load balancer 48 00:02:05,514 --> 00:02:07,533 ‫and view it. 49 00:02:09,330 --> 00:02:11,730 ‫So while this is getting provisions, 50 00:02:11,730 --> 00:02:14,340 ‫let's have a look at our Lambda function. 51 00:02:14,340 --> 00:02:15,960 ‫So if you go into Lambda right now, 52 00:02:15,960 --> 00:02:17,010 ‫we have a Lambda function with 53 00:02:17,010 --> 00:02:19,380 ‫some very simple code, 54 00:02:19,380 --> 00:02:21,870 ‫and that just returns the status 200 55 00:02:21,870 --> 00:02:22,980 ‫and says hello from Lambda. 56 00:02:22,980 --> 00:02:25,500 ‫So if we go ahead and test this function 57 00:02:25,500 --> 00:02:28,833 ‫with a new test event and save this, 58 00:02:31,980 --> 00:02:33,780 ‫test event, we just have to name it. 59 00:02:36,210 --> 00:02:38,190 ‫Okay, and we go ahead and test this one. 60 00:02:38,190 --> 00:02:42,000 ‫We can see, "Hello from Lambda" with a status code of 200. 61 00:02:42,000 --> 00:02:42,833 ‫So this is good. 62 00:02:42,833 --> 00:02:45,120 ‫Now let's add a little bit of logging. 63 00:02:45,120 --> 00:02:48,240 ‫So here I will just have console dot log, 64 00:02:48,240 --> 00:02:49,620 ‫and then event. 65 00:02:49,620 --> 00:02:54,480 ‫This is to see what event is going to be passed here. 66 00:02:54,480 --> 00:02:57,660 ‫So if I deploy first the changes, 67 00:02:57,660 --> 00:03:00,780 ‫and then test my function as we can see, 68 00:03:00,780 --> 00:03:02,790 ‫oh, and it's not console dot log of course, 69 00:03:02,790 --> 00:03:04,110 ‫it is going to be print. 70 00:03:04,110 --> 00:03:07,950 ‫So print, and then event. 71 00:03:07,950 --> 00:03:11,820 ‫Let's deploy this function and test it, 72 00:03:11,820 --> 00:03:13,770 ‫and now we get a hello from Lambda, 73 00:03:13,770 --> 00:03:15,540 ‫and here we can see 74 00:03:15,540 --> 00:03:17,760 ‫that the event that was passed is being printed 75 00:03:17,760 --> 00:03:18,720 ‫to the console. 76 00:03:18,720 --> 00:03:19,553 ‫And this will be helpful, 77 00:03:19,553 --> 00:03:22,860 ‫when we want to see the event pass to another function 78 00:03:22,860 --> 00:03:24,990 ‫from the load balancer. 79 00:03:24,990 --> 00:03:28,740 ‫So back in the load balancer, let's refresh this, 80 00:03:28,740 --> 00:03:31,290 ‫and here is the DNS name, and the status is active. 81 00:03:31,290 --> 00:03:33,843 ‫So if I go and open a new tab and press enter, 82 00:03:34,680 --> 00:03:38,400 ‫as you can see, this gave me a response of type DNS, 83 00:03:38,400 --> 00:03:43,400 ‫so if I just go and open this file in a text editor, 84 00:03:43,890 --> 00:03:46,860 ‫what I get out of it is hello from Lambda. 85 00:03:46,860 --> 00:03:49,890 ‫So if we go back into our load balancer, 86 00:03:49,890 --> 00:03:53,370 ‫this is because while we respond with hello from Lambda, 87 00:03:53,370 --> 00:03:55,650 ‫but what we would like to have this response 88 00:03:55,650 --> 00:03:56,910 ‫not to be downloaded, 89 00:03:56,910 --> 00:04:00,060 ‫but instead to be displayed in my web browser. 90 00:04:00,060 --> 00:04:05,060 ‫So to do so, let's do example Lambda load balancer AWS, 91 00:04:05,670 --> 00:04:07,440 ‫and we have a documentation called 92 00:04:07,440 --> 00:04:10,770 ‫using Lambda with the application balancer. 93 00:04:10,770 --> 00:04:12,330 ‫And this is what the request looks like, 94 00:04:12,330 --> 00:04:14,340 ‫we'll have a look at it in a second, 95 00:04:14,340 --> 00:04:18,030 ‫but this is what the documents for the response should be. 96 00:04:18,030 --> 00:04:20,277 ‫So we need to respond with something like this, 97 00:04:20,277 --> 00:04:23,040 ‫and this is going to be interpreted correctly 98 00:04:23,040 --> 00:04:24,510 ‫by my load balancer, 99 00:04:24,510 --> 00:04:27,750 ‫and this will make sure we receive a proper HTTP response. 100 00:04:27,750 --> 00:04:30,540 ‫So back in here, I'm going to, instead of returning this, 101 00:04:30,540 --> 00:04:34,140 ‫I'm going to return the document I just found online. 102 00:04:34,140 --> 00:04:36,390 ‫So we deploy these changes, 103 00:04:36,390 --> 00:04:39,060 ‫and test our function 104 00:04:39,060 --> 00:04:41,880 ‫by going directly in the load balancer, 105 00:04:41,880 --> 00:04:43,260 ‫refreshing this page, 106 00:04:43,260 --> 00:04:46,290 ‫and as you can see, I get the hello from Lambda, 107 00:04:46,290 --> 00:04:50,670 ‫which was coming from my body text right here. 108 00:04:50,670 --> 00:04:52,140 ‫So this first part is working, 109 00:04:52,140 --> 00:04:56,010 ‫and if you want to do some examples, 110 00:04:56,010 --> 00:04:57,577 ‫you can for example, change your text. 111 00:04:57,577 --> 00:05:01,020 ‫The magic happens because in the headers we set 112 00:05:01,020 --> 00:05:03,690 ‫the content type to be text html, 113 00:05:03,690 --> 00:05:06,510 ‫and therefore this is deployed in our web browser, 114 00:05:06,510 --> 00:05:10,140 ‫and we can change the status code here to be 200, and so on. 115 00:05:10,140 --> 00:05:13,710 ‫So this is how a Lambda function is properly 116 00:05:13,710 --> 00:05:16,440 ‫sent in data into our ALB, 117 00:05:16,440 --> 00:05:19,435 ‫and then our ALB showing the data directly 118 00:05:19,435 --> 00:05:21,903 ‫from the web browser itself. 119 00:05:24,120 --> 00:05:25,380 ‫The other thing we can do 120 00:05:25,380 --> 00:05:29,290 ‫is to go directly into the monitoring tab of our log 121 00:05:31,063 --> 00:05:32,263 ‫for our Lambda function, 122 00:05:33,119 --> 00:05:34,560 ‫and we can have a look at the recent request, 123 00:05:34,560 --> 00:05:38,040 ‫so we can have a look at what is the type of event passed 124 00:05:38,040 --> 00:05:41,160 ‫from the ALB into the Lambda function. 125 00:05:41,160 --> 00:05:44,730 ‫So let me click on this one right here, this log stream. 126 00:05:44,730 --> 00:05:47,580 ‫As we can see in this log stream, 127 00:05:47,580 --> 00:05:49,710 ‫we have this query right here, 128 00:05:49,710 --> 00:05:51,120 ‫this JSN document, 129 00:05:51,120 --> 00:05:53,610 ‫which represents the type of information 130 00:05:53,610 --> 00:05:57,300 ‫that the ALB will invoke our Lambda function with. 131 00:05:57,300 --> 00:05:59,070 ‫This is the same thing that you will find 132 00:05:59,070 --> 00:06:01,080 ‫in the documentation around here. 133 00:06:01,080 --> 00:06:05,100 ‫This is the request event of the ALB to the Lambda function. 134 00:06:05,100 --> 00:06:07,671 ‫And this is how you can, for example, look at the path, 135 00:06:07,671 --> 00:06:09,210 ‫the HTTP method, 136 00:06:09,210 --> 00:06:11,220 ‫the query string parameters, and so on, 137 00:06:11,220 --> 00:06:13,950 ‫as well as the headers. 138 00:06:13,950 --> 00:06:14,940 ‫So talking about the headers, 139 00:06:14,940 --> 00:06:16,020 ‫as you can see here, 140 00:06:16,020 --> 00:06:20,123 ‫I have a header where each header has a unique value, 141 00:06:20,123 --> 00:06:23,501 ‫but if you go into your target group right here 142 00:06:23,501 --> 00:06:25,560 ‫and you go to attributes, 143 00:06:25,560 --> 00:06:28,860 ‫you can configure multi-value header, 144 00:06:28,860 --> 00:06:31,860 ‫and if you allow multi-value header, 145 00:06:31,860 --> 00:06:34,080 ‫this allows the load balancer 146 00:06:34,080 --> 00:06:37,110 ‫to have multiple headers on here. 147 00:06:37,110 --> 00:06:39,780 ‫So here, for example, you could have multiple values 148 00:06:39,780 --> 00:06:42,990 ‫for the accept, or the host, or the user agent, and so on. 149 00:06:42,990 --> 00:06:45,390 ‫So this is a feature you would just have to know 150 00:06:45,390 --> 00:06:46,800 ‫for the load balancer, 151 00:06:46,800 --> 00:06:49,260 ‫and you would need to modify the Lambda code 152 00:06:49,260 --> 00:06:51,810 ‫a little bit as well for this to work, 153 00:06:51,810 --> 00:06:55,710 ‫because right here the headers are sent just as key values, 154 00:06:55,710 --> 00:06:57,660 ‫but if you enable multi-value header, 155 00:06:57,660 --> 00:06:59,730 ‫you need to change your response a little bit. 156 00:06:59,730 --> 00:07:02,160 ‫But I just wanted to show you this option, 157 00:07:02,160 --> 00:07:05,700 ‫so you know it's a target group type of option. 158 00:07:05,700 --> 00:07:07,750 ‫So let me, I should disable it right now. 159 00:07:09,660 --> 00:07:10,560 ‫And that'll be it. 160 00:07:12,270 --> 00:07:13,440 ‫And I also want to show you 161 00:07:13,440 --> 00:07:14,940 ‫how this Lambda function was invoked. 162 00:07:14,940 --> 00:07:18,030 ‫So as you can see, if you go under configuration, 163 00:07:18,030 --> 00:07:18,990 ‫you see for the triggers 164 00:07:18,990 --> 00:07:22,050 ‫that we have our application load balancer of course. 165 00:07:22,050 --> 00:07:24,780 ‫But if we look at the permissions of our function, 166 00:07:24,780 --> 00:07:26,880 ‫as you can see right here, 167 00:07:26,880 --> 00:07:31,140 ‫we have a resource based policy statement, 168 00:07:31,140 --> 00:07:32,790 ‫and this policy right here, 169 00:07:32,790 --> 00:07:34,920 ‫if we click on the statement ID, 170 00:07:34,920 --> 00:07:39,920 ‫allows my load balancer to invoke my Lambda function, 171 00:07:40,080 --> 00:07:41,730 ‫which is great and what we want. 172 00:07:41,730 --> 00:07:46,177 ‫So this is the condensed form, but you can do view policy, 173 00:07:46,177 --> 00:07:49,050 ‫and do the actual entire JSN document to do so. 174 00:07:49,050 --> 00:07:51,120 ‫So as we can see, this allows, 175 00:07:51,120 --> 00:07:53,730 ‫thanks to the resource-based policy on our Lambda function, 176 00:07:53,730 --> 00:07:56,433 ‫this allows our ALB to invoke our Lambda function. 177 00:07:57,450 --> 00:07:58,920 ‫So that's it for this lecture. 178 00:07:58,920 --> 00:08:00,690 ‫Now all you need to do to clean up 179 00:08:00,690 --> 00:08:03,180 ‫is just to delete the load balancer, 180 00:08:03,180 --> 00:08:04,860 ‫and you'll be good to go. 181 00:08:04,860 --> 00:08:05,730 ‫Okay, that's it. 182 00:08:05,730 --> 00:08:08,730 ‫I hope you liked it, and I will see you in the next lecture.