1
00:00:00,270 --> 00:00:06,390
In this video, we are going to learn a lot of the context object in Node.js functions.

2
00:00:07,530 --> 00:00:12,630
When lambda runs our function, it passes a context object to the handle method.

3
00:00:12,810 --> 00:00:18,360
This object provides methods and properties that provide information about the invocation, function

4
00:00:18,360 --> 00:00:20,040
and execution environment.

5
00:00:20,730 --> 00:00:22,540
You can see any lambda function code.

6
00:00:22,560 --> 00:00:25,050
The second parameter is the context object.

7
00:00:25,080 --> 00:00:26,550
You can see in the picture.

8
00:00:26,580 --> 00:00:31,830
The second parameter is context object that we can use some methods and parameters.

9
00:00:32,580 --> 00:00:39,420
So context methods, a context object has methods and parameters about the function, invocation function

10
00:00:39,420 --> 00:00:41,320
itself and execution environment.

11
00:00:41,340 --> 00:00:43,740
For example, there is the context method.

12
00:00:43,740 --> 00:00:46,620
Which name is gate remaining time in millis.

13
00:00:46,710 --> 00:00:51,020
This is returns the number of milliseconds left before the execution time loss.

14
00:00:51,630 --> 00:00:57,510
And we have some context properties, which is the function name, the function of the name of the lambda

15
00:00:57,510 --> 00:01:00,650
function and function version, the version of the function.

16
00:01:00,660 --> 00:01:04,930
So these kinds of information can be accessible from the context object.

17
00:01:04,950 --> 00:01:11,970
So now we are going to develop an example lambda function to see context objects and parameters into

18
00:01:11,970 --> 00:01:13,020
the lambda function.

19
00:01:13,080 --> 00:01:14,820
So let's get started.

20
00:01:14,910 --> 00:01:20,970
In order to do that, please create a new lecture folder and add the command text file and we will follow

21
00:01:20,970 --> 00:01:24,640
these for step one performing this example.

22
00:01:24,660 --> 00:01:31,020
So basically we are going to create a new lambda function and we will start with developing this function

23
00:01:31,020 --> 00:01:31,920
for that purpose.

24
00:01:31,920 --> 00:01:39,500
I'm going to create an index that JS file and under the index case file we will follow this example.

25
00:01:39,510 --> 00:01:45,450
In this example, as you can see that this is a similar handed method, but this time we are going to

26
00:01:45,450 --> 00:01:48,210
get second parameter, which is the context.

27
00:01:48,210 --> 00:01:52,860
So event comma, context, object, this will be the lambda function.

28
00:01:52,860 --> 00:01:58,800
And in this context object we basically lock some of the information, for example, remaining time

29
00:01:58,800 --> 00:02:00,450
out of the milliseconds.

30
00:02:00,450 --> 00:02:06,450
We are using the get remaining timeout in milliseconds and also we can get the function name in here.

31
00:02:06,480 --> 00:02:11,940
Basically we will lock this function name and remaining time in here and return back this information

32
00:02:11,940 --> 00:02:14,520
to the body of this lambda function.

33
00:02:15,180 --> 00:02:21,180
Please write this function and we will create this function into our environment.

34
00:02:21,180 --> 00:02:26,400
So if you come to the comma section, the second step is zip function code.

35
00:02:26,430 --> 00:02:33,490
In order to do that, I am going to use compress archive command because we have only indexes.

36
00:02:33,510 --> 00:02:36,930
Please open a new terminal and go to.

37
00:02:40,650 --> 00:02:41,490
Lecture.

38
00:02:43,470 --> 00:02:46,830
Now I'm going to compress archive or index JS.

39
00:02:46,860 --> 00:02:49,590
This will be a great function zip file.

40
00:02:49,710 --> 00:02:54,030
And the third step is create lambda function with using the sail command.

41
00:02:54,060 --> 00:02:57,060
If you scroll down, you can see the third step.

42
00:02:57,180 --> 00:03:03,970
In order to perform create a new function, we should get the function role which is the Lambda X.

43
00:03:03,990 --> 00:03:11,610
Aaron Please get this Aaron name and after that we are going to create function with using the lambda

44
00:03:11,610 --> 00:03:13,200
create function select command.

45
00:03:13,350 --> 00:03:17,460
So in order to do that, I'm going to copy and paste this command.

46
00:03:17,490 --> 00:03:19,380
This command will be create a new function.

47
00:03:19,380 --> 00:03:20,790
Which name is my function?

48
00:03:20,790 --> 00:03:21,450
Three.

49
00:03:22,200 --> 00:03:23,550
Runtime will be Node.js.

50
00:03:23,580 --> 00:03:30,840
Zip file which we have a zip in the last step and handle is index and the run name will be the lambda

51
00:03:30,840 --> 00:03:33,450
x, which we create in the beginning of this section.

52
00:03:33,570 --> 00:03:36,210
Hit the enter and create this function.

53
00:03:36,210 --> 00:03:43,200
It is created successfully and now it is time to invoke this function with following the same steps.

54
00:03:43,200 --> 00:03:45,840
So I'm going to create a JSON file.

55
00:03:45,900 --> 00:03:47,010
You can see in here.

56
00:03:47,010 --> 00:03:48,660
Let me create a new file.

57
00:03:50,910 --> 00:03:54,450
Jason file this will be the our main.

58
00:03:56,300 --> 00:04:00,320
Don't object that we will pass one invoking our lambda function.

59
00:04:00,350 --> 00:04:01,190
Okay.

60
00:04:01,220 --> 00:04:05,330
Now I'm going to invoke our lambda function, which is the my function theory.

61
00:04:05,420 --> 00:04:12,770
In order to do that, please complete this response and create your window and copy this command, which

62
00:04:12,770 --> 00:04:14,660
is the lambda IMO command.

63
00:04:14,690 --> 00:04:21,170
This will be the invoke or a the synchronously function name is my function three and payload is even

64
00:04:21,170 --> 00:04:24,890
adjacent file that we passed in this file.

65
00:04:24,920 --> 00:04:25,320
Okay.

66
00:04:25,340 --> 00:04:29,030
Please hit the enter and invoke our function.

67
00:04:29,750 --> 00:04:32,150
So as you can see that we get success response.

68
00:04:32,150 --> 00:04:35,390
If you see the response JSON object.

69
00:04:35,420 --> 00:04:40,370
You can see the details of the context object parameter.

70
00:04:40,880 --> 00:04:42,650
The first parameter is function name.

71
00:04:42,650 --> 00:04:46,550
Function name is my function three and log it to name.

72
00:04:46,550 --> 00:04:48,770
You can see the log setter name in here.

73
00:04:48,860 --> 00:04:56,720
If you also compare this log system from the lambda functions, you can see lambda functions and see

74
00:04:56,720 --> 00:04:59,200
that our newly created function is in here.

75
00:04:59,210 --> 00:05:05,390
If you go to monitor and V cloud watch logs, you can see the log system name comes in here.

76
00:05:05,420 --> 00:05:06,530
This is the same name.

77
00:05:06,530 --> 00:05:10,820
What we have seen in the our response object.

78
00:05:10,820 --> 00:05:18,290
And if you see like to see the details, you can see the logs about the context remaining time and also

79
00:05:18,290 --> 00:05:20,090
function name and so on.

80
00:05:20,420 --> 00:05:25,980
So we can see all these context object details into documentation.

81
00:05:26,000 --> 00:05:33,710
If you scroll down the C command text, you can see in here, this is the all context properties from

82
00:05:33,710 --> 00:05:35,030
official documentation.

83
00:05:35,270 --> 00:05:42,230
You can see that function name version invoked function Aaron Memory limit, log group name, log system,

84
00:05:42,230 --> 00:05:45,080
name, identity, client context and so on.

85
00:05:45,260 --> 00:05:49,400
This is the official documentation about the context object.

86
00:05:49,640 --> 00:05:50,000
Okay.

87
00:05:50,030 --> 00:05:57,400
As you can see that we have seen how we can use a lambda context object in the Node.js functions.
