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