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