Sitemap
AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Press enter or click to view image in full size

Serverless

--

First, we need to understand what is serverless and non-serveless which we also call monolith.

Let’s understand Monolith first then we’ll deep dive into Serverless.

In a monolith, we create a virtual machine on AWS, Azure, or GCP where we buy a machine on rent in AWS we call it an EC2 instance and we have a variety of options to choose machines depending upon our needs, as you can see in below picture we have the option to choose machine depending upon our needs and we AWS will charge on an hourly basis.

Press enter or click to view image in full size

Let’s say we choose t2.micro after choosing the machine we need to install node js and all the dependencies which is required to run our code then we will run our code on the same machine and this machine is up 24 * 7, if we calculate that how much this t2.micro will cost, we can easily calculate as it’s hourly charge is 0.0116$, so per month cost will be 24 * 30 * 0.0116 = 8.35$ which is equal to 690 INR.

Drawbacks of using monolith.

  1. Our machine will be continuously running 24 * 7 whether there is any user using our website or not.
  2. We need to manage the configuration of EC2, also an OS installation.
  3. We need to also do the code deployment.
  4. We need to also worry about the dependencies that are required to run our code, like node js.
  5. Let’s say 10000 users suddenly come to our website then we need to also worry about scaling up and when there are fewer users then we need to scale down also, in AWS we use ASG(Auto Scaling Group) and definitely ASG also costs some money.

Let’s see how serverless works

In serverless we need to only worry about the code and we don’t have to worry about the underlying architecture that is called serverless

Press enter or click to view image in full size

In terms of AWS, there is something which is called AWS lambda, we just need to give our code to AWS and it’s AWS's responsibility to allocate OS(Operating System), RAM, and Storage, handle deployment, upscale, or downscale.

This is called serverless we don’t have a server we have only code.

Get Adnaan Azam’s stories in your inbox

Join Medium for free to get updates from this writer.

Benefits

  1. AWS lambda is charged on a per invocation basis as of now it charges 0.001$/ invocation not an hourly basis as in the case of EC2 we have been charged on an hourly basis regardless of whether there is any user on our website or not.
  2. We don’t have to worry about scaling it will be handled automatically by our cloud provider.

How Serverless work

Press enter or click to view image in full size

We have a code deployed on serverless and as of now, it’s in a Sleep state when the user visits https://www.finoyou.in our code will change to a Start state then our code run line by line and return the response to our user, and then it will switch to Sleep state.

Let’s say at some time 10k users visit our website then serverless will replicate our code, and when there are fewer users it will scale down accordingly.

Press enter or click to view image in full size

Let’s create our first lambda in AWS

  1. Open AWS and serach for lambda and then hit Create Function button
Press enter or click to view image in full size

2. Let’s configure our lambda function

  • Write our function name
  • Choose runtime in this case we choose Node js 18 version.
  • Hit Create Function
Press enter or click to view image in full size

3. When our lambda function is created it will navigate to this page where we can see some of the code written.

Press enter or click to view image in full size

4. We need to add a trigger so that we can access this code for that we need to add a trigger, click on Add Trigger which is on the left-hand side it will navigate to this page, and choose API Gateway and Rest API under API type and hit Add

Press enter or click to view image in full size

Yeah we successfully created our first lambda, as you can see at the bottom of the page we got an API endpoint = https://gybu7kpo5g.execute-api.ap-south-1.amazonaws.com/default/mediumFunc

Press enter or click to view image in full size
Press enter or click to view image in full size

Hope you guys like this article.

--

--