1
00:00:00,090 --> 00:00:04,890
In this video we are going to develop order acknowledgements microservices.

2
00:00:05,310 --> 00:00:11,220
So before we start, we should understand the business logic of the order, acknowledge and microservices.

3
00:00:11,220 --> 00:00:18,510
Basically the client application will be sent a http post request when any order created this will be

4
00:00:18,510 --> 00:00:25,750
order placed event and this will be sent to post request to the API gateway an API gateway redirect

5
00:00:25,750 --> 00:00:31,740
to request to the order acknowledge microservice lambda function and this will be synchronous invocation.

6
00:00:31,740 --> 00:00:37,170
That means it will require required to return back to synchronous response to the client application.

7
00:00:37,380 --> 00:00:42,750
And inside of this lambda function code in the microservice, the business logic should perform several

8
00:00:42,750 --> 00:00:43,260
things.

9
00:00:43,260 --> 00:00:50,100
For example, this will be create a new item into order dynamically v table and also publish a message

10
00:00:50,100 --> 00:00:56,370
to the Amazon sass to redirect the request to the excuse and downstream services.

11
00:00:56,550 --> 00:01:01,980
So that means we will do several things when we are creating a new item in the order table.

12
00:01:02,010 --> 00:01:09,660
We should generate a new UID so that means we also have generate UID and create any item in order table.

13
00:01:09,660 --> 00:01:11,490
And after that we will publish message.

14
00:01:11,490 --> 00:01:18,480
So that means order equals microservice interact with the dynamic db or and amazon sass to publish message

15
00:01:18,480 --> 00:01:21,420
and accommodate the incoming request from API Gateway.

16
00:01:21,450 --> 00:01:27,330
This will be the synchronous invocation and response baked in synchronous invocation to the API Gateway

17
00:01:27,330 --> 00:01:28,710
and the client application.

18
00:01:28,980 --> 00:01:30,930
Okay, let's get start to the development.

19
00:01:30,930 --> 00:01:37,200
If you open the visual code I have, create a separate folder for the order acknowledgements microservices.

20
00:01:37,200 --> 00:01:41,760
I will develop all codes in this lecture folder and we will follow these steps.

21
00:01:41,760 --> 00:01:45,600
If you open the command text, you can see that we have created index case file.

22
00:01:45,600 --> 00:01:52,230
This will be our lambda function code and I'm going to create a package JSON file and we will make these

23
00:01:52,230 --> 00:01:57,390
folder as Node.js application and we will install the required package in our application.

24
00:01:57,390 --> 00:02:02,760
So according to our business case, we can see that these are the required packages that we are going

25
00:02:02,760 --> 00:02:04,770
to install one by one.

26
00:02:04,770 --> 00:02:11,130
The first package is the UUID for generating new ID before inserting a dynamic DB table.

27
00:02:11,160 --> 00:02:18,180
The second one and the third one is Dynamo DB related SDK package, which is a Dynamo DB client and

28
00:02:18,180 --> 00:02:19,350
Util Dynamo DB.

29
00:02:19,380 --> 00:02:23,790
These are the SDK packages when we interact and perform current operations on Dynamo DB.

30
00:02:23,820 --> 00:02:30,900
And the last one is the SNS client SDK package to publish message to the SNS from the Lambda function.

31
00:02:30,900 --> 00:02:36,360
For that purpose we will run these NPM install commands and after that we will develop our lambda function

32
00:02:36,360 --> 00:02:36,780
code.

33
00:02:36,810 --> 00:02:42,060
Here you can see the several steps that we are going to perform when we are implementing business logic

34
00:02:42,060 --> 00:02:48,420
in the order acknowledges microservices and here you can see the expected payload that comes from the

35
00:02:48,420 --> 00:02:54,420
API gateway client application and we will test these expected payload with publishing sans topic.

36
00:02:54,540 --> 00:02:55,410
Okay, very good.

37
00:02:55,410 --> 00:02:56,490
Let's get started.

38
00:02:56,490 --> 00:03:01,590
In order to that, I I'm going to start with creating index JS file.

39
00:03:01,620 --> 00:03:09,140
If you come back to our index file, we'll put copy and paste a basic implementation of the Lambda 100

40
00:03:09,150 --> 00:03:12,360
method that we will modify during this video.

41
00:03:12,360 --> 00:03:18,270
So if you come back to a command section, the second step is creating package JSON file with this command.

42
00:03:18,300 --> 00:03:23,280
In order to do that, I'm going to open a terminal and locate the correct section folder.

43
00:03:23,280 --> 00:03:25,770
So this will be create a package JSON.

44
00:03:25,770 --> 00:03:32,520
So that means this will be make this lecture folder as a Node.js application that we will deploy to

45
00:03:32,520 --> 00:03:33,510
Lambda function.

46
00:03:33,510 --> 00:03:41,490
For that purpose, I'm going to copy and paste NPM in command, hit enter and this is created our package

47
00:03:41,490 --> 00:03:42,150
JSON file.

48
00:03:42,150 --> 00:03:45,780
And the first thing we should do that we should add a type statement.

49
00:03:45,780 --> 00:03:52,140
We can come here and set the type as a module in order to gain schema script six standards.

50
00:03:52,140 --> 00:03:52,740
Okay.

51
00:03:52,860 --> 00:03:57,420
After adding this type attribute, we should start to install required package.

52
00:03:57,420 --> 00:04:00,210
If you come back to our package JSON file, scroll down.

53
00:04:00,210 --> 00:04:02,610
You can see that I'm going to install one by one.

54
00:04:02,610 --> 00:04:06,450
The first package is the UID.

55
00:04:06,480 --> 00:04:12,000
You can think that we will generate any ID before inserting an item to dynamo db.

56
00:04:12,000 --> 00:04:14,880
So that's why I have installed this uid.

57
00:04:15,060 --> 00:04:21,660
And after that we will install this client as an aspect package and also dynamo DB package.

58
00:04:21,690 --> 00:04:22,740
Think about that.

59
00:04:22,740 --> 00:04:25,560
Interactions into order microservices.

60
00:04:25,560 --> 00:04:31,800
Basically we need to generate ID with the UUID library and we need to save into Dynamo DB table.

61
00:04:31,800 --> 00:04:35,550
And after that we will publish message to Psns to perform these actions.

62
00:04:35,550 --> 00:04:37,890
I am installing required package.

63
00:04:37,890 --> 00:04:41,310
This is the SNS SDK package for JavaScript version three.

64
00:04:43,910 --> 00:04:47,750
And I'm following the dynamite DB package after that.

65
00:04:47,750 --> 00:04:53,400
So let's wait for a while since these SMS package is installed successfully.

66
00:04:53,420 --> 00:05:00,770
Yes, it is installed if you come here and copy from the NPM install client dynamo tb hit enter.

67
00:05:00,800 --> 00:05:04,280
This will be installed required package for dynamic DB package.

68
00:05:04,280 --> 00:05:07,100
And also I'm going to install util dynamo db.

69
00:05:07,130 --> 00:05:13,010
You have already familiar with the dynamo db sdk package, but this time we will do several things in

70
00:05:13,010 --> 00:05:14,340
the lambda function.

71
00:05:14,500 --> 00:05:14,800
Okay.

72
00:05:14,810 --> 00:05:20,210
If you go back to package JSON file and see the dependencies, we can see that.

73
00:05:20,210 --> 00:05:26,030
Make sure that you have four dependencies for our application uid dynamo, adb, util, dynamo, adb

74
00:05:26,030 --> 00:05:27,110
and SNS.

75
00:05:27,460 --> 00:05:29,780
Okay, now it is time to develop our lambda function code.

76
00:05:29,780 --> 00:05:32,810
We should plan our development for our order.

77
00:05:32,820 --> 00:05:34,460
Acknowledge microservices.

78
00:05:34,460 --> 00:05:41,180
So that means you should check the architecture and make sure that and plan your to do things with code.

79
00:05:41,210 --> 00:05:44,810
I'm going to open our index JS file.

80
00:05:44,840 --> 00:05:52,070
The first thing I would like to change the our method buddy, you can come here and change the exports

81
00:05:52,070 --> 00:05:55,040
handler to the export code code standard.

82
00:05:55,070 --> 00:06:00,940
That means we will use Postscript six standard and after that we will implement these to do logic.

83
00:06:00,950 --> 00:06:08,210
So as you remember that we when we get the request from synchronously, we should have back to response

84
00:06:08,210 --> 00:06:09,950
synchronized to API gateway.

85
00:06:09,950 --> 00:06:17,360
So that's why this should be stay in our method and after that when the request comes to order acknowledge

86
00:06:17,450 --> 00:06:18,410
microservices.

87
00:06:18,410 --> 00:06:25,040
The first thing we should do that will let the incoming event and also we will perform the get request

88
00:06:25,040 --> 00:06:27,140
but the payload information.

89
00:06:27,140 --> 00:06:29,510
So basically let them write for the zero.

90
00:06:29,990 --> 00:06:32,270
What the incoming event.

91
00:06:33,660 --> 00:06:36,500
And after that we will get the request by the payload.

92
00:06:36,510 --> 00:06:43,740
We will get the request but the payload that means or order acknowledge microservice triggered from

93
00:06:43,740 --> 00:06:50,010
the API gateway and we will get the event JSON object and get the body information to reach the incoming

94
00:06:50,010 --> 00:06:52,650
payload of the post HTTP post operation.

95
00:06:53,160 --> 00:06:59,340
After that, when we get the incoming payload is you remember that if you open the command text, we

96
00:06:59,340 --> 00:07:01,280
will expect this kind of payload.

97
00:07:01,290 --> 00:07:07,740
And when we got this kind of payload, we will start our operations, which means we will generate the

98
00:07:07,740 --> 00:07:13,410
ID with using UID library and prepare the payload with including the ID information.

99
00:07:13,410 --> 00:07:21,690
And after that we will publish message to the SNS with using SNS SDK package and after publishing the

100
00:07:21,690 --> 00:07:29,190
message we will save the order item into Dynamo DB table with using Dynamo DB SDK package and the last

101
00:07:29,190 --> 00:07:31,950
item we will return back the synchronous response.

102
00:07:32,310 --> 00:07:38,880
You can come here and say that return back synchronous order payload which generated ID to the API gateway.

103
00:07:39,100 --> 00:07:39,410
Okay.

104
00:07:39,420 --> 00:07:46,970
These are the archetypes that we are going to implement in this lambda function for our order solutions

105
00:07:47,040 --> 00:07:47,850
microservices.

106
00:07:47,880 --> 00:07:48,020
Okay.

107
00:07:48,060 --> 00:07:53,730
Now we can start the implementation part for that purpose, open the pseudocode and go to index case

108
00:07:53,740 --> 00:07:54,270
file.

109
00:07:54,300 --> 00:07:57,870
So I'm going to start with the cache below.

110
00:07:58,170 --> 00:08:03,870
So let me come here and cover we to try to look in the tribal look.

111
00:08:03,870 --> 00:08:06,210
We will perform these actions one by one.

112
00:08:06,300 --> 00:08:11,670
I will start with the first action which is the world date incoming event.

113
00:08:11,670 --> 00:08:17,880
So we will check the event, the HTTP method, and after that we will throw an exception.

114
00:08:17,880 --> 00:08:24,390
So that means we have only accept the HTTP post request when creating a new order item.

115
00:08:24,390 --> 00:08:29,150
And the first thing we have checked that the incoming event async method should be paused.

116
00:08:29,160 --> 00:08:31,500
Which event comes from the API gateway?

117
00:08:31,530 --> 00:08:37,230
If it comes from the HTTP get put or other methods, we will throw an exception.

118
00:08:37,230 --> 00:08:40,500
This is the first step and we will validate the incoming event.

119
00:08:40,650 --> 00:08:46,890
And after that the another step is the get request payload.

120
00:08:46,920 --> 00:08:54,360
We will get request payload from the incoming event, but the operation, you can see that we basically

121
00:08:54,360 --> 00:08:59,430
get the event that the information maybe you remember in the API gateway triggered the lambda function.

122
00:08:59,430 --> 00:09:04,110
We have a body information and we can pass this information, get the odd request.

123
00:09:04,110 --> 00:09:10,710
So this is the this is the JSON object that we are sending from the our payload of the HTTP post operation.

124
00:09:10,710 --> 00:09:17,820
So I have also put a validation when we get the order request, basically this order occurs should not

125
00:09:17,820 --> 00:09:20,340
be null and the type also should not be null.

126
00:09:20,340 --> 00:09:21,960
If one of the item is null.

127
00:09:21,990 --> 00:09:26,370
I have throw an exception because type is important for our action.

128
00:09:26,370 --> 00:09:31,470
So as you remember that in the command text we will send this kind of JSON object.

129
00:09:31,470 --> 00:09:37,470
So we will check the whole object and also we will check the type and we will see the shipment requirement

130
00:09:37,470 --> 00:09:41,490
for the upcoming videos putting some business logic in the type attribute.

131
00:09:41,520 --> 00:09:44,550
That's why I have checked these two attributes.

132
00:09:44,670 --> 00:09:50,850
Okay, we will finish the first step and now we are going to focus on the second step, which is the

133
00:09:50,850 --> 00:09:53,730
generate ID with using UUID library.

134
00:09:53,730 --> 00:10:00,480
So before we start, we should go to the import statement because when we are using UUID library, we

135
00:10:00,480 --> 00:10:05,580
basically put a required import statement for using UID version for.

136
00:10:05,580 --> 00:10:12,750
So please add this import statement after come back to our second step so we will generate ID.

137
00:10:12,750 --> 00:10:16,650
So in order to generate the ID, this is the steps that we are going to do.

138
00:10:16,650 --> 00:10:25,320
We basically use uid v for method and get to order ID and set the order request id as order id.

139
00:10:25,440 --> 00:10:29,580
Maybe you remember we have a partition k for all the table, which is the id.

140
00:10:29,580 --> 00:10:35,550
So that's why I have defined any attribute as ID and pasting these needed generated order id.

141
00:10:35,850 --> 00:10:36,220
Okay.

142
00:10:36,240 --> 00:10:36,840
Very good.

143
00:10:36,840 --> 00:10:42,900
So after that we will perform our actual business logic in the third and fourth step, which is to publish,

144
00:10:42,900 --> 00:10:45,480
to source and save into dynamic DV table.

145
00:10:45,630 --> 00:10:53,550
I am skipping this three and four part and focus on the another part, which is the five step.

146
00:10:53,790 --> 00:10:57,150
And if you come back to fifth one.

147
00:10:58,360 --> 00:11:03,310
We basically turned back to sync order payload which generated ID to the API gateway.

148
00:11:03,460 --> 00:11:10,030
In order to do that, I'm going to prepare any return statement in here, which is a success return

149
00:11:10,030 --> 00:11:10,690
statement.

150
00:11:10,690 --> 00:11:14,800
So this code is 200 body is a successful message.

151
00:11:14,800 --> 00:11:20,650
And also I'm passing the data that will come from the are this three and four operation.

152
00:11:20,740 --> 00:11:28,270
And if you scroll down now, we can remove these response of the method because we will return back

153
00:11:28,270 --> 00:11:31,450
the success response after finished all these steps.

154
00:11:31,450 --> 00:11:39,720
And in the case we look, we have to handle the error and also implement the error return statement

155
00:11:39,730 --> 00:11:41,770
if you come here and.

156
00:11:42,580 --> 00:11:43,780
Write down this method.

157
00:11:43,780 --> 00:11:49,990
This will be basically logging the error if this happened in the error in the tribal log and after that

158
00:11:49,990 --> 00:11:56,080
return back to API Gateway which scored 500 and the message with error details.

159
00:11:56,110 --> 00:11:57,670
Okay, very good.

160
00:11:57,820 --> 00:12:05,200
As you can see that we have developed our function without implementing third and the fourth step.

161
00:12:05,200 --> 00:12:07,570
But you understand the idea now.

162
00:12:07,570 --> 00:12:11,590
We will continue with the third and fourth step in the next video.
