In this video we are going to develop lambda function for dynamic deviations is know that in the last
video we are listed objects into the bucket and we are iterating these objects and add item into dynamic
DB table.
So for that purpose we will use dynamic DB client SDK package that we have already did in the last sections.
So that's why I'm not going to explain detailed.
We will install client dynamic DB and also we have utility library that we will use util dynamic DB.
It is also very important to use Marshall and Marshall Operations to do that.
Please open Visual Studio code and go to index case file.
We are in here write item on the DB table with the incoming objects from the S3 bucket.
So to perform these Dynamo DB actions before we start, we should add the required package into our
Node.js module.
As remember that this lecture folder will be our Node.js application that we will deploy on the Lambda
functions.
So now I'm going to run the NPM install command into our CLI.
Please go to correct folder structure and run the NPM install commands one by one.
The first one is the NPM install client dynamo DB package hit enter.
Okay.
The second one is the api install util dynamo db hit enter.
And after that, please verify these two packages are imported successfully.
If you go to package JSON file, you can see that these two client dynamo DB and line MDB installed
successfully.
Now we can use these libraries in order to interact with the dynamic DB.
So for that purpose callback the index JS is know that when we interact in resources we need a client
object.
To do that we need also dynamic DB client JavaScript file, which is a separate Node.js module.
In order to do that, come to our folder and create a new file.
Which name is Dynamo DB Client Logs?
So I'm going to copy and paste the codes.
Let me copy paste.
This is very similar with the H3 client.
We basically export dynamo DB client from the dynamic DB client object of the client SDK library.
So now if you come back to our indexes now we are ready to use our actual code and implement to Dynamo
DB operations.
But before that, let me change these S3 bucket operations.
You remember that in the last video we are passing these parameters to the list, object, command and
send this command to the client and it is return back to us data.
So what data is coming to us?
It is basically return to us object list.
So I'm going to change the variable name as object list and also change the console log as the object
list.
So we got the object list from the Amazon S3 and we will iterate the object list to insert our dynamic
DB table.
So this is the first part that we finished and now we are going to contribute the second part.
So let me copy in here.
This is the first part.
And now here you can see the second part.
So in the second part we will iterate our object list and we will add item into Dynamo DB.
As you remember that one S3 return vectors object list, it has the content array.
So that's why we are writing the four condition for iteration in here.
So in the fourth iteration we are going to iterate the contents array and get the content in a for loop.
So in this loop, basically in the first line of code, we basically log the incoming content and after
that we are preparing the parameters that we use in the put item command in Dynamo DB.
So here you can find the parameters.
So these parameters are including the table name and the item item will be the R content of the object
list and we will pasting the whole content into bucket objects table.
And we have some basic attribute values which are the remove undefined values true and convert clause
instance to map true.
So with these attributes we can pass the whole content object as item into bucket objects dynamo db
table.
After prepare the parameters, we basically send the request to the put item command.
This time we will use put item command in here, passing the parameters as this way and put item command
is provide to create a new item in dynamo DB table and use the DB client object to send this command
available way to Amazon Dynamo DB and after that we will look to our created result and we finished
the our code operations.
Okay.
Of course now we should not forget to record import statements as you know that we have import put item
command we should import and also dynamo DB client object.
If you go on top of these indexes file, let me write this import statement.
This is for the put item command dynamo.
DB And also we will use Marshall Command when preparing an item in the parameters and after that we
will use Dynamo DB client object.
Of course this is really important.
This is separate Node.js module that we use the interactions of Dynamo.
DB So these three import statements for Dynamo DB These two for the S three OC.
Now, after preparing the import statement, if we scroll down and see that the command and Dynamo DB
client is activated, let me explain from the secret.
If you come back to our method, you can see that this is the 100 method for the lambda function and
we have two main actions.
The first one is list object into bucket and the second one right object into dynamo DB table.
To perform the first object, we are preparing the parameters.
We can move these parameters in here.
Let me cut and paste in this line of code.
So this line of code is performing the first task which is the list object into bucket.
So that means we will interact with S3 client object and running to.
Object command and get back to us.
The objects into a bucket.
The bucket name is in here.
So what is most coming from the Amazon is three we can see in here in the last video we get this response.
So they had a contents array and in the contents array it is listing the objects into Amazon S3 bucket.
So we have one object and you can see the key and other attribute values in here.
So that means these content zero which is these objects will be an item of dynamic DB table is you remember
that when we create a dynamic DB table, we set the partition key as a key.
So these objects including the key and the value of the text.
So that means we can use these attributes and the partition K into our dynamic DB table.
So that's why we can pass the whole object as an item of our application.
You can see in here, if you scroll down, we are iterating the contents and we get prepare the parameters
of the dynamic DB Put item command with passing the table name and also item in the item section.
We passed the whole content object which including the key attribute and this is the ah partition partition
key of the dynamic DB table, so that's why we can pass these as item.
And after that, after preparing the parameters, we basically send the input item command with using
the DB client in order to perform from creating an item into Dynamo DB table.
So these are the our codes and we finish the development of these 100 method.
Now we are going to perform local test.
So I would like to verify our code is working or not with running the codes on my local computer.
For that purpose, make sure that you have one handed method and at the end of this file you call this
100 method.
And after that we can open a new terminal and click the node command right to node command and passing
our indexes.
So this will be listing objects into bucket name of a bucket, and after that add an item in Dynamo
DB table.
Let's run this command and see what happens.
As you can see that we have got successful response.
If you scroll up and see what's going on, we can see that the first success message comes from the
Amazon S3 and if you scroll down, you can see the content array.
So the first array will be an item of our dynamic DB and in attributes they are key, last modified
and e take and so on.
The key attribute is the partition key of our table we can verify from dynamic.
DB If you come to Dynamic DB tables, we have a bucket object table and the partition key is a key string.
So that's why we can pass these value into items.
And if you scroll down, the second response comes from the Amazon dynamic.
DB That means we have successfully implement insert an item into dynamic DB table to verify that if
you go back to console and open the bucket objects table and explore table items, if you run the second
command, you can see that we have successfully insert an item which the key is a index.html and the
other attributes comes from the successfully.
These are the objects into the Amazon S3 that we return from the list object command.
So now we have finished the development path and local test from the replication successfully.
In the next video we are going to deploy our Node.js lambda function and invoke asynchronously from
Amazon S3.