<?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: Ashwin Soni</title>
    <description>The latest articles on DEV Community by Ashwin Soni (@ashwinsoni).</description>
    <link>https://dev.to/ashwinsoni</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%2F125356%2Ff489dee1-a4ad-4803-90b0-aa6508621b69.jpg</url>
      <title>DEV Community: Ashwin Soni</title>
      <link>https://dev.to/ashwinsoni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashwinsoni"/>
    <language>en</language>
    <item>
      <title>Getting started with serverless functions using NodeJS and AWS</title>
      <dc:creator>Ashwin Soni</dc:creator>
      <pubDate>Sun, 15 Dec 2019 06:02:15 +0000</pubDate>
      <link>https://dev.to/ashwinsoni/getting-started-with-serverless-functions-using-nodejs-and-aws-42on</link>
      <guid>https://dev.to/ashwinsoni/getting-started-with-serverless-functions-using-nodejs-and-aws-42on</guid>
      <description>&lt;p&gt;&lt;i&gt;Originally published at &lt;a href="https://medium.com/@ashwinsoni/getting-started-with-serverless-functions-using-nodejs-and-aws-eef8c84abbf3"&gt;medium.com/@ashwinsoni&lt;/a&gt; on Dec 15, 2019&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jbk3ejWZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vuzdkt40y71njkh2lhyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jbk3ejWZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vuzdkt40y71njkh2lhyo.png" alt="node-lambda-love"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Image Source: &lt;a href="https://www.rubix.nl/blogs/how-setup-unit-testing-aws-lambda-serverless-functions-nodejs/"&gt;Rubix&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What are serverless functions?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Serverless doesn’t mean an absence of servers, but it means they are actually on a cloud. The challenge of managing these servers is taken away from you.&lt;/li&gt;
&lt;li&gt;Breaking the &lt;a href="https://en.wikipedia.org/wiki/Monolithic_application"&gt;Monolithic app&lt;/a&gt; into manageable &lt;a href="https://microservices.io/"&gt;Microservices&lt;/a&gt; is necessary for the ease of maintenance&lt;/li&gt;
&lt;li&gt;Microservices can be further broken down into function as a service viz. serverless functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2XQGSgu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w5g264ocf41yrl16ptty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2XQGSgu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w5g264ocf41yrl16ptty.png" alt="mono-micro-function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Image Source: &lt;a href="https://stackify.com/wp-content/uploads/2019/07/image-1.png"&gt;Stackify&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;There are various cloud provider which helps us with the serverless function,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.alibabacloud.com/products/function-compute"&gt;Alibaba Function Compute&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/lambda/features/"&gt;AWS Lambda Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://azure.microsoft.com/en-in/services/functions/"&gt;Azure Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/functions/"&gt;Google Cloud Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.ibm.com/functions/"&gt;IBM Cloud Functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;....and so on&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In this article, we will touch upon the &lt;a href="https://aws.amazon.com/lambda/features/"&gt;AWS Lambda Functions&lt;/a&gt; and go through steps for creating an AWS Lambda and deploying the same  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why and when do we need serverless functions?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When there is no need for server provisioning, monitoring, logging, or managing the underlying infrastructure. Instead, the focus is on your business logic, broken down into smaller, single-purpose functions&lt;/li&gt;
&lt;li&gt;In that way, a developer could dedicate most of the time on business logic which is very crucial for business and end-user&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There are many use cases like,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Transforming the unstructured data into the structured one &lt;/li&gt;
&lt;li&gt;Integrating the third party APIs&lt;/li&gt;
&lt;li&gt;Sending mail on successful operations&lt;/li&gt;
&lt;li&gt;Handling high transactional endpoint with high availability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;....and so on&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is AWS Lambda?
&lt;/h2&gt;

&lt;p&gt;As per definition from &lt;a href="https://aws.amazon.com/lambda/"&gt;official site of AWS&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In Laymen's term, Lambda functions are nothing but the piece of code/function which performs the stateless operation and can be run independently from main project codebase and deployed on cloud&lt;/li&gt;
&lt;li&gt;This flexibility of managing the piece of code independently helps us in ease of deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Write serverless AWS Lambda function using NodeJS
&lt;/h2&gt;

&lt;p&gt;Pre-requisite:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Need an &lt;a href="https://console.aws.amazon.com/"&gt;AWS account&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Need &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"&gt;AWS CLI installed&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html"&gt;configured on local&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Need &lt;a href="https://www.npmjs.com/package/serverless"&gt;serverless module&lt;/a&gt; globally installed on local along with &lt;a href="https://serverless.com/framework/docs/providers/aws/cli-reference/config-credentials/"&gt;AWS serverless configuration&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The Hello World project is a time-honored tradition in computer programming ~ Github&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let us continue the tradition...&lt;/p&gt;

&lt;p&gt;Let's understand the steps to create an AWS Lambda,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;a href="https://www.npmjs.com/package/serverless"&gt;serverless&lt;/a&gt; module globally on your local for managing serverless function
&amp;gt; yarn global add serverless&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a hello world template &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;serverless create --template hello-world --path hello-world-function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You should now have a folder structure as,&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello-world-function/
├── .gitignore
├── handler.js
└── serverless.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```.gitignore
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```js
'use strict';

module.exports.helloWorld = (event, context, callback) =&amp;gt; {
const response = {
    statusCode: 200,
    headers: {
    'Access-Control-Allow-Origin': '*', // Required for CORS support to work
    },
    body: JSON.stringify({
    message: 'Go Serverless v1.0! Your function executed successfully!',
    input: event,
    }),
};

callback(null, response);
};

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





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```yml
# Welcome to serverless. Read the docs
# https://serverless.com/framework/docs/

# Serverless.yml is the configuration the CLI
# uses to deploy your code to your provider of choice

# The `service` block is the name of the service
service: hello-world-function

# The `provider` block defines where your service will be deployed
provider:
    name: aws
    runtime: nodejs10.x

# The `functions` block defines what code to deploy
functions:
    helloWorld:
        handler: handler.helloWorld
        # The `events` block defines how to trigger the handler.helloWorld code
        events:
            - http:
                path: hello-world
                method: get
                cors: true

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



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Test/invoke on local&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;serverless invoke local --function helloWorld&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Deploy the code&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;serverless deploy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The output of the above command will deploy our Lambda function and give you the HTTP endpoint something like,&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;endpoints:
    GET - https://oyqliuc8hd.execute-api.us-east-1.amazonaws.com/dev/hello-world
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Also we can verify our code by going through the &lt;a href="https://console.aws.amazon.com/"&gt;AWS console&lt;/a&gt; under &lt;a href="https://console.aws.amazon.com/lambda/"&gt;Lambda service&lt;/a&gt; of the deployed region&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HCSH5oik--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pjm0nr45afsm09jrnlg6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HCSH5oik--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/pjm0nr45afsm09jrnlg6.png" alt="hello-world-code-on-aws"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Things to Note in &lt;code&gt;serverless.yml&lt;/code&gt; file&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- To deploy on a specific region, we need to add `region: &amp;lt;region-name&amp;gt;` node under the provider section. Otherwise, deployed to default configured region
- To trigger a Lambda function, we specify the type of trigger under `functions: events:` in the given `hello-world` template the trigger is an HTTP endpoint. We can always configure the triggering event as per our use case
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The aforementioned is the minimal configuration that is required to get started with AWS serverless function and everything under the hood like zipping, uploading the code to cloud, defining roles and permissions gets self-managed.&lt;/p&gt;

&lt;p&gt;One of the ways to manage and deploy serverless function manually by creating roles, permission, zipping and uploading code on the cloud. We may follow - &lt;a href="https://www.youtube.com/watch?v=3BlXU2zEzvY"&gt;Create AWS Lambda function using AWS CLI&lt;/a&gt;&lt;/p&gt;



&lt;center&gt;&lt;i&gt;Feel free to reach out in case of any queries or concern&lt;/i&gt;&lt;/center&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://serverless.com/framework/docs/providers/aws/cli-reference/"&gt;AWS Serverless CLI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackify.com/aws-lambda-with-node-js-a-complete-getting-started-guide/"&gt;AWS Lambda with NodeJS a complete getting started guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=71cd5XerKss"&gt;AWS Lambda Tutorial: Lambda + Serverless = HAPPY&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>aws</category>
      <category>serverless</category>
      <category>lambda</category>
    </item>
  </channel>
</rss>
