In this video we are going to develop order acknowledgements microservices.
So before we start, we should understand the business logic of the order, acknowledge and microservices.
Basically the client application will be sent a http post request when any order created this will be
order placed event and this will be sent to post request to the API gateway an API gateway redirect
to request to the order acknowledge microservice lambda function and this will be synchronous invocation.
That means it will require required to return back to synchronous response to the client application.
And inside of this lambda function code in the microservice, the business logic should perform several
things.
For example, this will be create a new item into order dynamically v table and also publish a message
to the Amazon sass to redirect the request to the excuse and downstream services.
So that means we will do several things when we are creating a new item in the order table.
We should generate a new UID so that means we also have generate UID and create any item in order table.
And after that we will publish message.
So that means order equals microservice interact with the dynamic db or and amazon sass to publish message
and accommodate the incoming request from API Gateway.
This will be the synchronous invocation and response baked in synchronous invocation to the API Gateway
and the client application.
Okay, let's get start to the development.
If you open the visual code I have, create a separate folder for the order acknowledgements microservices.
I will develop all codes in this lecture folder and we will follow these steps.
If you open the command text, you can see that we have created index case file.
This will be our lambda function code and I'm going to create a package JSON file and we will make these
folder as Node.js application and we will install the required package in our application.
So according to our business case, we can see that these are the required packages that we are going
to install one by one.
The first package is the UUID for generating new ID before inserting a dynamic DB table.
The second one and the third one is Dynamo DB related SDK package, which is a Dynamo DB client and
Util Dynamo DB.
These are the SDK packages when we interact and perform current operations on Dynamo DB.
And the last one is the SNS client SDK package to publish message to the SNS from the Lambda function.
For that purpose we will run these NPM install commands and after that we will develop our lambda function
code.
Here you can see the several steps that we are going to perform when we are implementing business logic
in the order acknowledges microservices and here you can see the expected payload that comes from the
API gateway client application and we will test these expected payload with publishing sans topic.
Okay, very good.
Let's get started.
In order to that, I I'm going to start with creating index JS file.
If you come back to our index file, we'll put copy and paste a basic implementation of the Lambda 100
method that we will modify during this video.
So if you come back to a command section, the second step is creating package JSON file with this command.
In order to do that, I'm going to open a terminal and locate the correct section folder.
So this will be create a package JSON.
So that means this will be make this lecture folder as a Node.js application that we will deploy to
Lambda function.
For that purpose, I'm going to copy and paste NPM in command, hit enter and this is created our package
JSON file.
And the first thing we should do that we should add a type statement.
We can come here and set the type as a module in order to gain schema script six standards.
Okay.
After adding this type attribute, we should start to install required package.
If you come back to our package JSON file, scroll down.
You can see that I'm going to install one by one.
The first package is the UID.
You can think that we will generate any ID before inserting an item to dynamo db.
So that's why I have installed this uid.
And after that we will install this client as an aspect package and also dynamo DB package.
Think about that.
Interactions into order microservices.
Basically we need to generate ID with the UUID library and we need to save into Dynamo DB table.
And after that we will publish message to Psns to perform these actions.
I am installing required package.
This is the SNS SDK package for JavaScript version three.
And I'm following the dynamite DB package after that.
So let's wait for a while since these SMS package is installed successfully.
Yes, it is installed if you come here and copy from the NPM install client dynamo tb hit enter.
This will be installed required package for dynamic DB package.
And also I'm going to install util dynamo db.
You have already familiar with the dynamo db sdk package, but this time we will do several things in
the lambda function.
Okay.
If you go back to package JSON file and see the dependencies, we can see that.
Make sure that you have four dependencies for our application uid dynamo, adb, util, dynamo, adb
and SNS.
Okay, now it is time to develop our lambda function code.
We should plan our development for our order.
Acknowledge microservices.
So that means you should check the architecture and make sure that and plan your to do things with code.
I'm going to open our index JS file.
The first thing I would like to change the our method buddy, you can come here and change the exports
handler to the export code code standard.
That means we will use Postscript six standard and after that we will implement these to do logic.
So as you remember that we when we get the request from synchronously, we should have back to response
synchronized to API gateway.
So that's why this should be stay in our method and after that when the request comes to order acknowledge
microservices.
The first thing we should do that will let the incoming event and also we will perform the get request
but the payload information.
So basically let them write for the zero.
What the incoming event.
And after that we will get the request by the payload.
We will get the request but the payload that means or order acknowledge microservice triggered from
the API gateway and we will get the event JSON object and get the body information to reach the incoming
payload of the post HTTP post operation.
After that, when we get the incoming payload is you remember that if you open the command text, we
will expect this kind of payload.
And when we got this kind of payload, we will start our operations, which means we will generate the
ID with using UID library and prepare the payload with including the ID information.
And after that we will publish message to the SNS with using SNS SDK package and after publishing the
message we will save the order item into Dynamo DB table with using Dynamo DB SDK package and the last
item we will return back the synchronous response.
You can come here and say that return back synchronous order payload which generated ID to the API gateway.
Okay.
These are the archetypes that we are going to implement in this lambda function for our order solutions
microservices.
Okay.
Now we can start the implementation part for that purpose, open the pseudocode and go to index case
file.
So I'm going to start with the cache below.
So let me come here and cover we to try to look in the tribal look.
We will perform these actions one by one.
I will start with the first action which is the world date incoming event.
So we will check the event, the HTTP method, and after that we will throw an exception.
So that means we have only accept the HTTP post request when creating a new order item.
And the first thing we have checked that the incoming event async method should be paused.
Which event comes from the API gateway?
If it comes from the HTTP get put or other methods, we will throw an exception.
This is the first step and we will validate the incoming event.
And after that the another step is the get request payload.
We will get request payload from the incoming event, but the operation, you can see that we basically
get the event that the information maybe you remember in the API gateway triggered the lambda function.
We have a body information and we can pass this information, get the odd request.
So this is the this is the JSON object that we are sending from the our payload of the HTTP post operation.
So I have also put a validation when we get the order request, basically this order occurs should not
be null and the type also should not be null.
If one of the item is null.
I have throw an exception because type is important for our action.
So as you remember that in the command text we will send this kind of JSON object.
So we will check the whole object and also we will check the type and we will see the shipment requirement
for the upcoming videos putting some business logic in the type attribute.
That's why I have checked these two attributes.
Okay, we will finish the first step and now we are going to focus on the second step, which is the
generate ID with using UUID library.
So before we start, we should go to the import statement because when we are using UUID library, we
basically put a required import statement for using UID version for.
So please add this import statement after come back to our second step so we will generate ID.
So in order to generate the ID, this is the steps that we are going to do.
We basically use uid v for method and get to order ID and set the order request id as order id.
Maybe you remember we have a partition k for all the table, which is the id.
So that's why I have defined any attribute as ID and pasting these needed generated order id.
Okay.
Very good.
So after that we will perform our actual business logic in the third and fourth step, which is to publish,
to source and save into dynamic DV table.
I am skipping this three and four part and focus on the another part, which is the five step.
And if you come back to fifth one.
We basically turned back to sync order payload which generated ID to the API gateway.
In order to do that, I'm going to prepare any return statement in here, which is a success return
statement.
So this code is 200 body is a successful message.
And also I'm passing the data that will come from the are this three and four operation.
And if you scroll down now, we can remove these response of the method because we will return back
the success response after finished all these steps.
And in the case we look, we have to handle the error and also implement the error return statement
if you come here and.
Write down this method.
This will be basically logging the error if this happened in the error in the tribal log and after that
return back to API Gateway which scored 500 and the message with error details.
Okay, very good.
As you can see that we have developed our function without implementing third and the fourth step.
But you understand the idea now.
We will continue with the third and fourth step in the next video.