1 00:00:12,019 --> 00:00:16,830 Welcome back to BackSpace Academy. In this lab we're going to be creating a 2 00:00:16,830 --> 00:00:20,400 Nodejs Lambda function using the Lambda 3 00:00:20,400 --> 00:00:25,260 console. Once we've done that we're going to edit that that Lambda function with 4 00:00:25,260 --> 00:00:31,080 our own nodejs application code and then we're going to invoke and test that 5 00:00:31,080 --> 00:00:36,180 Lambda function by sending a request to it and seeing what it responds to and we 6 00:00:36,180 --> 00:00:42,840 can do all that again using the Lambda console. Let's get into it. okay so 7 00:00:42,840 --> 00:00:47,850 starting off in the Lambda console. Again we get there from services and select 8 00:00:47,850 --> 00:00:51,780 Lambda. Not a problem there. I'm just going to open up this toolbar on the 9 00:00:51,780 --> 00:00:58,559 left here and go to functions and then create function. We're going to be 10 00:00:58,559 --> 00:01:02,940 authoring this from scratch. We're going to be using our own code so it will 11 00:01:02,940 --> 00:01:09,240 provide us with just a blank canvas to use we will call the function 12 00:01:09,240 --> 00:01:15,930 backspace - Lambda- lab and we're going to be using the nodejs runtime and we need to 13 00:01:15,930 --> 00:01:20,400 choose or create an execution role. I've got an existing role here you 14 00:01:20,400 --> 00:01:25,200 probably will have this as well called lambda basic execution. If you don't 15 00:01:25,200 --> 00:01:29,759 already have this role then there are instructions in the lab notes on how to 16 00:01:29,759 --> 00:01:36,380 create that role. So that looks fine now and we're going to create that function. 17 00:01:36,500 --> 00:01:43,380 Okay so that has successfully created our backspace - Lambda lab function. 18 00:01:43,380 --> 00:01:48,329 If we scroll down we can see that bare bones code that has been put in 19 00:01:48,329 --> 00:01:53,130 here by AWS. We're not going to use it. We're going to use our own code which 20 00:01:53,130 --> 00:02:02,969 I'm just now going to copy from the lab notes and paste that in there. I'll just 21 00:02:02,969 --> 00:02:07,020 bring this down a little bit so we can see it better. Okay so what we've got 22 00:02:07,020 --> 00:02:12,569 here is that we've got our our handler function or handler module. So we 23 00:02:12,569 --> 00:02:16,020 have an exports and it's going to export this handler 24 00:02:16,020 --> 00:02:20,610 and that name handler can be anything you want. It doesn't have to be called 25 00:02:20,610 --> 00:02:23,970 handler but you need to remember what you call it so you can refer to it later 26 00:02:23,970 --> 00:02:27,660 on. So this handler module is going to be 27 00:02:27,660 --> 00:02:31,980 receiving and something called an event. So it's going to receive it a JSON 28 00:02:31,980 --> 00:02:37,739 object we're calling it event here and inside that will have key one key two and 29 00:02:37,739 --> 00:02:41,760 key three. So we need to, when we send that to it, we need to make sure that 30 00:02:41,760 --> 00:02:46,049 those fields of key one key two and key three are there. So the first thing we're 31 00:02:46,049 --> 00:02:49,410 going to do is create that variable called response and then we're going to 32 00:02:49,410 --> 00:02:54,720 console log out that the function was invoked successfully. Now obviously a 33 00:02:54,720 --> 00:02:58,920 Lambda function doesn't have a screen so it's not going to be a console log 34 00:02:58,920 --> 00:03:03,319 screen that we can view, other than in the the actual management console here. 35 00:03:03,319 --> 00:03:10,830 So those console logs will be output to a cloudwatch log group. So what 36 00:03:10,830 --> 00:03:14,549 we're going to do now is we're going to try and create the response which will 37 00:03:14,549 --> 00:03:19,739 be consisting of item 1 item 2 at item 3 and we're just going to say received and 38 00:03:19,739 --> 00:03:24,269 whatever key one key two and key 3 are. So what did we receive. We're just going to 39 00:03:24,269 --> 00:03:30,840 send back to the requester. We're going to console out the returned response and 40 00:03:30,840 --> 00:03:34,799 then we're going to turn that response into a string so we can read it and 41 00:03:34,799 --> 00:03:39,209 again that will go to the cloudwatch logs group and then we're going to 42 00:03:39,209 --> 00:03:41,850 return that response. If there's an error we'll just 43 00:03:41,850 --> 00:03:46,410 console.log out the error and then return the response. So that all looks 44 00:03:46,410 --> 00:03:50,069 fine and that should work perfectly. First thing that we need to do now that 45 00:03:50,069 --> 00:03:55,769 we've modified our Lambda function is that we need to save it. So now that 46 00:03:55,769 --> 00:04:02,160 we've saved it we can scroll up and we can test our Lambda function. So what 47 00:04:02,160 --> 00:04:06,930 we're doing here is we're configuring a test event. So what lambda or what the 48 00:04:06,930 --> 00:04:11,400 lambda console will do for us, it will send that request to our lambda function 49 00:04:11,400 --> 00:04:16,919 in the format that we that we specify here. So we'll give it a name first just 50 00:04:16,919 --> 00:04:21,630 going to call a test and I'm going to change this key one key two and 51 00:04:21,630 --> 00:04:26,030 key three values from value one value two 52 00:04:26,030 --> 00:04:32,570 and I'm just copying here from the lab notes and I'm going to change that. I'm 53 00:04:32,570 --> 00:04:36,950 going to change that to milk eggs and bread. So we can clearly see that the 54 00:04:36,950 --> 00:04:40,669 actual test has worked and our code has worked. Just going to click on create to 55 00:04:40,669 --> 00:04:45,320 create that test. Now that the test has been created we can run that. So I just 56 00:04:45,320 --> 00:04:51,800 click on test and here we can see that the execution results are successful. 57 00:04:51,800 --> 00:04:57,350 We'll expand the details. Now this here is what was returned from our Lambda 58 00:04:57,350 --> 00:05:03,260 function. It's item one received bread received eggs received milk, exactly how 59 00:05:03,260 --> 00:05:08,180 we expected from our code. What we can also see is it is the output from our 60 00:05:08,180 --> 00:05:12,590 console.log commands are going to be listed here. So we can see here 61 00:05:12,590 --> 00:05:18,200 we've got function loaded successfully and then returning response and then the 62 00:05:18,200 --> 00:05:23,450 string of that response converted to a string and outputted here 63 00:05:23,450 --> 00:05:30,020 to the console and also that it's been stored in a cloudwatch log group. So we 64 00:05:30,020 --> 00:05:34,430 click here that will open CloudWatch and we can have a look at that 65 00:05:34,430 --> 00:05:42,890 Cloudwatch log group. So there it is. There that top one and there we can see that 66 00:05:42,890 --> 00:05:48,350 the function loaded successfully and also console logged out returning 67 00:05:48,350 --> 00:05:53,570 response and then the string of that response. So that's a very handy tool if 68 00:05:53,570 --> 00:05:57,380 you have any issues with your Lambda function, you can have a look at your 69 00:05:57,380 --> 00:06:01,640 logs and see what the console log output is and see if there's any errors coming 70 00:06:01,640 --> 00:06:05,930 up here that will be output it to the console or to the CloudWatch log group. 71 00:06:05,930 --> 00:06:10,430 So just going to close that out there now that brings us to the end of this 72 00:06:10,430 --> 00:06:17,770 lab what I'll do now is clean it all up. So actions delete that function and 73 00:06:17,770 --> 00:06:22,250 there we go it's gone. So that brings us to the end of this quite a quick clip 74 00:06:22,250 --> 00:06:27,190 and I look forward to seeing you in the next one.