In this video, we are going to learn a lot of the context object in Node.js functions.
When lambda runs our function, it passes a context object to the handle method.
This object provides methods and properties that provide information about the invocation, function
and execution environment.
You can see any lambda function code.
The second parameter is the context object.
You can see in the picture.
The second parameter is context object that we can use some methods and parameters.
So context methods, a context object has methods and parameters about the function, invocation function
itself and execution environment.
For example, there is the context method.
Which name is gate remaining time in millis.
This is returns the number of milliseconds left before the execution time loss.
And we have some context properties, which is the function name, the function of the name of the lambda
function and function version, the version of the function.
So these kinds of information can be accessible from the context object.
So now we are going to develop an example lambda function to see context objects and parameters into
the lambda function.
So let's get started.
In order to do that, please create a new lecture folder and add the command text file and we will follow
these for step one performing this example.
So basically we are going to create a new lambda function and we will start with developing this function
for that purpose.
I'm going to create an index that JS file and under the index case file we will follow this example.
In this example, as you can see that this is a similar handed method, but this time we are going to
get second parameter, which is the context.
So event comma, context, object, this will be the lambda function.
And in this context object we basically lock some of the information, for example, remaining time
out of the milliseconds.
We are using the get remaining timeout in milliseconds and also we can get the function name in here.
Basically we will lock this function name and remaining time in here and return back this information
to the body of this lambda function.
Please write this function and we will create this function into our environment.
So if you come to the comma section, the second step is zip function code.
In order to do that, I am going to use compress archive command because we have only indexes.
Please open a new terminal and go to.
Lecture.
Now I'm going to compress archive or index JS.
This will be a great function zip file.
And the third step is create lambda function with using the sail command.
If you scroll down, you can see the third step.
In order to perform create a new function, we should get the function role which is the Lambda X.
Aaron Please get this Aaron name and after that we are going to create function with using the lambda
create function select command.
So in order to do that, I'm going to copy and paste this command.
This command will be create a new function.
Which name is my function?
Three.
Runtime will be Node.js.
Zip file which we have a zip in the last step and handle is index and the run name will be the lambda
x, which we create in the beginning of this section.
Hit the enter and create this function.
It is created successfully and now it is time to invoke this function with following the same steps.
So I'm going to create a JSON file.
You can see in here.
Let me create a new file.
Jason file this will be the our main.
Don't object that we will pass one invoking our lambda function.
Okay.
Now I'm going to invoke our lambda function, which is the my function theory.
In order to do that, please complete this response and create your window and copy this command, which
is the lambda IMO command.
This will be the invoke or a the synchronously function name is my function three and payload is even
adjacent file that we passed in this file.
Okay.
Please hit the enter and invoke our function.
So as you can see that we get success response.
If you see the response JSON object.
You can see the details of the context object parameter.
The first parameter is function name.
Function name is my function three and log it to name.
You can see the log setter name in here.
If you also compare this log system from the lambda functions, you can see lambda functions and see
that our newly created function is in here.
If you go to monitor and V cloud watch logs, you can see the log system name comes in here.
This is the same name.
What we have seen in the our response object.
And if you see like to see the details, you can see the logs about the context remaining time and also
function name and so on.
So we can see all these context object details into documentation.
If you scroll down the C command text, you can see in here, this is the all context properties from
official documentation.
You can see that function name version invoked function Aaron Memory limit, log group name, log system,
name, identity, client context and so on.
This is the official documentation about the context object.
Okay.
As you can see that we have seen how we can use a lambda context object in the Node.js functions.