1
00:00:00,360 --> 00:00:05,460
In this video, we are going to develop Hands on labs, which is the calculator project.

2
00:00:05,760 --> 00:00:07,620
You can see the image on the slide.

3
00:00:07,650 --> 00:00:12,380
This is very similar with the previous hands on loop, but this time we are going to create a lambda

4
00:00:12,390 --> 00:00:19,290
and develop calculator function and this will be invoked synchronously from the CLI commands and using

5
00:00:19,290 --> 00:00:20,580
the event JSON object.

6
00:00:20,580 --> 00:00:26,670
And in the event object, we should start with the designing event first and after that we will develop

7
00:00:26,670 --> 00:00:29,840
our lambda function according to these JSON object.

8
00:00:29,850 --> 00:00:31,530
So let's get started.

9
00:00:33,090 --> 00:00:40,050
Please open the Ouija board window and you can see that these are the steps, what we are going to do

10
00:00:40,050 --> 00:00:41,860
for this calculator project.

11
00:00:41,880 --> 00:00:45,630
First of all, this time we will start with the design of the event.

12
00:00:45,660 --> 00:00:52,590
This is the best practice to follow with creating event object before developing lambda function to

13
00:00:52,590 --> 00:00:58,750
understand how our lambda function and how we should develop our function according to incoming events.

14
00:00:58,860 --> 00:00:59,280
Object.

15
00:00:59,280 --> 00:01:03,230
So that's why I have put the first place designing lambda event.

16
00:01:03,240 --> 00:01:06,080
So basically this is the calculator project.

17
00:01:06,090 --> 00:01:13,620
For that purpose we are going to create a JSON file into a lecture folder and put these JSON format

18
00:01:13,620 --> 00:01:14,340
in here.

19
00:01:14,370 --> 00:01:21,450
You can find these lecture 63 calculator project and if you go to the event object or if you create

20
00:01:21,450 --> 00:01:26,180
these this object, you can create these kind of incoming images object.

21
00:01:26,190 --> 00:01:32,910
So we set that we have A and B attributes that could be represent number one and number two.

22
00:01:32,910 --> 00:01:41,610
And we have operated we expect opt attribute and the operator we can expect a calculator processes in

23
00:01:41,610 --> 00:01:43,730
this in this attribute value.

24
00:01:43,740 --> 00:01:50,940
So basically we will perform some operation with these numbers and it is can be configurable to change

25
00:01:50,940 --> 00:01:51,720
the operation.

26
00:01:51,720 --> 00:01:57,360
For example, we can multiply these numbers or we can divide these numbers, but this time I would like

27
00:01:57,360 --> 00:01:59,490
to stay at some operation.

28
00:01:59,490 --> 00:02:05,940
So the idea is that we are expecting this event JSON when invoking our calculated lambda functions after

29
00:02:05,940 --> 00:02:13,760
designing these lambda event JSON file object, now we can develop our lambda function for that purpose.

30
00:02:13,770 --> 00:02:19,860
Please go to the command section and see that now we are in the first step, which is the lambda function

31
00:02:19,860 --> 00:02:20,370
code.

32
00:02:20,400 --> 00:02:26,130
For that purpose we are going to create an index JS file and in the index case file here you can find

33
00:02:26,130 --> 00:02:28,800
the basic implementation of handle method.

34
00:02:28,830 --> 00:02:34,620
We are getting the event object which will be the our events object and in the event object in the first

35
00:02:34,620 --> 00:02:40,230
place, we are going to lock these incoming event into the Amazon Cloud Showcase and after that we will

36
00:02:40,230 --> 00:02:42,210
perform the calculator operation.

37
00:02:42,210 --> 00:02:46,260
So please start with coding in the validation.

38
00:02:46,260 --> 00:02:53,760
I would like to put a validation before we start because it is very important to get event A and even

39
00:02:53,760 --> 00:02:54,840
be an operator.

40
00:02:54,840 --> 00:03:01,170
Otherwise if there is undefined, we should return an exception for that purpose.

41
00:03:01,170 --> 00:03:09,090
I'm going to develop this block with throwing an exception if any of the parameters are undefined.

42
00:03:09,150 --> 00:03:15,510
So you can see these if statement this is validation, we can check a, B and or attributes in the event

43
00:03:15,510 --> 00:03:21,390
incoming event JSON object if one of them is undefined, we are throwing a new exception which is the

44
00:03:21,420 --> 00:03:23,850
event should exist these attributes.

45
00:03:24,000 --> 00:03:24,340
Okay.

46
00:03:24,390 --> 00:03:30,900
After the validation we can perform the actual business logic which is the or calculated project for

47
00:03:30,900 --> 00:03:31,470
that purpose.

48
00:03:31,470 --> 00:03:34,890
I'm going to create an.

49
00:03:35,990 --> 00:03:42,370
Object, which is the result object that will be load up to the perform the calculation.

50
00:03:42,380 --> 00:03:50,960
So after that I'm going to root use switch statement see which should be the event that operator operation

51
00:03:50,960 --> 00:03:58,550
and the value should be check operator will be plus the or multiply.

52
00:03:58,580 --> 00:04:08,780
For that reason I'm going to write this case and if there is a plus sign I'm going to calculate result

53
00:04:08,780 --> 00:04:12,500
a cool event dot a plus event dot b.

54
00:04:13,750 --> 00:04:19,150
Look, now we have finished the same operation and is the same way.

55
00:04:19,150 --> 00:04:22,480
I'm going to develop other calculated operations.

56
00:04:25,270 --> 00:04:30,460
So now this is the operation we are deducting.

57
00:04:32,000 --> 00:04:42,770
A and even B, and if you copy this case, this time I'm going to perform multiply operation so put

58
00:04:42,770 --> 00:04:48,380
to multiply and this time we are multiplying the incoming A and B numbers.

59
00:04:48,380 --> 00:04:51,560
And the last case will be the divide.

60
00:04:53,840 --> 00:05:01,490
For that purpose, I'm going to divide you A and event b oc.

61
00:05:01,520 --> 00:05:06,890
I can remove the default block because we have put the if statement.

62
00:05:06,890 --> 00:05:12,950
So these operators and the numbers should be exist in our incoming event JSON and this is our switch

63
00:05:12,950 --> 00:05:21,080
statement that we are performing the calculator operation and after that I'm going to lock this result

64
00:05:21,080 --> 00:05:22,220
for that purpose.

65
00:05:22,250 --> 00:05:29,480
We can look to the watch, look the result calculated result operation.

66
00:05:29,480 --> 00:05:33,110
We can see this line of code and after that I'm going to turn.

67
00:05:33,110 --> 00:05:41,510
The result here is very simple calculator lambda function that we are going to publish and deploy the

68
00:05:41,960 --> 00:05:43,040
lambda function.

69
00:05:43,070 --> 00:05:46,970
After we have finished the development, we can go to the next step.

70
00:05:46,970 --> 00:05:51,530
If you follow the command text, you can see that the next step is zip file.

71
00:05:51,560 --> 00:05:53,690
So please open a new terminal.

72
00:05:57,070 --> 00:05:59,860
And go to a related lecture.

73
00:06:00,910 --> 00:06:01,630
Calculator.

74
00:06:01,780 --> 00:06:02,280
Okay.

75
00:06:02,320 --> 00:06:10,570
Now I'm going to follow the second command since we have one index case file, it is created function

76
00:06:10,570 --> 00:06:11,290
zip file.

77
00:06:11,380 --> 00:06:16,570
And I am following the third step which is the create lambda function for that purpose.

78
00:06:16,570 --> 00:06:20,260
I have already get the A run before, which is the lambda execution role.

79
00:06:20,440 --> 00:06:26,560
Now I'm going to run directly this lambda create function select command hit enter.

80
00:06:26,560 --> 00:06:31,090
The function name should be the calculator and function is created successfully.

81
00:06:31,180 --> 00:06:39,400
And if you scroll down we have last step is invoke this function as you remember that we have created

82
00:06:39,670 --> 00:06:43,960
JSON file so now I'm going to invoke this file with.

83
00:06:45,100 --> 00:06:51,700
Give them even an object so you can copy and paste this output and hit enter.

84
00:06:51,850 --> 00:06:56,380
If you hit enter you can see this code is to 100 successfully executed.

85
00:06:56,380 --> 00:07:00,880
And if you go to response, Jason, you can see the response is seven.

86
00:07:00,880 --> 00:07:08,020
So that means we have performed this A and B operation and some these numbers very good.

87
00:07:08,020 --> 00:07:12,040
So after that, we can also change these events.

88
00:07:12,040 --> 00:07:16,300
JSON file, for example, as you can see that now the response is seven.

89
00:07:16,300 --> 00:07:22,660
But what if I change this operation as a multiply if you save this event JSON file and if you run the

90
00:07:22,690 --> 00:07:29,110
invoke again hit the enter and this time if you check the response, Jason, the response coming ten

91
00:07:29,110 --> 00:07:33,940
so that means we have multiply these two numbers A and B, very good.

92
00:07:33,940 --> 00:07:39,430
So now let's check these operations, these Lambda functions from the A management console.

93
00:07:40,000 --> 00:07:46,150
Please open the A management console, go to functions and see that we have calculated function go to

94
00:07:46,150 --> 00:07:52,510
function page and as you can see that our codes coming in index case as expected.

95
00:07:52,510 --> 00:07:57,700
So now before checking the cloud shows I would like to test again in here.

96
00:07:57,700 --> 00:08:05,410
If you click the test section and create a test operation and if you send a random head over this evaluation

97
00:08:05,410 --> 00:08:12,490
file, let's say this operation and click the test button and see what's happened this time the execution

98
00:08:12,490 --> 00:08:16,630
result is failed and we can see that the error is returning.

99
00:08:16,630 --> 00:08:19,630
The error type and error message is even should exist.

100
00:08:19,630 --> 00:08:23,350
AB and operator attributes in the incoming evaluation.

101
00:08:23,350 --> 00:08:27,370
So we also test the worse scenario with throwing an exception.

102
00:08:27,370 --> 00:08:32,530
Now if you would like to check the Amazon cloud looks, we should see three in location.

103
00:08:32,530 --> 00:08:39,490
And in these three location we should see that this function is invoked by the this incoming event.

104
00:08:39,490 --> 00:08:47,850
First we are some these two and five and return to the seven and after that we change the incoming event

105
00:08:47,860 --> 00:08:52,540
JSON file and we are multiply these numbers and return back to the ten.

106
00:08:53,080 --> 00:09:00,010
And lastly, we are sending a random invitation and we solve that it is returning an exception regarding

107
00:09:00,010 --> 00:09:04,840
these incoming payload should be a, b and operator attributes.

108
00:09:05,290 --> 00:09:06,520
Okay, very good.

109
00:09:06,850 --> 00:09:12,790
As you can see that we have successfully developed our hands on love, which is the calculator project.
