1 00:00:11,690 --> 00:00:16,440 Welcome back to BackSpace Academy. In this lab we're going to be creating a 2 00:00:16,440 --> 00:00:22,920 Python Lambda function using the lambda management console. Once we've done that 3 00:00:22,920 --> 00:00:27,660 we're going to edit that Lambda function with our own Python code and then we'll 4 00:00:27,660 --> 00:00:33,120 invoke that Lambda function and test it also by sending a request to it and 5 00:00:33,120 --> 00:00:37,980 seeing exactly what it responds with and we're going to do all of this just using 6 00:00:37,980 --> 00:00:44,550 the Lambda management console. So let's get into it. Okay so starting off in the 7 00:00:44,550 --> 00:00:49,219 Lambda management console. Of course we get there by services and then lambda. 8 00:00:49,219 --> 00:00:54,449 I'm just going to open up this this toolbar on the left hand side here. 9 00:00:54,449 --> 00:00:59,430 It will go to functions and then we'll create our function. So we're going to be 10 00:00:59,430 --> 00:01:04,049 authoring this from scratch we'll give that function a name. I'm just going to 11 00:01:04,049 --> 00:01:11,220 go to the lab notes here and copy the name over. Okay so it's backspace-lambda-lab. 12 00:01:11,220 --> 00:01:15,299 Now the runtime that we're going to be using for this is not NodeJS, 13 00:01:15,299 --> 00:01:21,060 it's going to be Python. So we'll select up the top here Python and we need to 14 00:01:21,060 --> 00:01:26,460 create or choose an execution role. So I've already got one here. You probably 15 00:01:26,460 --> 00:01:31,049 will have as well called Lambda basic execution. If you don't have 16 00:01:31,049 --> 00:01:35,280 that role already then there are instructions in the lab notes on how to 17 00:01:35,280 --> 00:01:40,079 actually create that role that IAM role. So everything there is ready to go. 18 00:01:40,079 --> 00:01:47,460 We'll create that function. Okay so that has successfully created and what it has 19 00:01:47,460 --> 00:01:53,610 created is just a very basic bare-bones bit of code for our Lambda function. 20 00:01:53,610 --> 00:01:56,850 There we can see that that is there. All that's doing is just sending back a 21 00:01:56,850 --> 00:02:00,180 hello from lambda. We're going to do a little bit more from that because we're 22 00:02:00,180 --> 00:02:05,880 going to be receiving and interacting with our requester. So what I'll do now 23 00:02:05,880 --> 00:02:12,110 is I'll just copy from the lab notes the code that we're going to be using. 24 00:02:12,460 --> 00:02:19,010 Okay so we can see we're going to be importing JSON so we need to import 25 00:02:19,010 --> 00:02:24,080 that that module or that package and we're going to be using that because 26 00:02:24,080 --> 00:02:30,740 we're going to be receiving a JSON object from from a requester and what 27 00:02:30,740 --> 00:02:34,880 we're doing you've done here is it this is our lambda handle. So we've called it 28 00:02:34,880 --> 00:02:38,690 Lambda_handler but we can call it whatever we want. We just need to 29 00:02:38,690 --> 00:02:43,220 make sure that we refer to it as whatever we've called it here. It's going 30 00:02:43,220 --> 00:02:48,230 to be receiving an event. So after we've received that event and that will be as 31 00:02:48,230 --> 00:02:53,600 an JSON object, we're going to be printing that the function loaded 32 00:02:53,600 --> 00:02:59,240 successfully. Now that print is not going to be going to a screen because Lambda 33 00:02:59,240 --> 00:03:03,200 doesn't have a screen but where it will be going to is it will be going to 34 00:03:03,200 --> 00:03:08,090 Cloudwatch logs but we can also see the output for that in the management 35 00:03:08,090 --> 00:03:13,730 console as well. Which we'll see soon when we test this, and what we're going 36 00:03:13,730 --> 00:03:17,090 to do next is that we're going to create this response object which is going to 37 00:03:17,090 --> 00:03:23,360 be item one and then we're just going to add receive two, whatever we received as 38 00:03:23,360 --> 00:03:28,250 key one key two and key three. So our event object is going to have some 39 00:03:28,250 --> 00:03:33,410 fields called key one key two key three and we're going to send them back to the 40 00:03:33,410 --> 00:03:38,630 requester. So we're just going to print here again returning response and then 41 00:03:38,630 --> 00:03:45,080 the response and that will be printed out to the cloudwatch logs group that 42 00:03:45,080 --> 00:03:49,670 has been created, which will be automatically created by the Lambda 43 00:03:49,670 --> 00:03:53,090 service and then we're going to return that response to the requester. So that 44 00:03:53,090 --> 00:03:57,410 looks fine what we need to do now is, because we've modified the code we need 45 00:03:57,410 --> 00:04:02,660 to make sure that we save this, otherwise it will be still using the old code. 46 00:04:02,660 --> 00:04:06,560 I will scroll up now. What we can do is we can test this function quite easily in 47 00:04:06,560 --> 00:04:13,550 the console here. We'll click on test now. What I'm going to do is going to create 48 00:04:13,550 --> 00:04:19,070 or just kind of slightly modify this test. So just cutting and pasting from 49 00:04:19,070 --> 00:04:23,270 the lab notes here, we've got here key one key two key three. Got to have value 50 00:04:23,270 --> 00:04:27,240 one value two value three. I'm just going to change that to 51 00:04:27,240 --> 00:04:31,690 milk eggs and bread so we can clearly see that the test is actually worked 52 00:04:31,690 --> 00:04:38,500 fine. We'll give this event a name and we'll create that, and so what's going to 53 00:04:38,500 --> 00:04:43,000 happen is that when we click on test, it's going to send this to our Lambda 54 00:04:43,000 --> 00:04:49,630 function. So if we click on test again, it will send that to our Lambda function. 55 00:04:49,630 --> 00:04:53,050 We'll be able to see whether our Lambda function is working. So here we can see 56 00:04:53,050 --> 00:04:58,540 execution result succeeded. We'll have a look at the details here and that is 57 00:04:58,540 --> 00:05:03,790 what was sent back from our Lambda function, item one received bread received 58 00:05:03,790 --> 00:05:09,160 eggs received milk, exactly how we expected it to behave. If we scroll down 59 00:05:09,160 --> 00:05:14,200 here also we can see that we've got a log output from our print commands and 60 00:05:14,200 --> 00:05:20,440 so we can see here returning response item one and which is going to be the 61 00:05:20,440 --> 00:05:25,570 the response sent out. We also have here function loaded successfully. So at 62 00:05:25,570 --> 00:05:29,880 the start of the function we had a print command that was going to send that out. 63 00:05:29,880 --> 00:05:37,060 Now this log output goes to cloudwatch logs. So that what we can do there is, 64 00:05:37,060 --> 00:05:43,860 we can click here and we can see that Cloudwatch log group. 65 00:05:49,050 --> 00:05:52,949 So at the top here, we'll see the log stream for that cloudwatch log group 66 00:05:52,949 --> 00:05:58,199 and here we can see it's outputted function loaded successfully and 67 00:05:58,199 --> 00:06:04,099 returning response and then it's done item one receipt bread and whatever and 68 00:06:04,099 --> 00:06:08,819 that is exactly how we expected. So it's a very good tool that, so if you have any 69 00:06:08,819 --> 00:06:11,970 problems with your lambda function, you can always go and have a look at these 70 00:06:11,970 --> 00:06:15,930 cloudwatch logs and check for any errors, check for any console log output 71 00:06:15,930 --> 00:06:21,810 or a print output and it's a very good tool to use. So now that we've 72 00:06:21,810 --> 00:06:25,050 done that, we don't need this anymore. We've done what we need to do 73 00:06:25,050 --> 00:06:31,639 and we can clean this up. So we go to actions and delete the function and 74 00:06:31,639 --> 00:06:36,509 there the function has gone. So that brings us to the end of quite a quick lab and I 75 00:06:36,509 --> 00:06:40,430 look forward to seeing you in the next ones.