Today we are building Our First serverless webpage using Aws Lambda.
What is ServerLess?
ServerLess means the name it says Don't worry More about servers instead of concentrate on Core Product.
Aws, take care of the servers and no need to worry about scaling up and scaling down one good thing about serverless is Aws only charges for you when someone hits your webpage if there is no traffic it means no charges.
I think now you have some understanding about serverless let's learn it more by building.
What are Requirements?
Nodejs v8+
I'm using the serverless framework Cli to create and deploy apps.
Open your terminal
npm install -g serverless
sls login // SLS is a shortcut of serverless
After sls login, You need to configure Your Aws Credentials with a serverless framework
Once It is done Your are good to go.
mkdir serverless-app
cd serverless-app
sls create --template hello-world
The above command generates the boilerplate.
Boilerplate generates Three files like in above image.
Now we need to install the express and other packages.
npm init // to generate a package.json file
npm i --save express body-parser hbs serverless-http
Now open the handler.js file and clear it why because we writing it from scratch.
Just same as how we are building the express app.
we need to export the app like i did in line 16.
Now create a views folder in your root directory.
index.hbs code
Now final step open the serverless.yml file.
runtime nodejs8.10 why because Aws lambda currently supports v8.10.
In line 11 we need to specify the name of our function.
In line 12 handler it means we need to tell on which file it needs to look.
Now in your terminal run.
sls deploy
It will give us back endpoint copy and open in your browser
That's it we are done.
If you build the same thing using Aws console it is not very easy for the new people. So that I'm using the serverless framework to show you in an easy way.
Above image is our output.
Now open your aws console
It will show the active functions
For monitoring, Debugging and error detection of lambdas we are using Dashbird
Why Dashbird?
Dashbird helps us actively monitoring the health and errors.
One main thing about Dashbird is its user-friendly Interface.
Dashbird visualizes all your AWS Lambda metrics like
memory utilization, invocation count, and execution duration.
How to integrate Dashbird with our Aws Lambdas?
For these, we need to create an account on Dashbird.
Once you are done then open Aws Iam for permissions.
click on Roles.
Select Another Aws Account It will ask for account id.
Now open your Dashbird account Click on Add new Organization it will show the Account Id and External ID paste it on your another Aws account page.
Once Your are done it will redirect to policy page now click on create
The policy it will open on the new tab.
Just like above image you can get this json on the Dashbird where you get the Account id and External id.
click on Review Policy on Aws it will ask for a name you can name it as dashbird-delegation-role Now click on Create Policy That's it we are successfully created
the policy.
Now go to roles tab click on refresh policies you can see the dashbird-delegation-role select it and click on the Create role.
Copy the Arn and paste it on Dashbird that's it you are Done.
DashBird Interface
Hope you guys enjoyed if you have any doubts feel free to ask.
Happy coding.....
Code Repostiory
other interesting Posts on Serverless
Top comments (5)
Awesome article Sai! I look forward to your next one!
Thanks John
Easy, just build it and leave it on your machine. Vuola, it's serverless. :D
So, it isn't serverless in that there is no server. You write an application for a node server, and then with one command it gets deployed on an AWS server, right?
It gets deployed as a Lambda function so you don't have to mess around with setting up an ec2 instance (a server). The Lambda function runs on a server yes, but that's all abstracted away. The Lambda function also has auto scaling built in and you are only charged for usage vs an ec2 instance where you're charged for up time and other charges.