1
00:00:00,180 --> 00:00:04,200
In this video we are going to create a lambda function for our product.

2
00:00:04,200 --> 00:00:11,490
Microservices is lambda is event driven computing service and event driven computing response to event.

3
00:00:11,490 --> 00:00:14,580
When the event source happens, it triggered the lambda function.

4
00:00:14,970 --> 00:00:21,470
The event source could be a request to end point, which will be go through the using the API gateway.

5
00:00:21,480 --> 00:00:24,720
So we create a lambda function for the back end of our API.

6
00:00:24,750 --> 00:00:30,720
This lambda function performs CRUD operations over the Dynamo DB and the function used events from the

7
00:00:30,720 --> 00:00:36,250
API gateway to determine how to interact with Dynamo DB in order to create a lambda function.

8
00:00:36,270 --> 00:00:39,060
Please go to the A mission console.

9
00:00:39,090 --> 00:00:41,580
Go to lambda functions and create function.

10
00:00:41,970 --> 00:00:48,300
So we will create function alter from scratch and the function name will be the product function runtime

11
00:00:48,300 --> 00:00:52,600
will be Node.js and leave as it is for the default complications.

12
00:00:52,620 --> 00:00:58,080
The only thing we should do that under the permission, please click the change default execution role

13
00:00:58,080 --> 00:01:04,440
and we are going to create a new role from the policy templates because we will interact with the Dynamo

14
00:01:04,440 --> 00:01:04,980
DB.

15
00:01:05,010 --> 00:01:10,470
So give the role name as a product role and the policy templates should be the.

16
00:01:11,340 --> 00:01:15,470
Simple microservices, simple microservice permission.

17
00:01:15,480 --> 00:01:21,150
This is important because these provide to interact with dynamic DB and give required permission to

18
00:01:21,150 --> 00:01:22,170
lambda function.

19
00:01:22,380 --> 00:01:28,470
So if you scroll down and click the create function, these will be create our function.

20
00:01:28,470 --> 00:01:34,950
But see that there is an exemption which is set that role with the name product will already exist.

21
00:01:34,950 --> 00:01:40,350
So make sure that this rule is including simple microservice permission come to iam.

22
00:01:41,400 --> 00:01:42,480
Court rules.

23
00:01:45,480 --> 00:01:49,290
And if you search with the product roll and if you open the product roll.

24
00:01:50,220 --> 00:01:56,620
You can see the permissions permissions are including Lambda Microservice execution role, which including

25
00:01:56,640 --> 00:01:57,960
the Dynamo DB interaction.

26
00:01:57,960 --> 00:02:02,220
So that means we can use this product role in our function.

27
00:02:02,220 --> 00:02:10,110
So if you come back to Lambda function, I can use an existing role so we can choose existing role as

28
00:02:10,110 --> 00:02:13,650
a product role and we can continue with this role.

29
00:02:13,650 --> 00:02:15,300
So please create function.

30
00:02:18,060 --> 00:02:22,560
As you can see that we have successfully create our product lambda function which is simple Microsoft's

31
00:02:22,560 --> 00:02:29,550
role that provide to interact crowd operations on dynamic DB table you can verify that in the configurations

32
00:02:29,550 --> 00:02:35,070
go to permissions and see the resource summary in Amazon Dynamic DB, we have five actions.

33
00:02:35,100 --> 00:02:38,250
These actions are CRUD operations on Dynamic DB.

34
00:02:38,280 --> 00:02:45,450
So our role is OC for our lambda function, which is the product role, including the simple microservice

35
00:02:45,450 --> 00:02:47,790
roles from interacting dynamic.

36
00:02:47,790 --> 00:02:52,500
DB Now we can configure our lambda function with additional configurations.

37
00:02:52,500 --> 00:02:55,680
You can click the configuration type and see the details.

38
00:02:55,680 --> 00:02:57,570
We can see the general configuration.

39
00:02:57,570 --> 00:03:03,210
This time we will follow the best practices when developing our CRUD operations on this lambda function.

40
00:03:03,210 --> 00:03:07,840
So one of the best practice is using environment variables as soon as possible.

41
00:03:07,860 --> 00:03:13,200
For that reason, I would like to define some environment variables to get dynamic DB related parameters

42
00:03:13,200 --> 00:03:17,730
as an environment variables, if you come here and click the environment variables, you can see that

43
00:03:17,730 --> 00:03:19,200
there is no environment for this.

44
00:03:19,200 --> 00:03:22,410
So click the edit button and add an environment variable.

45
00:03:22,410 --> 00:03:27,360
So I would like to add environment variable as a primary key, which is the ID.

46
00:03:27,570 --> 00:03:32,280
So this is our primary key of ID of the product table.

47
00:03:32,310 --> 00:03:34,500
ID is a partition and primary key.

48
00:03:34,500 --> 00:03:40,140
And I'm going to add additional environment which is the Dynamo DB table name and give the table name

49
00:03:40,170 --> 00:03:45,900
as a product so these parameters could be changed without changing any code on Lambda function.

50
00:03:45,900 --> 00:03:50,460
So that's why we will define as environment variable on lambda functions.

51
00:03:50,610 --> 00:03:57,870
So click the say button and after that you verify that environments very environment variables located

52
00:03:57,870 --> 00:03:58,530
in here.

53
00:03:58,530 --> 00:04:00,210
So come to code part.

54
00:04:00,210 --> 00:04:06,510
So that means we can use a mime, we can get environment variables into the code part of lambda function.

55
00:04:06,510 --> 00:04:13,380
But before implementing these actual logic, I would like to add a log path into our lambda function.

56
00:04:13,380 --> 00:04:19,830
As you remember that this is our generic code which is basically log incoming event from the API gateway

57
00:04:19,830 --> 00:04:22,020
and after that I don't implement any code.

58
00:04:22,020 --> 00:04:27,660
Now we will implement in the upcoming sections, but I'm leaving as it is for the response part and

59
00:04:27,660 --> 00:04:29,760
deploy the function.

60
00:04:31,050 --> 00:04:33,110
For now, it is enough to implement handler code.

61
00:04:33,120 --> 00:04:38,670
This is only return message with 200 success code, but we will develop this function interacting with

62
00:04:38,670 --> 00:04:42,360
product dynamic DB table in order to perform CRUD operations soon.

63
00:04:42,600 --> 00:04:48,270
And this code implementation, all this interaction with product dynamic DB table will be handled by

64
00:04:48,270 --> 00:04:51,840
using a SDK JavaScript version three.

65
00:04:51,990 --> 00:04:55,110
So I'm not going to jump at this SDK now.

66
00:04:55,140 --> 00:04:59,430
Instead of that, let's focus on the infrastructure, create operations.

67
00:04:59,580 --> 00:05:05,370
As you remember that if you check our architecture, we basically create product table and create product

68
00:05:05,370 --> 00:05:06,360
microservices.

69
00:05:06,360 --> 00:05:11,310
And we should also need to create API, Gateway Rest API.
