In this video, we are going to develop inventory, microscopes, lambda function code.
So that means we are in here and we will develop this inventory microscope with pulling skills and we
will save these items into inventory database.
So as you remember that we have created a normative table for inventory operations.
If you open the console, go to the DB tables, you can see the inventory tables and partition key is
a code.
Now please open the Visual Studio code and I have created separate lecture folder for dimensional microservices.
In the common text.
We will follow this step.
We solve the inventory table and now I'm going to show you incoming event JSON object from the space.
You can see the notification event that we have stored in the last video.
If you click the notification and you are JSON, you can see that these records are consumed by the
Lambda microservices and in the body information we will follow the SAS documentation and in the body
we have a message.
So in the message we can access the actual JSON payload that we sent from the HTTP post request.
So for that purpose, I'm going to follow these events, JSON, and we will implement these lecture
folder as a Node.js application for eventual microservices.
So I'm going to locate this folder and I will run the API NPM command to create package JSON file.
So for that purpose, let me come here and locate the correct folder.
Which is the of microservices.
Now we can run these commands one by one.
So basically.
Running MP I'm in and why to make these folder as a node.js module and the first thing.
Please open the package JSON and go to.
Configurations and set the type, attribute and value should be module in order to gain ActionScript
six standards.
And after that, if you come back to our commands, we have two required package should be installed
these Node.js application these are the client dynamo db and util dynamo db to interact with the Dynamo
ADB package.
So I'm going to install one by one these two packages and after that we will develop our actual lambda
operations.
Okay.
Very good.
So now we can focus on the development part.
So for that purpose, please verify that in the package json, we have installed the required package
successfully and your type as a module.
After that, you can create an index case and index case.
I have created an empty function which is the export handle, lambda function and log.
The first line of code we will implement in the to do statement and I put for the case below.
Okay.
Now we will design our inventory microservices and implement this function code.
The idea is very simple.
We are in here and basically we will handle incoming event from the case and interact with the inventory
dynamo DB table.
So let's make a plan with Writing Circle.
If you open the our index case, we will write the code.
For example, the first item we should do that iterate the case records because this event has records
and we will iterate these records.
And after that, in the second step, we will get requests by the payload of Psns because we will reach
the body information.
This comes from the Psns and after that we will get in request from the body of the SNS data to reach
the order data.
So that means if you open the command text, you can remember that we will reach from the Psns as case
lambda.
We are in this point, so we will get the response request from the SCS and request get the request
from the Psns.
So in the SNS we have an actual order data that we can access.
So that's why, that's why we get the request two times.
And the last action is the fourth step, which is a save order item in the inventory dynamically we
table with using dynamo db sdk package.
Okay, let's do these actions one by one.
I'm going to start with the first action, which is the ITERATE case records.
I will start with writing in three block.
We can come here and iterate in this position.
I have created four statement.
Basically we will iterate event records because we know that in the incoming event you can check the
notification event, JSON.
We have records array, you can come here and come to go home.
As you can see that we have records array, so that's why we are reaching these the first item of the
record.
So go back to index case and iterate event records and get the records and log records.
So we finish the first item, iterating is case and now I'm going to perform second item, which is
the gate request by the payload of Psns.
So for the first payload we will get the record body information and this will be the SAS published
message.
So if you come back to our notification event and if you go to the information the body including the
SNS event, JSON object, so that's why we have convert the first time which is getting the SNS published
message.
So this is the message when we are publishing the Psns topic.
So get these message first and after that we will go one level deeper.
This is the gate request, but the payload of all the data, which is our actual data.
In order to do that, I'm going to pass again with the SNS published message and get the message as
an order request, because we have one more level.
And if you go back to our notification event, Jason file in this JSON file, but if you go to message
after that, we get the actual message that we post from the HTTP post operation.
So we reach these order data in the level of the SNS message.
So if you come back to our indexes so we can successfully get the order request from the incoming record
of the case.
Okay.
So the last step is save order item, but before that we can perform business logic and prepare for
the inventory item.
So for that purpose I'm going to set a partition.
K it is very important before sending request to Dynamo DB.
So basically we will validate for the order request or the request should not be null type should not
be null and also type should not be a shipment requirement.
So think about that.
We have a business rule.
That means if the incoming order type is not a requirement, these should not consume from the inventory
or microservices.
If you go back to our architecture, think about that.
The client order comes in here and the order type.
Is equal the shipment requirement.
So that means this should be consumed by the shipment and deduct from the inventory.
And both these two Lambda Micro sources consume this order.
But if the order type is not equal, the shipment requirement, for example, only notification requirement.
This should consume from notification microservices, not interested in inventory and the shipment microservices.
So we have this kind of condition and business rule.
You can think that we will put these if condition, but we will solve these condition in different way
in the upcoming videos.
But in this video we should put these if statement to make sure that the order type is shipment requirement.
And after that we will perform our business logic in the event of microservices.
For that purpose, I'm going to set a partition K of the order request, which is the inventory table.
This will be the code, as you remember, that the code is the partition.
Okay, so I'll set the partition K as an order request item.
So in the order request we have an item attribute.
If you go back to our common text, you can see the expected payload and here you can see the item and
attribute of the incoming payload.
So we are passing item as a code to make sure that the partition key attribute includes this order request
and assign all these order requests as a matter item.
So that means we have create an inventory item and inventory item will be our new item in the inventory
table.
So after that we will perform the actual save operation.
So I'm going to come here and perform these four step, which is the same order item in dynamic DB.
So basically create a dynamic DB parameter, spacing the table name as inventory and placing the item
as an inventory item that we assign in this point and create inventory item in here.
And after that, we will test these parameters into the put item command and we will use dynamic DB
client object.
So that means we should create the negative client object and also import to put item command statement.
So we forgot the adding output item dynamic DB client.
For that purpose, I'm going to copy the DB client from the other project and paste into our lecture.
And as you can see that I have copy and paste dynamic DB client in here.
So if you come back in here, I would like to import the required statement and we will use Marshall
and Marshall and put item command dynamo, ADB Command Command.
You can also copy all import statements from the previous lecture.
If you go back to our order acknowledgment, you can copy all these import statements regarding to Dynamo
DB and after that, come back to our index case.
And if you go on top of this page, which is the inventory microservice index, paste, import statement
and save this file, this is for the put item command when inserting Dynamo DB Marshall when preparing
the parameters, DB client should be included in every project when interacting dynamic.
DB If you go back to fourth step now, as you can see that all these objects are assigned and comes
correctly, now I have clear the empty spaces.
Okay, very good.
So basically we will finish the development of the inventory microservices.
Now it is time to zip this code and upload inventory microservice on the Lambda Cloud environment.
For that purpose, go to folder page and go to index JS right click reveal in Explorer and go to correct
directory and copy all items, remove commands and the other items can be zipped.
I can remove the notification and shipment event and only project related items I have added right click
send to zip command and this will be the inventory microservices.
So give the name as an inventory.
Microservices.
Okay.
Now please open the console.
Go to inventory.
Lambda function.
Open the lambda function.
Go the inventory microservices.
So please click the upload from.
But before that, I would like to show one more important thing.
Go to configuration and see the permissions.
If you see that we have create a new role which in the name is a role and in this role, including dynamic
interaction, it is very important to put command using to put command for Dynamo DB.
These required permissions should be added the execution role.
If you come back to code and click the upload from and zip file.
So click the upload button and go back to our lecture folder, which is the microservice.
Find the microservice.
Click to open and click this button.
So this will upload function into a cloud environment that we develop in our local Node.js project.
So if you come here and follow the indexes, you will review the code and see some exceptions in here.
Basically, inventory item is not defined, but it is a good practice to check codes, review codes
on a list environment.
We forgot the defined variable, which is the enter item.
Please go back to our widgets to do code.
Go the indexes.
I think we don't need the inventory item variable in here.
We can remove the all inventory item because we have already assigned the code attribute into order
request.
So we will pass the order request for our case.
So please remove the object, assign line and copy order request and paid order request is a matter
item into item dynamic db.
Save these indexes file and after that I will follow the same steps.
Please delete this zip file and right click indexes revealed in explorer copy control a for select all
items, remove commands and event JSON files.
Now we have select only required items.
Right click send to zip file compressed zip folder and write in one.
Inventory is now open.
The adults lambda function go to inventory function and click the upload button, click the zip file,
select the inventory microservices, click the save button.
Look, this will upload our new code into a lambda environment.
So as you can see that this time if we reveal the code, we don't see any exception or error.
Okay, now we are ready for the handle.
Incoming invocations to inventory microservices.