- [Jon] So, now that you have learned a lot about Lambda, but all in theory, let's put it to practice. In an earlier lecture, I told you about that brilliant application of HelloName. So, I want to use this, this time, and then do a demonstration about that HelloName application. If you don't remember it, don't worry. Let me re-explain it to you. The idea from it is I want to go to www.example.com/?name=jon and then that is supposed to reply with "Hello jon." So, the architecture that you've seen from our previous lecture was that API Gateway was receiving, was the endpoint of this www.example.com, sending that into Lambda, and then Lambda, when it needed to, was sending its logs into CloudWatch Logs. That was the whole architecture. So, for this to work, right now, the first piece that we need to do is to create an Identity and Access Management role. Why do we need that? Well, this role is going to be applied to our Lambda function, because Lambda needs to be able to send its logs into CloudWatch Logs. So, that's the first step that I need to go and do. So, join me into the console. First, I am into the Identity and Access Management console, under the Roles, and I'm going to create a brand new role. This role is going to be for Lambda, and then the permissions that I need, I'm going to use an already built policy that comes with your account. That is called the "AWSLambdaBasicExecutionRole," which gives us Write access into CloudWatch Logs. Don't forget to click that little check mark button that is right there. And, I'll click Next: Tags, which are optional, so I'm not gonna go through them. And then, in Review, I need to specify a name, I'm going to call that the LambdaBasicExecutionRole, and create this role. Now that my role is created, I can go and create my Lambda function. So, let's go into the Lambda console, click the Create a Function button. I'll author it from scratch. I won't be using any blueprints or Serverless Application Repository here. So, I will give it a name, called the HelloName Lambda function, and now, I need to select a runtime, the language I will want to use. Obviously, I will be using Node.js because that's my favorite language. And then, I need to specify a certain role, well, the LambdaBasicExecutionRole that we have just created into the IAM console a few seconds ago. I'll hit Create this function. Let me explain to you this console that is right here. The first part is the designer that you see at the very top. This designer makes everything work inside of the Lambda console. If I click on different buttons that is inside of this designer, the bottom of the page changes, as we move along. So if I click on this View permissions, at the bottom of the page, I now see the function policy and then my execution role that I have just created. If I go back and I click on this HelloName piece that is there, which is my Lambda function, now, I can see my code of my Lambda function. Yes. This is how the designer is actually working. On the left-hand side, I can define triggers for my Lambda function, what will call the Lambda function itself. And then from the Lambda function, what it has access to, or what you see on the right-hand side, CloudWatch Logs, is the only thing that my Lambda function has access to right now. Now, let me scroll down into that function code. I have the code entry, three methods. Upload a .zip file, upload a file via S3, or edit the code inline. Today, I'm going to use the edit the code inline. Then, my runtime, Node.js, and then I have the handler. This handler is what tells Lambda, the service, what to call inside of your code. It says call the index file, index.js file. So, if I look on the lower left, I have the index.js file, and then, call the handler function. I have an export of a handler function. That's how Lambda will call my Lambda function. Now, inside of this code, I have a status code of 200, which means an okay status, that everything has worked. And, I have the body, which says Hello from Lambda, and that response is being returned by this handler. So, that's what we return back, depending on the event data that's being sent into my Lambda function. So, what I really want here is to change this Hello to be Hello jon. That's what I want to send. So, I'll save this function, and I'll test it. When I click the Test button, I need to configure an event data, an event data that will be sent into Lambda. So, I'll give it a certain name, HelloNameTest. I'll pass in a certain parameter, name, and then the value is going to be jonathan. And, I'll create this test, then execute it. Now, my execution. If I scroll back to the top of the page, I can see the execution succeeded. It's in green. If it was in red, then I will have had a problem with my test itself. Let's take a look at the details. It says statusCode 200, and the body says "Hello jon." Isn't that supposed to say "jonathan?" Let's go and take a look at the code. Oh yeah. I've hard-coded the value of the name inside of my code. I'm not supposed to have done that. I need to take the data coming from the event payload. So, let me remove that piece in there, and I'll add event.name. From the event variable, I'm going to have a name parameter sent into it. I'll save it and test it again. And this time, I can see "Hello jonathan." Another piece that I can see from within this console is I can see the duration of this Lambda function. It only took 28 milliseconds for it to execute, and I got billed 100 milliseconds. I used 71 MBs of memory, out of 128 MBs of memory that has been assigned to this Lambda function. Scrolling down in the page, in the basic settings, I can see that this is where I can specify my amount of memory for this specific Lambda function, which also controls the CPU, the amount of CPU power that I get, as well as the timeout, the maximum amount of time, that I want my Lambda function to run. So, the next step into here that I want to show you is logs. So, Lambda is supposed to be able to execute, and call, and output logs. So, let's go into our code, and add certain logs into here. I'll go into this and remove that TODO implement, and I'll output a certain log statement. One of the good things to output in your logs is the event payload, to be able to troubleshoot that a little bit later. So, I'll pass an event, event, and then I'll use the JSON.stringify function to take the event payload that is passed as a JSON input, and then stringify it, create a string from it, and then that will be what will be logged in there. I'll hit Save on this and test it one more time. Now, I want to go and look at those logs. Let's go and take a look. I have the Configuration tab right here. This is what we've been playing with all along. But if I go into the Monitoring tab, there is a whole new dashboard that gets executed here. In here, you're going to find that I have invocation count, how many times I have invoked this. You'll find the duration of my Lambda function, and you'll also find the errors. It seems like there are zero errors for this Lambda function I have executed. So, my Lambda function is working. At the top, I also find the View logs in CloudWatch button. So, if I click that, the CloudWatch Logs console opens. I can see now that I have a certain log stream that got created for me. So, there are three log streams. Why is that? Well, every time that I change my Lambda function, a brand new container gets deployed, a brand new Lambda function gets invoked in there, which means there will be a brand new log stream that gets created. I changed my code three times; hence, why I see three log streams in there. The last log stream is the 10:30 log stream, right here. So, if I click on it, what I am going to find in there is the start of my Lambda function. And then, I have the event, colon, my payload, so the name "jonathan" that was sent as part of my test. So, this is how you can do troubleshooting with Lambda. Lambda is using console.log, or any print output in Python, for example, will be sent into CloudWatch Logs, and you can go in and review them inside of CloudWatch Logs. Let's go back into our diagram. So at this point, we've created everything on the right-hand side from Lambda to CloudWatch Logs. In the next video, I'm going to integrate API Gateway into this architecture, where this www.example.com/?name=jon is going to be sent into. If you have the time, you can follow me over there.