1
00:00:00,240 --> 00:00:06,420
In this video, we are going to develop the function for asynchronous invocation from Amazon S3.

2
00:00:06,690 --> 00:00:08,010
Let's get started.

3
00:00:08,040 --> 00:00:09,780
Please open to decode.

4
00:00:09,780 --> 00:00:12,900
And I have created separate section and lecture folder.

5
00:00:12,990 --> 00:00:18,390
And from now on, we will develop this lecture folder and this will be the our Node.js application.

6
00:00:18,570 --> 00:00:23,970
So in this video, we will develop our lambda function and we have to interaction.

7
00:00:23,970 --> 00:00:27,360
First of all, get bucket detail from Amazon S3.

8
00:00:27,390 --> 00:00:33,310
And after that, we will write Dynamo DB Table with details of the S3 bucket and object theme.

9
00:00:33,330 --> 00:00:37,110
So these are the two interactions to develop our lambda function.

10
00:00:37,110 --> 00:00:39,210
I have created indexes in here.

11
00:00:39,210 --> 00:00:44,910
So in the index case you can see the basic 100 method, which is the lambda function hundred method

12
00:00:44,910 --> 00:00:48,980
and this is the boilerplate code that we can develop together.

13
00:00:48,990 --> 00:00:54,330
So as you can see that in the first line of code we have locked the incoming event and after that we

14
00:00:54,330 --> 00:00:56,790
return back to 200 success response.

15
00:00:56,790 --> 00:01:02,410
So in the to do part, we will perform our actual logic, which is the get information about the bucket

16
00:01:02,410 --> 00:01:05,790
details and write these details into Dynamo DB table.

17
00:01:06,210 --> 00:01:09,120
For that purpose, let me write with the zero code.

18
00:01:09,120 --> 00:01:15,540
So that means we will first of all, with the incoming event, we will list the objects into the bucket.

19
00:01:15,540 --> 00:01:22,080
So that means it is required the S3 integration from the Lambda function to get objects from the bucket

20
00:01:22,080 --> 00:01:24,840
because in the event we have bucket information.

21
00:01:24,840 --> 00:01:31,170
So after that we will write these object details on Dynamo DB table.

22
00:01:31,170 --> 00:01:36,960
So these are the two steps that we are going to develop in this lambda function and we will start with

23
00:01:36,960 --> 00:01:38,940
the listing object into bucket.

24
00:01:39,210 --> 00:01:44,160
So for list object into bucket, we will use a HDC JavaScript library.

25
00:01:44,160 --> 00:01:51,360
So if you open the our S3 client object, we will use the list object command.

26
00:01:51,360 --> 00:01:57,510
If you search for the list object command, I'm going to use version two command because version two

27
00:01:57,510 --> 00:01:58,560
is the latest one.

28
00:01:58,560 --> 00:02:04,350
If you open the list object, version two command, you can see all the details in here that we will

29
00:02:04,350 --> 00:02:07,410
use for listing objects into given bucket name.

30
00:02:07,410 --> 00:02:11,820
So basically it is returning all objects in the bucket with each request.

31
00:02:11,850 --> 00:02:17,520
We can use the request parameters as the selection criteria and we can use these operations.

32
00:02:17,520 --> 00:02:20,010
We must have the read access.

33
00:02:20,010 --> 00:02:22,860
You can read these very important word.

34
00:02:23,010 --> 00:02:27,990
In order to use this operation, we have to have read access to the bucket.

35
00:02:28,020 --> 00:02:34,320
This is very important, which is the also performing and perform to list bucket action.

36
00:02:34,320 --> 00:02:40,350
To do that, we should give this list bucket item policy into our permissions of the lambda function.

37
00:02:40,590 --> 00:02:48,570
If you don't give this permission when we run these invoke lambda function, this will be throw an exception

38
00:02:48,570 --> 00:02:55,140
about the action required permissions access not exception and access tonight about the list bucket

39
00:02:55,140 --> 00:02:55,980
operations.

40
00:02:55,980 --> 00:02:59,760
We should give this permission into our lambda function for that purpose.

41
00:02:59,760 --> 00:03:04,590
Please go to lambda function, go to S3 function configuration permission.

42
00:03:04,590 --> 00:03:10,410
If you scroll down and if you select the resource summary dropdown box and select the Amazon S3, you

43
00:03:10,410 --> 00:03:14,100
can see that we have only get object action.

44
00:03:14,100 --> 00:03:21,780
But for our business case we will listing S3 objects and we also need to list object action that is

45
00:03:21,780 --> 00:03:23,760
explained in the documentation.

46
00:03:24,120 --> 00:03:30,030
So for that purpose, I'm going to at least bucket requirement permission into our role.

47
00:03:30,030 --> 00:03:36,300
And in order to do that, I'm going to right click and open new tab for the role name and this will

48
00:03:36,300 --> 00:03:43,350
be open the IAM role and in the I am role page, I'm going to go to permission policies.

49
00:03:43,350 --> 00:03:47,850
You can see the permissions and this is my role name of our lambda function.

50
00:03:47,850 --> 00:03:53,370
If you scroll down you can see the permission policies and we have three policy, one for the Lambda

51
00:03:53,370 --> 00:03:58,200
Basic execution role, which is the default one and one for the Lambda Microservice execution role,

52
00:03:58,200 --> 00:04:03,360
which is the dynamic DB cloud operations and one for the Lambda S3 execution role.

53
00:04:03,390 --> 00:04:09,360
It is only including get object, but we also need to list bucket list bucket permissions.

54
00:04:09,360 --> 00:04:09,620
Okay.

55
00:04:09,690 --> 00:04:11,490
Now we can add the required permission.

56
00:04:11,490 --> 00:04:14,040
You can see in here there's a permission button.

57
00:04:14,040 --> 00:04:17,100
If you click this button, you can see the attached policies.

58
00:04:17,100 --> 00:04:18,840
I'm going to attach policy.

59
00:04:18,840 --> 00:04:21,990
So in here I'm going to search for the S3.

60
00:04:21,990 --> 00:04:27,660
You can search for the S3 and here you can see the Amazon S3 return access.

61
00:04:27,660 --> 00:04:33,660
If you expand this policy, you can see that it is called the list, object, list, bucket and so on.

62
00:04:33,660 --> 00:04:37,920
All these actions is handled in the Amazon S3 Return Access.

63
00:04:37,920 --> 00:04:43,680
So please select these Amazon S3 Return Access and click the attach policy.

64
00:04:44,740 --> 00:04:47,690
So it is successfully attached to my role.

65
00:04:47,710 --> 00:04:54,640
If you scroll down, verify that Amazon S3 don't access attached and these including the list object

66
00:04:54,640 --> 00:04:57,370
and list bucket permissions in our world.

67
00:04:57,500 --> 00:04:58,720
Okay, very good.

68
00:04:58,720 --> 00:05:06,310
So now we can go back to our lambda function if you refresh the page and come here as three functions

69
00:05:06,310 --> 00:05:08,830
and go to configuration and permission.

70
00:05:08,830 --> 00:05:15,580
This time, if you select the Amazon three, you can see that we have also get and list object resources

71
00:05:15,580 --> 00:05:16,690
added successfully.

72
00:05:16,690 --> 00:05:22,240
Okay, now we can focus on the code part, but before that let's review the documentation.

73
00:05:22,240 --> 00:05:27,880
If you go back to the documentation, we are going to use list object version two command for listing

74
00:05:27,880 --> 00:05:30,910
object into bucket and we give required permission.

75
00:05:30,910 --> 00:05:33,610
If you scroll down, we will see the example usage.

76
00:05:33,610 --> 00:05:39,960
Here you can see the command and this is the our client is three package and we will use is modules

77
00:05:40,000 --> 00:05:44,290
to import statement and the rest of the code is very same.

78
00:05:44,290 --> 00:05:50,830
We are creating a client object and sent client object to this command to perform our business logic

79
00:05:50,950 --> 00:05:52,690
and passing the parameters.

80
00:05:52,690 --> 00:05:56,980
These are the very same and you can check the input parameter in here.

81
00:05:56,980 --> 00:06:01,300
Basically, we will give this kind of properties when requesting list object.

82
00:06:01,300 --> 00:06:03,790
The important parameter of course, the bucket name.

83
00:06:04,030 --> 00:06:06,670
Okay, let's start to developing our application.

84
00:06:06,670 --> 00:06:12,790
If you open the Visual Studio code, you can think that this will be the our Node.js application for

85
00:06:12,790 --> 00:06:13,600
that purpose.

86
00:06:13,630 --> 00:06:21,520
In this folder structure, I'm going to create a package JSON file with running the NPM init via command.

87
00:06:21,550 --> 00:06:28,540
Please open a terminal and go to record section and go to record lecture.

88
00:06:28,540 --> 00:06:36,220
File lecture will be 250 and now I'm going to run npm init dash.

89
00:06:36,220 --> 00:06:40,720
Why this will be create a package JSON file with the default information.

90
00:06:40,720 --> 00:06:46,900
You can see in here we have created a package JSON file and the first thing we should do that the adding

91
00:06:46,930 --> 00:06:54,520
type module in here, come here and click the type and set the module in order to activate script six

92
00:06:54,520 --> 00:06:58,600
modules and Script six standards, we should add type module.

93
00:06:58,600 --> 00:07:01,720
Please don't forget this and save this package JSON file.

94
00:07:01,840 --> 00:07:04,320
Now we can install required packages.

95
00:07:04,330 --> 00:07:09,190
If you come back to our list object, you can come to S3 client.

96
00:07:09,220 --> 00:07:17,200
See that we are going to use client S3 package for the SDK JavaScript version three copy this NPM,

97
00:07:17,200 --> 00:07:23,860
install command and go back to our visual code and please make sure that you are in the correct folder

98
00:07:23,860 --> 00:07:26,170
structure and paste this command in here.

99
00:07:26,170 --> 00:07:32,230
This will be install the required S3 package into our package JSON file and these are Node.js application

100
00:07:32,230 --> 00:07:34,510
that we are going to deploy our lambda function.

101
00:07:35,250 --> 00:07:42,900
So after finishing this install operation, we will see the package JSON file and check the dependencies.

102
00:07:44,360 --> 00:07:44,830
Okay.

103
00:07:44,840 --> 00:07:50,690
If you scroll down in the package JSON file, you can see that we have successfully instal required

104
00:07:50,840 --> 00:07:54,230
a client package from the our SDK.

105
00:07:54,260 --> 00:08:00,990
Now it is time to create S3 client separate Node.js module to perform our actions.

106
00:08:01,010 --> 00:08:06,140
Please create S3 Client JavaScript file into our directory.

107
00:08:06,620 --> 00:08:17,240
Come here and create a neve S3 client that JavaScript file and I'm going to develop this file with this

108
00:08:17,240 --> 00:08:18,140
line of codes.

109
00:08:18,170 --> 00:08:23,780
As you remember that we are using S3 client from these HDC packets and we are in the region, US is

110
00:08:24,320 --> 00:08:29,920
and basically we are creating a new client and export this client into the separate Node.js model.

111
00:08:29,930 --> 00:08:35,540
So this helps to decrease the package size and increase the call start issues.

112
00:08:35,660 --> 00:08:38,930
Okay, now we can focus on developing our function.

113
00:08:39,170 --> 00:08:43,540
As you remember that we have two items that we will interacting.

114
00:08:43,700 --> 00:08:48,360
S3 Bucket for listing object into bucket and we will write on the DB table.

115
00:08:48,380 --> 00:08:53,690
We have installed the required bucket client object and now we can develop this part.

116
00:08:53,690 --> 00:08:56,990
In the next video we will implement list object into bucket.
