1
00:00:00,120 --> 00:00:04,740
In this video, we are going to develop inventory, microscopes, lambda function code.

2
00:00:04,740 --> 00:00:11,520
So that means we are in here and we will develop this inventory microscope with pulling skills and we

3
00:00:11,520 --> 00:00:14,490
will save these items into inventory database.

4
00:00:14,550 --> 00:00:18,780
So as you remember that we have created a normative table for inventory operations.

5
00:00:18,780 --> 00:00:24,780
If you open the console, go to the DB tables, you can see the inventory tables and partition key is

6
00:00:24,780 --> 00:00:25,310
a code.

7
00:00:25,320 --> 00:00:32,370
Now please open the Visual Studio code and I have created separate lecture folder for dimensional microservices.

8
00:00:32,370 --> 00:00:33,390
In the common text.

9
00:00:33,390 --> 00:00:34,560
We will follow this step.

10
00:00:34,560 --> 00:00:40,680
We solve the inventory table and now I'm going to show you incoming event JSON object from the space.

11
00:00:40,680 --> 00:00:44,310
You can see the notification event that we have stored in the last video.

12
00:00:44,340 --> 00:00:51,420
If you click the notification and you are JSON, you can see that these records are consumed by the

13
00:00:51,420 --> 00:00:58,560
Lambda microservices and in the body information we will follow the SAS documentation and in the body

14
00:00:58,560 --> 00:00:59,760
we have a message.

15
00:00:59,760 --> 00:01:06,660
So in the message we can access the actual JSON payload that we sent from the HTTP post request.

16
00:01:06,780 --> 00:01:12,870
So for that purpose, I'm going to follow these events, JSON, and we will implement these lecture

17
00:01:12,870 --> 00:01:16,260
folder as a Node.js application for eventual microservices.

18
00:01:16,260 --> 00:01:22,890
So I'm going to locate this folder and I will run the API NPM command to create package JSON file.

19
00:01:22,890 --> 00:01:28,260
So for that purpose, let me come here and locate the correct folder.

20
00:01:29,310 --> 00:01:31,350
Which is the of microservices.

21
00:01:31,380 --> 00:01:33,980
Now we can run these commands one by one.

22
00:01:33,990 --> 00:01:35,160
So basically.

23
00:01:36,340 --> 00:01:42,270
Running MP I'm in and why to make these folder as a node.js module and the first thing.

24
00:01:42,280 --> 00:01:44,680
Please open the package JSON and go to.

25
00:01:45,610 --> 00:01:51,210
Configurations and set the type, attribute and value should be module in order to gain ActionScript

26
00:01:51,330 --> 00:01:52,600
six standards.

27
00:01:52,600 --> 00:01:58,600
And after that, if you come back to our commands, we have two required package should be installed

28
00:01:58,600 --> 00:02:04,040
these Node.js application these are the client dynamo db and util dynamo db to interact with the Dynamo

29
00:02:04,040 --> 00:02:04,990
ADB package.

30
00:02:04,990 --> 00:02:13,270
So I'm going to install one by one these two packages and after that we will develop our actual lambda

31
00:02:13,270 --> 00:02:13,840
operations.

32
00:02:13,840 --> 00:02:14,130
Okay.

33
00:02:14,140 --> 00:02:14,710
Very good.

34
00:02:14,710 --> 00:02:17,380
So now we can focus on the development part.

35
00:02:17,380 --> 00:02:23,380
So for that purpose, please verify that in the package json, we have installed the required package

36
00:02:23,380 --> 00:02:25,840
successfully and your type as a module.

37
00:02:25,990 --> 00:02:29,830
After that, you can create an index case and index case.

38
00:02:29,830 --> 00:02:35,770
I have created an empty function which is the export handle, lambda function and log.

39
00:02:35,770 --> 00:02:41,140
The first line of code we will implement in the to do statement and I put for the case below.

40
00:02:41,320 --> 00:02:41,650
Okay.

41
00:02:41,650 --> 00:02:46,330
Now we will design our inventory microservices and implement this function code.

42
00:02:46,330 --> 00:02:47,890
The idea is very simple.

43
00:02:47,920 --> 00:02:53,590
We are in here and basically we will handle incoming event from the case and interact with the inventory

44
00:02:53,590 --> 00:02:54,790
dynamo DB table.

45
00:02:54,970 --> 00:02:57,560
So let's make a plan with Writing Circle.

46
00:02:57,610 --> 00:03:01,990
If you open the our index case, we will write the code.

47
00:03:01,990 --> 00:03:08,140
For example, the first item we should do that iterate the case records because this event has records

48
00:03:08,140 --> 00:03:10,690
and we will iterate these records.

49
00:03:10,690 --> 00:03:17,440
And after that, in the second step, we will get requests by the payload of Psns because we will reach

50
00:03:17,440 --> 00:03:18,430
the body information.

51
00:03:18,430 --> 00:03:26,110
This comes from the Psns and after that we will get in request from the body of the SNS data to reach

52
00:03:26,110 --> 00:03:27,160
the order data.

53
00:03:27,160 --> 00:03:33,810
So that means if you open the command text, you can remember that we will reach from the Psns as case

54
00:03:33,850 --> 00:03:34,270
lambda.

55
00:03:34,270 --> 00:03:40,180
We are in this point, so we will get the response request from the SCS and request get the request

56
00:03:40,180 --> 00:03:41,110
from the Psns.

57
00:03:41,110 --> 00:03:46,330
So in the SNS we have an actual order data that we can access.

58
00:03:46,330 --> 00:03:49,900
So that's why, that's why we get the request two times.

59
00:03:49,900 --> 00:03:55,870
And the last action is the fourth step, which is a save order item in the inventory dynamically we

60
00:03:55,900 --> 00:03:58,930
table with using dynamo db sdk package.

61
00:03:59,050 --> 00:04:02,200
Okay, let's do these actions one by one.

62
00:04:02,200 --> 00:04:07,060
I'm going to start with the first action, which is the ITERATE case records.

63
00:04:07,060 --> 00:04:09,460
I will start with writing in three block.

64
00:04:09,490 --> 00:04:12,340
We can come here and iterate in this position.

65
00:04:12,670 --> 00:04:14,440
I have created four statement.

66
00:04:14,440 --> 00:04:20,320
Basically we will iterate event records because we know that in the incoming event you can check the

67
00:04:20,320 --> 00:04:21,790
notification event, JSON.

68
00:04:21,790 --> 00:04:27,910
We have records array, you can come here and come to go home.

69
00:04:28,150 --> 00:04:33,400
As you can see that we have records array, so that's why we are reaching these the first item of the

70
00:04:33,400 --> 00:04:33,970
record.

71
00:04:33,970 --> 00:04:40,030
So go back to index case and iterate event records and get the records and log records.

72
00:04:40,030 --> 00:04:46,180
So we finish the first item, iterating is case and now I'm going to perform second item, which is

73
00:04:46,180 --> 00:04:49,150
the gate request by the payload of Psns.

74
00:04:49,150 --> 00:04:56,830
So for the first payload we will get the record body information and this will be the SAS published

75
00:04:56,830 --> 00:04:57,400
message.

76
00:04:57,400 --> 00:05:03,850
So if you come back to our notification event and if you go to the information the body including the

77
00:05:03,940 --> 00:05:10,630
SNS event, JSON object, so that's why we have convert the first time which is getting the SNS published

78
00:05:10,630 --> 00:05:11,200
message.

79
00:05:11,200 --> 00:05:15,100
So this is the message when we are publishing the Psns topic.

80
00:05:15,100 --> 00:05:20,410
So get these message first and after that we will go one level deeper.

81
00:05:20,530 --> 00:05:25,450
This is the gate request, but the payload of all the data, which is our actual data.

82
00:05:25,450 --> 00:05:33,100
In order to do that, I'm going to pass again with the SNS published message and get the message as

83
00:05:33,100 --> 00:05:36,250
an order request, because we have one more level.

84
00:05:36,250 --> 00:05:43,000
And if you go back to our notification event, Jason file in this JSON file, but if you go to message

85
00:05:43,000 --> 00:05:48,730
after that, we get the actual message that we post from the HTTP post operation.

86
00:05:48,730 --> 00:05:53,470
So we reach these order data in the level of the SNS message.

87
00:05:53,470 --> 00:05:59,890
So if you come back to our indexes so we can successfully get the order request from the incoming record

88
00:05:59,890 --> 00:06:00,920
of the case.

89
00:06:01,150 --> 00:06:01,870
Okay.

90
00:06:01,870 --> 00:06:08,740
So the last step is save order item, but before that we can perform business logic and prepare for

91
00:06:08,740 --> 00:06:09,850
the inventory item.

92
00:06:09,850 --> 00:06:13,510
So for that purpose I'm going to set a partition.

93
00:06:13,510 --> 00:06:17,590
K it is very important before sending request to Dynamo DB.

94
00:06:17,620 --> 00:06:23,350
So basically we will validate for the order request or the request should not be null type should not

95
00:06:23,350 --> 00:06:27,400
be null and also type should not be a shipment requirement.

96
00:06:27,400 --> 00:06:28,810
So think about that.

97
00:06:28,810 --> 00:06:30,400
We have a business rule.

98
00:06:30,400 --> 00:06:38,350
That means if the incoming order type is not a requirement, these should not consume from the inventory

99
00:06:38,350 --> 00:06:39,340
or microservices.

100
00:06:39,340 --> 00:06:42,310
If you go back to our architecture, think about that.

101
00:06:42,310 --> 00:06:45,070
The client order comes in here and the order type.

102
00:06:45,300 --> 00:06:47,160
Is equal the shipment requirement.

103
00:06:47,160 --> 00:06:51,500
So that means this should be consumed by the shipment and deduct from the inventory.

104
00:06:51,510 --> 00:06:55,260
And both these two Lambda Micro sources consume this order.

105
00:06:55,260 --> 00:07:01,240
But if the order type is not equal, the shipment requirement, for example, only notification requirement.

106
00:07:01,260 --> 00:07:07,530
This should consume from notification microservices, not interested in inventory and the shipment microservices.

107
00:07:07,530 --> 00:07:11,210
So we have this kind of condition and business rule.

108
00:07:11,220 --> 00:07:17,520
You can think that we will put these if condition, but we will solve these condition in different way

109
00:07:17,520 --> 00:07:18,660
in the upcoming videos.

110
00:07:18,660 --> 00:07:24,570
But in this video we should put these if statement to make sure that the order type is shipment requirement.

111
00:07:24,570 --> 00:07:28,380
And after that we will perform our business logic in the event of microservices.

112
00:07:28,380 --> 00:07:34,710
For that purpose, I'm going to set a partition K of the order request, which is the inventory table.

113
00:07:34,740 --> 00:07:38,270
This will be the code, as you remember, that the code is the partition.

114
00:07:38,270 --> 00:07:42,540
Okay, so I'll set the partition K as an order request item.

115
00:07:42,540 --> 00:07:45,630
So in the order request we have an item attribute.

116
00:07:45,660 --> 00:07:51,420
If you go back to our common text, you can see the expected payload and here you can see the item and

117
00:07:51,420 --> 00:07:52,980
attribute of the incoming payload.

118
00:07:52,980 --> 00:07:59,400
So we are passing item as a code to make sure that the partition key attribute includes this order request

119
00:07:59,400 --> 00:08:03,330
and assign all these order requests as a matter item.

120
00:08:03,330 --> 00:08:08,670
So that means we have create an inventory item and inventory item will be our new item in the inventory

121
00:08:08,670 --> 00:08:09,210
table.

122
00:08:09,210 --> 00:08:13,050
So after that we will perform the actual save operation.

123
00:08:13,050 --> 00:08:20,790
So I'm going to come here and perform these four step, which is the same order item in dynamic DB.

124
00:08:20,820 --> 00:08:26,010
So basically create a dynamic DB parameter, spacing the table name as inventory and placing the item

125
00:08:26,010 --> 00:08:30,720
as an inventory item that we assign in this point and create inventory item in here.

126
00:08:30,750 --> 00:08:37,320
And after that, we will test these parameters into the put item command and we will use dynamic DB

127
00:08:37,320 --> 00:08:38,040
client object.

128
00:08:38,040 --> 00:08:43,980
So that means we should create the negative client object and also import to put item command statement.

129
00:08:43,980 --> 00:08:48,090
So we forgot the adding output item dynamic DB client.

130
00:08:48,090 --> 00:08:54,840
For that purpose, I'm going to copy the DB client from the other project and paste into our lecture.

131
00:08:54,870 --> 00:08:58,830
And as you can see that I have copy and paste dynamic DB client in here.

132
00:08:58,830 --> 00:09:05,400
So if you come back in here, I would like to import the required statement and we will use Marshall

133
00:09:05,400 --> 00:09:09,120
and Marshall and put item command dynamo, ADB Command Command.

134
00:09:09,120 --> 00:09:12,780
You can also copy all import statements from the previous lecture.

135
00:09:12,780 --> 00:09:19,140
If you go back to our order acknowledgment, you can copy all these import statements regarding to Dynamo

136
00:09:19,140 --> 00:09:22,260
DB and after that, come back to our index case.

137
00:09:22,260 --> 00:09:29,190
And if you go on top of this page, which is the inventory microservice index, paste, import statement

138
00:09:29,190 --> 00:09:35,070
and save this file, this is for the put item command when inserting Dynamo DB Marshall when preparing

139
00:09:35,070 --> 00:09:39,720
the parameters, DB client should be included in every project when interacting dynamic.

140
00:09:39,720 --> 00:09:46,500
DB If you go back to fourth step now, as you can see that all these objects are assigned and comes

141
00:09:46,500 --> 00:09:50,190
correctly, now I have clear the empty spaces.

142
00:09:50,190 --> 00:09:51,150
Okay, very good.

143
00:09:51,150 --> 00:09:55,950
So basically we will finish the development of the inventory microservices.

144
00:09:55,950 --> 00:10:02,280
Now it is time to zip this code and upload inventory microservice on the Lambda Cloud environment.

145
00:10:02,280 --> 00:10:11,210
For that purpose, go to folder page and go to index JS right click reveal in Explorer and go to correct

146
00:10:11,220 --> 00:10:18,480
directory and copy all items, remove commands and the other items can be zipped.

147
00:10:18,480 --> 00:10:26,040
I can remove the notification and shipment event and only project related items I have added right click

148
00:10:26,280 --> 00:10:31,020
send to zip command and this will be the inventory microservices.

149
00:10:31,140 --> 00:10:33,810
So give the name as an inventory.

150
00:10:36,040 --> 00:10:37,300
Microservices.

151
00:10:37,330 --> 00:10:37,690
Okay.

152
00:10:37,690 --> 00:10:40,210
Now please open the console.

153
00:10:40,240 --> 00:10:42,070
Go to inventory.

154
00:10:42,070 --> 00:10:43,270
Lambda function.

155
00:10:43,360 --> 00:10:44,790
Open the lambda function.

156
00:10:44,800 --> 00:10:46,870
Go the inventory microservices.

157
00:10:47,200 --> 00:10:49,500
So please click the upload from.

158
00:10:49,510 --> 00:10:52,660
But before that, I would like to show one more important thing.

159
00:10:52,690 --> 00:10:55,180
Go to configuration and see the permissions.

160
00:10:55,210 --> 00:11:01,140
If you see that we have create a new role which in the name is a role and in this role, including dynamic

161
00:11:01,360 --> 00:11:06,310
interaction, it is very important to put command using to put command for Dynamo DB.

162
00:11:06,340 --> 00:11:10,540
These required permissions should be added the execution role.

163
00:11:11,050 --> 00:11:14,990
If you come back to code and click the upload from and zip file.

164
00:11:15,010 --> 00:11:20,950
So click the upload button and go back to our lecture folder, which is the microservice.

165
00:11:20,950 --> 00:11:22,390
Find the microservice.

166
00:11:22,420 --> 00:11:24,370
Click to open and click this button.

167
00:11:24,670 --> 00:11:32,420
So this will upload function into a cloud environment that we develop in our local Node.js project.

168
00:11:32,440 --> 00:11:41,170
So if you come here and follow the indexes, you will review the code and see some exceptions in here.

169
00:11:41,170 --> 00:11:46,870
Basically, inventory item is not defined, but it is a good practice to check codes, review codes

170
00:11:46,870 --> 00:11:48,640
on a list environment.

171
00:11:48,670 --> 00:11:52,240
We forgot the defined variable, which is the enter item.

172
00:11:52,270 --> 00:11:54,520
Please go back to our widgets to do code.

173
00:11:54,550 --> 00:11:55,650
Go the indexes.

174
00:11:55,660 --> 00:11:59,140
I think we don't need the inventory item variable in here.

175
00:11:59,170 --> 00:12:04,870
We can remove the all inventory item because we have already assigned the code attribute into order

176
00:12:04,870 --> 00:12:05,520
request.

177
00:12:05,530 --> 00:12:09,130
So we will pass the order request for our case.

178
00:12:09,130 --> 00:12:15,850
So please remove the object, assign line and copy order request and paid order request is a matter

179
00:12:15,850 --> 00:12:17,650
item into item dynamic db.

180
00:12:17,680 --> 00:12:22,180
Save these indexes file and after that I will follow the same steps.

181
00:12:22,540 --> 00:12:31,060
Please delete this zip file and right click indexes revealed in explorer copy control a for select all

182
00:12:31,060 --> 00:12:36,040
items, remove commands and event JSON files.

183
00:12:36,070 --> 00:12:38,830
Now we have select only required items.

184
00:12:38,860 --> 00:12:45,400
Right click send to zip file compressed zip folder and write in one.

185
00:12:47,200 --> 00:12:50,650
Inventory is now open.

186
00:12:50,650 --> 00:12:57,430
The adults lambda function go to inventory function and click the upload button, click the zip file,

187
00:12:57,460 --> 00:13:01,840
select the inventory microservices, click the save button.

188
00:13:04,400 --> 00:13:09,830
Look, this will upload our new code into a lambda environment.

189
00:13:10,190 --> 00:13:15,800
So as you can see that this time if we reveal the code, we don't see any exception or error.

190
00:13:16,010 --> 00:13:19,040
Okay, now we are ready for the handle.

191
00:13:19,040 --> 00:13:21,980
Incoming invocations to inventory microservices.
