1
00:00:00,360 --> 00:00:07,530
In this video, we are going to continue developing our product microservices as a lambda function and

2
00:00:07,530 --> 00:00:09,110
we will continue our development.

3
00:00:09,120 --> 00:00:16,620
If you open the visual code, we can come here and see that index JS is empty and before starting to

4
00:00:16,620 --> 00:00:23,160
developing our lambda function, we have to add a new model which is the dynamic DB client JavaScript

5
00:00:23,160 --> 00:00:30,360
module and it is good practice to create a separate Node.js module when connecting to dynamic DB object

6
00:00:30,390 --> 00:00:33,240
with using the SDK libraries in order to do that.

7
00:00:33,240 --> 00:00:39,120
In the last section, we use lots of time, which is the DB client JS file.

8
00:00:39,120 --> 00:00:48,030
I'm copy this file and come here and paste to my latest folder which is the product, product crud microservices

9
00:00:48,030 --> 00:00:49,200
and paste in here.

10
00:00:49,290 --> 00:00:49,650
Okay.

11
00:00:49,650 --> 00:00:54,210
Now I can close these files and focus on these DB client.

12
00:00:54,210 --> 00:01:02,160
JS so that means we create a new JavaScript file under our folder and this is the name DB client that

13
00:01:02,160 --> 00:01:06,750
means dynamic DB client object and develop this line of codes.

14
00:01:06,750 --> 00:01:08,040
So what is this line?

15
00:01:08,040 --> 00:01:14,580
Of course we basically use SDK and create any dynamic DB client object to interact with the dynamic

16
00:01:14,580 --> 00:01:17,760
DB API and specifying the region information.

17
00:01:17,760 --> 00:01:26,250
You can change region information as poor as per your cloud environment and we export the DB client

18
00:01:26,250 --> 00:01:27,600
to our application.

19
00:01:27,630 --> 00:01:33,570
You have already know and familiar with these codes and this is the best practice to create database

20
00:01:33,570 --> 00:01:36,270
connection before Lambda Handle method execution.

21
00:01:36,270 --> 00:01:42,000
By this way, it reduced the execution time and reduced the concurrency of lambda function and of course

22
00:01:42,000 --> 00:01:48,150
it reduce the costing pay as you go model because the execution time reduced dramatically if we have

23
00:01:48,150 --> 00:01:49,860
already database connection.

24
00:01:49,980 --> 00:01:56,460
So that's why it is best practice to exporting and creating separate node modules for the dynamic DB

25
00:01:56,460 --> 00:01:57,960
database connections.

26
00:01:58,200 --> 00:01:58,840
Okay.

27
00:01:58,860 --> 00:02:05,170
We can start our actual business code development into the index JS file edge.

28
00:02:05,190 --> 00:02:11,000
Note that index JS includes our lambda function handle method and we have verified that this handle

29
00:02:11,010 --> 00:02:13,140
method invoked by the API gateway.

30
00:02:13,290 --> 00:02:17,310
So if you remember to our architecture, we can check the Oracle architecture.

31
00:02:17,310 --> 00:02:23,880
We are going to implement synchronous serverless CRUD operations with using the API, Gateway, Lambda

32
00:02:23,880 --> 00:02:26,310
and Dynamo DB for product microservices.

33
00:02:26,310 --> 00:02:29,970
And this invocation will be the synchronous communication type.

34
00:02:30,870 --> 00:02:36,990
So that means our product lambda function will trigger from the API gateway and we have already exposed

35
00:02:37,020 --> 00:02:43,770
get post delete and put APIs for product root resource type into Amazon API Gateway.

36
00:02:43,800 --> 00:02:49,700
Now it is time to catch these events from our Lambda function and process them according to our current

37
00:02:49,770 --> 00:02:50,640
requirements.

38
00:02:50,640 --> 00:02:57,360
That means we should know that how a API gateway triggered our lambda function which requires JSON trigger

39
00:02:57,360 --> 00:02:58,770
to our lambda function.

40
00:02:58,800 --> 00:03:04,800
According to this JSON event object, we will catch the HTTP URL and method and develop our logic into

41
00:03:04,800 --> 00:03:06,150
product lambda function.

42
00:03:06,150 --> 00:03:09,930
Now we can start our actual code for product microservices.

43
00:03:09,930 --> 00:03:11,940
I will start writing the code.

44
00:03:11,940 --> 00:03:14,910
That means we will put the command about what we will do.

45
00:03:14,940 --> 00:03:23,040
Please open the index and now I'm going to develop our lambda function code or we can copy from the

46
00:03:23,040 --> 00:03:26,940
lambda function into the a cloud environment.

47
00:03:26,940 --> 00:03:30,420
But this is the same architecture you can see in here.

48
00:03:30,660 --> 00:03:39,840
Basically this is the minimum code for the lambda function which generates from the ADA itself and we

49
00:03:39,840 --> 00:03:46,620
have added the log operation at the beginning of the method and also I have added this to the statement

50
00:03:46,620 --> 00:03:52,500
which is accommodate the incoming event from the API gateway and perform CRUD operation and we will

51
00:03:52,500 --> 00:03:54,810
look at the event HTTP method.

52
00:03:55,080 --> 00:04:01,020
We can compare this method with the ADA environment, open the product function, come the code section

53
00:04:01,530 --> 00:04:04,800
and we you can copy and paste in here.

54
00:04:04,800 --> 00:04:11,580
But this is the exact same method body in our index file of the local folder structure.

55
00:04:11,580 --> 00:04:15,360
So that's why we will develop in here and upload after that.

56
00:04:15,390 --> 00:04:20,520
After finished our development, we will upload this method to the ADA Cloud environment.

57
00:04:20,670 --> 00:04:25,620
But before we proceed, I would like to perform some refactoring on this method.

58
00:04:25,650 --> 00:04:33,930
Since we have activated Script six and use Node.js 16 for our lambda function, we can refactor our

59
00:04:33,930 --> 00:04:40,500
lambda function implementation on indexes because in the package JSON file, we are using this type

60
00:04:40,500 --> 00:04:41,280
module statement.

61
00:04:41,280 --> 00:04:45,750
So that means we can use execute six standards into index.

62
00:04:45,750 --> 00:04:53,880
JS So with the ActionScript six standards, for example, we can refactor with these exports method.

63
00:04:53,880 --> 00:04:59,610
So basically my script has an export statement and we can define.

64
00:05:00,500 --> 00:05:06,470
These methods with this way it is exactly the same and working fine with the lambda function if we are

65
00:05:06,470 --> 00:05:08,930
using the Amos six.

66
00:05:10,380 --> 00:05:14,220
And using the Node.js 16 plus.

67
00:05:14,760 --> 00:05:19,320
So as you can see that we'd executed six and Node.js 16.

68
00:05:19,800 --> 00:05:28,170
We can use export const handler method and we use export statement instead of export 100 method.

69
00:05:28,290 --> 00:05:31,620
You can compare it with lambda function code on a machine console.

70
00:05:31,650 --> 00:05:39,660
This seems very minor change, but we will use Postscript six and that's why we start with the changing

71
00:05:39,690 --> 00:05:41,430
our method body definition.

72
00:05:41,700 --> 00:05:47,310
So we are going to develop our product lambda function with the latest Node.js runtime and the latest

73
00:05:47,360 --> 00:05:55,230
ActionScript six versions because Node.js and executed gives us very powerful benefits that we can use

74
00:05:55,230 --> 00:05:58,050
in the concurrency and avoiding code.

75
00:05:58,050 --> 00:06:00,530
Start issues on lambda function.

76
00:06:00,540 --> 00:06:06,990
So now before developing this function, the only thing we should check that the incoming event from

77
00:06:06,990 --> 00:06:12,560
the API gateway is remember that API gateway triggered product lambda function with the events.

78
00:06:12,570 --> 00:06:16,140
So let's collect these events and start development after that.
