<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikhil Gautam</title>
    <description>The latest articles on DEV Community by Nikhil Gautam (@nikgautam).</description>
    <link>https://dev.to/nikgautam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F236538%2F6578a103-83a1-46b9-9c28-dd88c3d0cc58.jpeg</url>
      <title>DEV Community: Nikhil Gautam</title>
      <link>https://dev.to/nikgautam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikgautam"/>
    <language>en</language>
    <item>
      <title>Introduction to Serverless Functions</title>
      <dc:creator>Nikhil Gautam</dc:creator>
      <pubDate>Tue, 18 Aug 2020 15:27:39 +0000</pubDate>
      <link>https://dev.to/nikgautam/introduction-to-serverless-functions-45ll</link>
      <guid>https://dev.to/nikgautam/introduction-to-serverless-functions-45ll</guid>
      <description>&lt;p&gt;Most of you might have heard (maybe...) the buzzword "Serverless", this might sound some server-side magic which hosts your code without a server (which is totally wrong). In reality, this is a very easy and fast way to get a piece of code up and running on a remote server.&lt;/p&gt;

&lt;p&gt;Anyhow, in this article, I will be de-mystifying the serverless architecture and sharing with you everything I know about this topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Serverless Functions?
&lt;/h2&gt;

&lt;p&gt;Well, let's talk about servers first.&lt;/p&gt;

&lt;p&gt;Normally, when we ship our code to a server for hosting a website ( as an example ), we as a user of the server are solely responsible for scaling the app, the security of the app, restarting if it crashing or fixing bugs on the production like a boss. And that can take a lot of time sometimes, and it's even more frustrating when the size of the application is small or is only be supposed to be used a microservice. Also, the over-payment if you don't that level of computing power.&lt;/p&gt;

&lt;p&gt;So, what serverless proposes is that, hey, we'll take care of scaling, performance and most of the deployment stuff, you just need to provide us a function with does the task you want to do and follow a few rules. And you just pay only for what you use.&lt;/p&gt;

&lt;p&gt;And, AWS calls those functions &lt;a href="https://aws.amazon.com/lambda/"&gt;Lambda functions&lt;/a&gt;, GCP... &lt;a href="https://cloud.google.com/functions"&gt;Google Cloud Functions&lt;/a&gt;, Azure... &lt;a href="https://azure.microsoft.com/en-in/services/functions/"&gt;Azure Functions&lt;/a&gt;.. and on and on.... if there exist good cloud tech, every provider will jump on it like a kangaroo. These platforms are called Functions-as-a-Service (FaaS).&lt;/p&gt;

&lt;p&gt;In this article, we will be using &lt;a href="https://www.netlify.com/products/functions/"&gt;Netlify Functions&lt;/a&gt; it's not as big of a company as Amazon or Google, but it's more powerful as it is very easy to use compared to the alternative, also a bonus integrates very well with other Netlify products (btw which are awesome and you should check them out).&lt;/p&gt;

&lt;p&gt;Netlify Functions in the background uses AWS Lambda functions so you can use almost the same things that you learn here and implement on AWS.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you use Serverless Functions?
&lt;/h2&gt;

&lt;p&gt;In use, Lambda Functions ( in javascript ) are very similar to some like NodeJS, you can do to simple calculations, API calls, use is-odd npm package ( yup that exists ), etc - general NodeJS stuff.&lt;/p&gt;

&lt;p&gt;Every serverless function lives in a separate Javascript file, this file name will also be later used in the routing.&lt;/p&gt;

&lt;p&gt;Also, dependencies and shared code are also allowed.&lt;/p&gt;

&lt;p&gt;That means, if you are a front-end developer then you don't need to worry about maintaining a separated Node Application. If you are a back-end developer, then you can hand-off a few ( not very frequently used ) task to separate serverless functions, like login and logout, and adopt a kind-off microservice design.&lt;/p&gt;

&lt;p&gt;Serverless functions are a ready-to-go function, especially with Netlify's CI/CD features. Even with alternatives like PaaS solutions like Heroku which allow very easy deployment setup, this process is still much easier to maintain and use as Functions live inside your main project without the need of maintaining a separate project. Even with Docker containers, you still have to manage them.&lt;/p&gt;

&lt;p&gt;For front-end devs, serverless functions can be a huge time saver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets Begin...
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisite Node Packages
&lt;/h3&gt;

&lt;p&gt;In this article, we will be using the following NodeJS packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;netlify&lt;/code&gt;:- This is the official Netlify CLI. We can use init, deploy and set up your functions, so we will be installing this globally.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ npm install -g netlify
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;netlify-lambda&lt;/code&gt;:- This is an amazing tool which allows us to test the functions in a local dev environment ( with auto-reload ) and also compile them to netlify functions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nv"&gt;$ &lt;/span&gt;yarn add netlify-lambda
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;axios&lt;/code&gt;:- Axios is a promise-based HTTP client for the browser and node.js. We will be using it have some fun later on.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nv"&gt;$ &lt;/span&gt;yarn add axios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, I will be using Yarn in this tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Folder Structure
&lt;/h3&gt;

&lt;p&gt;We will be using the following folder structure in this article:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
├── &lt;span class="k"&gt;function&lt;/span&gt;
│   └── getTest.js
|   └── getMichaelScott.js
├── netlify.toml
├── package.json
└── yarn.lock

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, lets breakdown this structure -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;function&lt;/code&gt;:- In this folder, we will be storing all the functions we will we be writing.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;getTest.js&lt;/code&gt;:- This an example function, all the functions will be, similarly, stored in separate files. We will be exploring the contents of this file in later sections.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getMichaelScott.js&lt;/code&gt;:- Well... I like The Office, not my fault. (Its a file containing a function, duh)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;netlify.toml&lt;/code&gt;:- This is the config file for Netlify, here pass can pass deployment and build configurations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt;:- NodeJS package configuration file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;yarn.lock&lt;/code&gt;:- Auto-generated file by Yarn for package indexing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Project Setup and Config
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Scripts
&lt;/h4&gt;

&lt;p&gt;Since we are using the &lt;code&gt;netlify-lambda&lt;/code&gt; package for building and dev.&lt;/p&gt;

&lt;p&gt;We can add the following scripts in our &lt;code&gt;package.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lambda-serve"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"netlify-lambda serve function"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lambda-build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"netlify-lambda build function"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Actually, command syntax is &lt;code&gt;netlify-lambda serve/build &amp;lt;Function Folder&amp;gt;&lt;/code&gt;. Since all our functions will live in &lt;code&gt;function&lt;/code&gt; folder, our function folder will just &lt;code&gt;function&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Netlify Setup
&lt;/h4&gt;

&lt;p&gt;By default, &lt;code&gt;netlify-lambda&lt;/code&gt; puts the functions in &lt;code&gt;lambda&lt;/code&gt; folder after building. So, we need to tell Netlify where to find our functions.&lt;/p&gt;

&lt;p&gt;So, in the &lt;code&gt;netlify.toml&lt;/code&gt; file, we can add:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[build]&lt;/span&gt;
  &lt;span class="py"&gt;functions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"lambda"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, Netlify will look into the &lt;code&gt;lambda&lt;/code&gt; folder, which gets generated when we build the functions, by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn run lambda: build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Onto the Functions
&lt;/h3&gt;

&lt;p&gt;Now, the real deal, the functions. I will show you a basic example first then we will break everything down and understand how this stuff works.&lt;/p&gt;

&lt;h4&gt;
  
  
  Basic Format
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// some legendary code goes here&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, every function should return a &lt;code&gt;handler&lt;/code&gt; function. This function gets executed when the function's route is hit.&lt;br&gt;
When the function is executed, Netlify passes a 2 parameters i.e. &lt;code&gt;event&lt;/code&gt; and &lt;code&gt;context&lt;/code&gt; to our function and you must provide a &lt;code&gt;callback&lt;/code&gt; function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;event&lt;/code&gt;:- When your function is hit, Netlify passes an &lt;code&gt;event&lt;/code&gt; object which contains data related to the request. It consists of:-
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Path parameter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"httpMethod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Incoming request's method name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"headers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;Incoming&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;request&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"queryStringParameters"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A JSON string of the request payload."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"isBase64Encoded"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A boolean flag to indicate if the applicable request payload is Base64-encode"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;context&lt;/code&gt; :- This object provides methods and properties that provide information about the invocation, function, and execution environment. There's a lot of info in this object, too much for me to explain in this article. So, you can check out this &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html"&gt;link for more info&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;callback&lt;/code&gt;:- Now, this is responsible to send a response. in there you can specify general response stuff, like response body, status code, headers, etc.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Parameters&lt;/span&gt;
  &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;//Possible fields for Response in the Callback function&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isBase64Encoded&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;statusCode&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;httpStatusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;headers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;headerName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;headerValue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Working example&lt;/span&gt;
  &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;//For Errors&lt;/span&gt;
  &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Lambda can also use async functions, there instead of callback we return response object and throw errors. &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-async"&gt;Read about it more here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  Some more Functions
&lt;/h4&gt;

&lt;p&gt;I have put together 2 examples of serverless functions, you can go through these examples, if there are any doubts/confusions/problem you find in these feel free to flood the comments.&lt;/p&gt;
&lt;h5&gt;
  
  
  Function 1:-
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;function/getTest.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🚀 serverless is 🔥&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Function 2:-
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;function/getRonSwanson.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;michaelSaid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://michael-scott-quotes.herokuapp.com/quote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;michaelSaid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;michaelSaid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;michaelSaid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;michaelSaid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quote&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Michael left Dunder Mifflin!!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/QvXe5wq8w94A0/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/QvXe5wq8w94A0/source.gif" alt="Everbody Dance Now"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YAAS 😎.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Example 2, I have switched up a few things. I have used ES6 syntax, as &lt;code&gt;netlify-lambda&lt;/code&gt; package comes with the magical ability to compile it down. Also, I am using Async Function here, so, I can just return an object with same property specified above and throw Errors like a boss.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Runnin' dem functions
&lt;/h3&gt;

&lt;p&gt;To start a local development server, just do:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn run lambda: serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This should start a local dev server w/ auto-reload. By default, it uses port 9000, so to test the functions we visit &lt;a href="http://localhost:9000/"&gt;http://localhost:9000/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, you opened that and got an error.&lt;/p&gt;

&lt;p&gt;Well...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;netlify-lambda&lt;/code&gt; maps all functions to a specific route according to the fileName of the Function.&lt;/p&gt;

&lt;p&gt;For Example:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function/getTest.js -&amp;gt; http://localhost:9000/getTest
function/getMichaelScott.js -&amp;gt; http://localhost:9000/getMichealScott
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;During deployment, there will a few changes here, we'll take a look on that later on.&lt;/p&gt;

&lt;p&gt;For now, let's check if everything is fine or not.&lt;/p&gt;

&lt;p&gt;Response for function 1:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;➜ curl http://localhost:9000/getTest/
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;:&lt;span class="s2"&gt;"🚀 serverless is 🔥"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Response for function 2:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;➜ curl http://localhost:9000/getMichaelScott
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"michaelSaid"&lt;/span&gt;:&lt;span class="s2"&gt;"NO! GOD! NO GOD PLEASE NO! NO! NOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;yup, this article is Blessed...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/12XMGIWtrHBl5e/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/12XMGIWtrHBl5e/source.gif" alt="NO! GOD! NO!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Deploy the damn thing
&lt;/h3&gt;

&lt;p&gt;Everything seems fine here at Local Development Land. Let's deploy the damn this.&lt;/p&gt;

&lt;p&gt;First of all, we need to put this repo on Github.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FEW INTENSE GIT PUSHS LATER&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;on Github:- &lt;a href="https://github.com/nik-gautam/serverless-article"&gt;https://github.com/nik-gautam/serverless-article&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Onto Netlify
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;First, go to our profile and click the button "New Site From Git"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ozU69HoU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ozU69HoU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss1.png" alt="New site from Git"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Then, choose Github or wherever your repo is present.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J2AF7jJE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J2AF7jJE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss4.png" alt="Choose Github"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Then, choose your Repo.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RaTsEEq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RaTsEEq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss2.png" alt="Choose Repo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the required build command, which in our case is &lt;code&gt;yarn run lambda-build&lt;/code&gt;. Here, we also need to choose deploy branch which in out the case in the &lt;code&gt;master&lt;/code&gt; branch. Any other required deployment config is done here.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4qyXtG5M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4qyXtG5M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/nik-gautam/serverless-article/master/screenshots/ss3.png" alt="Add Build Command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AND DONE!!! Our functions are UP...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember, I said there would a change is routing, well, here it is.. to access the functions on the deployed server you would need to hit to following routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function/getTest.js -&amp;gt; http://localhost:9000/getTest -&amp;gt; https://{netlify-url}/.netlify/functions/getTest

function/getMichaelScott.js -&amp;gt; http://localhost:9000/getMichealScott -&amp;gt; https://{netlify-url}/.netlify/functions/getMichaelScott
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Check out &lt;a href="https://nifty-hopper-119d2a.netlify.app/.netlify/functions/getMichaelScott"&gt;https://nifty-hopper-119d2a.netlify.app/.netlify/functions/getMichaelScott&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also checkout Function Logs at Functions tab in Site Properties at Netlify.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o6fJ1BM7R2EBRDnxK/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o6fJ1BM7R2EBRDnxK/source.gif" alt="Congrats!!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, that was certainly something.&lt;/p&gt;

&lt;p&gt;Serverless is a really useful tech which I think more people should know and use.&lt;/p&gt;

&lt;p&gt;This was my first ever Tech Article and I hope to write more soon.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember this is not the end of Serverless&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next articles ( if I ever write them ) will be more focused on the practical application of this concept and I will show you how to take this from sheets to the streets.&lt;/p&gt;

&lt;p&gt;AND AS ALWAYS one more Michael Scott GIF&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ui1hpJSyBDWlG/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ui1hpJSyBDWlG/source.gif" alt="Wink Wink"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(and Forgive my bad jokes)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nik-gautam/serverless-article"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>aws</category>
      <category>serverless</category>
    </item>
  </channel>
</rss>
