In this video, we are going to develop the function for asynchronous invocation from Amazon S3.
Let's get started.
Please open to decode.
And I have created separate section and lecture folder.
And from now on, we will develop this lecture folder and this will be the our Node.js application.
So in this video, we will develop our lambda function and we have to interaction.
First of all, get bucket detail from Amazon S3.
And after that, we will write Dynamo DB Table with details of the S3 bucket and object theme.
So these are the two interactions to develop our lambda function.
I have created indexes in here.
So in the index case you can see the basic 100 method, which is the lambda function hundred method
and this is the boilerplate code that we can develop together.
So as you can see that in the first line of code we have locked the incoming event and after that we
return back to 200 success response.
So in the to do part, we will perform our actual logic, which is the get information about the bucket
details and write these details into Dynamo DB table.
For that purpose, let me write with the zero code.
So that means we will first of all, with the incoming event, we will list the objects into the bucket.
So that means it is required the S3 integration from the Lambda function to get objects from the bucket
because in the event we have bucket information.
So after that we will write these object details on Dynamo DB table.
So these are the two steps that we are going to develop in this lambda function and we will start with
the listing object into bucket.
So for list object into bucket, we will use a HDC JavaScript library.
So if you open the our S3 client object, we will use the list object command.
If you search for the list object command, I'm going to use version two command because version two
is the latest one.
If you open the list object, version two command, you can see all the details in here that we will
use for listing objects into given bucket name.
So basically it is returning all objects in the bucket with each request.
We can use the request parameters as the selection criteria and we can use these operations.
We must have the read access.
You can read these very important word.
In order to use this operation, we have to have read access to the bucket.
This is very important, which is the also performing and perform to list bucket action.
To do that, we should give this list bucket item policy into our permissions of the lambda function.
If you don't give this permission when we run these invoke lambda function, this will be throw an exception
about the action required permissions access not exception and access tonight about the list bucket
operations.
We should give this permission into our lambda function for that purpose.
Please go to lambda function, go to S3 function configuration permission.
If you scroll down and if you select the resource summary dropdown box and select the Amazon S3, you
can see that we have only get object action.
But for our business case we will listing S3 objects and we also need to list object action that is
explained in the documentation.
So for that purpose, I'm going to at least bucket requirement permission into our role.
And in order to do that, I'm going to right click and open new tab for the role name and this will
be open the IAM role and in the I am role page, I'm going to go to permission policies.
You can see the permissions and this is my role name of our lambda function.
If you scroll down you can see the permission policies and we have three policy, one for the Lambda
Basic execution role, which is the default one and one for the Lambda Microservice execution role,
which is the dynamic DB cloud operations and one for the Lambda S3 execution role.
It is only including get object, but we also need to list bucket list bucket permissions.
Okay.
Now we can add the required permission.
You can see in here there's a permission button.
If you click this button, you can see the attached policies.
I'm going to attach policy.
So in here I'm going to search for the S3.
You can search for the S3 and here you can see the Amazon S3 return access.
If you expand this policy, you can see that it is called the list, object, list, bucket and so on.
All these actions is handled in the Amazon S3 Return Access.
So please select these Amazon S3 Return Access and click the attach policy.
So it is successfully attached to my role.
If you scroll down, verify that Amazon S3 don't access attached and these including the list object
and list bucket permissions in our world.
Okay, very good.
So now we can go back to our lambda function if you refresh the page and come here as three functions
and go to configuration and permission.
This time, if you select the Amazon three, you can see that we have also get and list object resources
added successfully.
Okay, now we can focus on the code part, but before that let's review the documentation.
If you go back to the documentation, we are going to use list object version two command for listing
object into bucket and we give required permission.
If you scroll down, we will see the example usage.
Here you can see the command and this is the our client is three package and we will use is modules
to import statement and the rest of the code is very same.
We are creating a client object and sent client object to this command to perform our business logic
and passing the parameters.
These are the very same and you can check the input parameter in here.
Basically, we will give this kind of properties when requesting list object.
The important parameter of course, the bucket name.
Okay, let's start to developing our application.
If you open the Visual Studio code, you can think that this will be the our Node.js application for
that purpose.
In this folder structure, I'm going to create a package JSON file with running the NPM init via command.
Please open a terminal and go to record section and go to record lecture.
File lecture will be 250 and now I'm going to run npm init dash.
Why this will be create a package JSON file with the default information.
You can see in here we have created a package JSON file and the first thing we should do that the adding
type module in here, come here and click the type and set the module in order to activate script six
modules and Script six standards, we should add type module.
Please don't forget this and save this package JSON file.
Now we can install required packages.
If you come back to our list object, you can come to S3 client.
See that we are going to use client S3 package for the SDK JavaScript version three copy this NPM,
install command and go back to our visual code and please make sure that you are in the correct folder
structure and paste this command in here.
This will be install the required S3 package into our package JSON file and these are Node.js application
that we are going to deploy our lambda function.
So after finishing this install operation, we will see the package JSON file and check the dependencies.
Okay.
If you scroll down in the package JSON file, you can see that we have successfully instal required
a client package from the our SDK.
Now it is time to create S3 client separate Node.js module to perform our actions.
Please create S3 Client JavaScript file into our directory.
Come here and create a neve S3 client that JavaScript file and I'm going to develop this file with this
line of codes.
As you remember that we are using S3 client from these HDC packets and we are in the region, US is
and basically we are creating a new client and export this client into the separate Node.js model.
So this helps to decrease the package size and increase the call start issues.
Okay, now we can focus on developing our function.
As you remember that we have two items that we will interacting.
S3 Bucket for listing object into bucket and we will write on the DB table.
We have installed the required bucket client object and now we can develop this part.
In the next video we will implement list object into bucket.