In this video, we are going to develop Hands on labs, which is the calculator project.
You can see the image on the slide.
This is very similar with the previous hands on loop, but this time we are going to create a lambda
and develop calculator function and this will be invoked synchronously from the CLI commands and using
the event JSON object.
And in the event object, we should start with the designing event first and after that we will develop
our lambda function according to these JSON object.
So let's get started.
Please open the Ouija board window and you can see that these are the steps, what we are going to do
for this calculator project.
First of all, this time we will start with the design of the event.
This is the best practice to follow with creating event object before developing lambda function to
understand how our lambda function and how we should develop our function according to incoming events.
Object.
So that's why I have put the first place designing lambda event.
So basically this is the calculator project.
For that purpose we are going to create a JSON file into a lecture folder and put these JSON format
in here.
You can find these lecture 63 calculator project and if you go to the event object or if you create
these this object, you can create these kind of incoming images object.
So we set that we have A and B attributes that could be represent number one and number two.
And we have operated we expect opt attribute and the operator we can expect a calculator processes in
this in this attribute value.
So basically we will perform some operation with these numbers and it is can be configurable to change
the operation.
For example, we can multiply these numbers or we can divide these numbers, but this time I would like
to stay at some operation.
So the idea is that we are expecting this event JSON when invoking our calculated lambda functions after
designing these lambda event JSON file object, now we can develop our lambda function for that purpose.
Please go to the command section and see that now we are in the first step, which is the lambda function
code.
For that purpose we are going to create an index JS file and in the index case file here you can find
the basic implementation of handle method.
We are getting the event object which will be the our events object and in the event object in the first
place, we are going to lock these incoming event into the Amazon Cloud Showcase and after that we will
perform the calculator operation.
So please start with coding in the validation.
I would like to put a validation before we start because it is very important to get event A and even
be an operator.
Otherwise if there is undefined, we should return an exception for that purpose.
I'm going to develop this block with throwing an exception if any of the parameters are undefined.
So you can see these if statement this is validation, we can check a, B and or attributes in the event
incoming event JSON object if one of them is undefined, we are throwing a new exception which is the
event should exist these attributes.
Okay.
After the validation we can perform the actual business logic which is the or calculated project for
that purpose.
I'm going to create an.
Object, which is the result object that will be load up to the perform the calculation.
So after that I'm going to root use switch statement see which should be the event that operator operation
and the value should be check operator will be plus the or multiply.
For that reason I'm going to write this case and if there is a plus sign I'm going to calculate result
a cool event dot a plus event dot b.
Look, now we have finished the same operation and is the same way.
I'm going to develop other calculated operations.
So now this is the operation we are deducting.
A and even B, and if you copy this case, this time I'm going to perform multiply operation so put
to multiply and this time we are multiplying the incoming A and B numbers.
And the last case will be the divide.
For that purpose, I'm going to divide you A and event b oc.
I can remove the default block because we have put the if statement.
So these operators and the numbers should be exist in our incoming event JSON and this is our switch
statement that we are performing the calculator operation and after that I'm going to lock this result
for that purpose.
We can look to the watch, look the result calculated result operation.
We can see this line of code and after that I'm going to turn.
The result here is very simple calculator lambda function that we are going to publish and deploy the
lambda function.
After we have finished the development, we can go to the next step.
If you follow the command text, you can see that the next step is zip file.
So please open a new terminal.
And go to a related lecture.
Calculator.
Okay.
Now I'm going to follow the second command since we have one index case file, it is created function
zip file.
And I am following the third step which is the create lambda function for that purpose.
I have already get the A run before, which is the lambda execution role.
Now I'm going to run directly this lambda create function select command hit enter.
The function name should be the calculator and function is created successfully.
And if you scroll down we have last step is invoke this function as you remember that we have created
JSON file so now I'm going to invoke this file with.
Give them even an object so you can copy and paste this output and hit enter.
If you hit enter you can see this code is to 100 successfully executed.
And if you go to response, Jason, you can see the response is seven.
So that means we have performed this A and B operation and some these numbers very good.
So after that, we can also change these events.
JSON file, for example, as you can see that now the response is seven.
But what if I change this operation as a multiply if you save this event JSON file and if you run the
invoke again hit the enter and this time if you check the response, Jason, the response coming ten
so that means we have multiply these two numbers A and B, very good.
So now let's check these operations, these Lambda functions from the A management console.
Please open the A management console, go to functions and see that we have calculated function go to
function page and as you can see that our codes coming in index case as expected.
So now before checking the cloud shows I would like to test again in here.
If you click the test section and create a test operation and if you send a random head over this evaluation
file, let's say this operation and click the test button and see what's happened this time the execution
result is failed and we can see that the error is returning.
The error type and error message is even should exist.
AB and operator attributes in the incoming evaluation.
So we also test the worse scenario with throwing an exception.
Now if you would like to check the Amazon cloud looks, we should see three in location.
And in these three location we should see that this function is invoked by the this incoming event.
First we are some these two and five and return to the seven and after that we change the incoming event
JSON file and we are multiply these numbers and return back to the ten.
And lastly, we are sending a random invitation and we solve that it is returning an exception regarding
these incoming payload should be a, b and operator attributes.
Okay, very good.
As you can see that we have successfully developed our hands on love, which is the calculator project.