<?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: Gökhan Olgun</title>
    <description>The latest articles on DEV Community by Gökhan Olgun (@gkhn).</description>
    <link>https://dev.to/gkhn</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%2F182337%2Fc4312d16-c6d1-41dc-bb96-6349dade7dae.jpeg</url>
      <title>DEV Community: Gökhan Olgun</title>
      <link>https://dev.to/gkhn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gkhn"/>
    <language>en</language>
    <item>
      <title>Serverless application development with Node.js on AWS platform using Serverless framework</title>
      <dc:creator>Gökhan Olgun</dc:creator>
      <pubDate>Fri, 13 Dec 2019 13:45:46 +0000</pubDate>
      <link>https://dev.to/itnext/serverless-application-development-with-node-js-on-aws-platform-using-serverless-framework-5816</link>
      <guid>https://dev.to/itnext/serverless-application-development-with-node-js-on-aws-platform-using-serverless-framework-5816</guid>
      <description>&lt;p&gt;In this tutorial, we will &lt;strong&gt;create and deploy a serverless Node.js application&lt;/strong&gt; on the &lt;strong&gt;AWS&lt;/strong&gt; platform using various &lt;strong&gt;AWS services&lt;/strong&gt; such as &lt;strong&gt;Lambda&lt;/strong&gt; , &lt;strong&gt;Layers&lt;/strong&gt; , &lt;strong&gt;DynamoDB&lt;/strong&gt; , &lt;strong&gt;API Gateway&lt;/strong&gt; using the &lt;a href="https://serverless.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;Serverless framework&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg05tyaa0z80uum0txl2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg05tyaa0z80uum0txl2r.png" width="800" height="800"&gt;&lt;/a&gt;source: &lt;a href="https://serverless.com/" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="http://serverless.com" rel="noopener noreferrer"&gt;http://serverless.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s install the Serverless framework first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo npm install -g serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After installation is complete, &lt;strong&gt;you need to configure your AWS credentials&lt;/strong&gt;. You can find the needed information below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://serverless.com/framework/docs/providers/aws/guide/credentials/" rel="noopener noreferrer"&gt;Serverless Framework - AWS Lambda Guide - Credentials&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ll go with the aws configure option to keep things simple for now but I &lt;strong&gt;strongly recommend you to use &lt;em&gt;IAM roles&lt;/em&gt;&lt;/strong&gt;  instead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9o9rhscirf1noj7xvqxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9o9rhscirf1noj7xvqxp.png" width="534" height="159"&gt;&lt;/a&gt;AWS SDK configuration&lt;/p&gt;

&lt;p&gt;After completing the configuration you can test the Serverless framework with the following commands.&lt;/p&gt;

&lt;p&gt;Create a new service using aws-nodejs template:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls create --template aws-nodejs --path myService
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Serverless CLI will create an empty hello world service for you with a serverless.yml and handler.js which &lt;a href="https://serverless.com/framework/docs/providers/aws/examples/hello-world/node/" rel="noopener noreferrer"&gt;you can test&lt;/a&gt; your serverless configuration.&lt;/p&gt;

&lt;p&gt;What we’re going to do is creating a serverless application from scratch and deploying it to the AWS platform.&lt;/p&gt;

&lt;p&gt;First, create serverless.yml file in the root of your project directory.&lt;/p&gt;

&lt;p&gt;Let’s start configuring our serverless application.&lt;/p&gt;

&lt;p&gt;In this part of serverless.yml file, we define our service and provider resources we’re going to use. The provider is AWS in our case.&lt;/p&gt;

&lt;p&gt;Add the following lines to serverless.yml file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We configured our service on dev stage and &lt;strong&gt;assigned the required permissions&lt;/strong&gt; to access messages DynamoDB table; hence, we need to create messages table.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now it’s time to &lt;strong&gt;create our Lambda functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Create a functions directory in the root of your project directory and put the following JavaScript files in it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;getMessages Lambda function will let us &lt;strong&gt;retrieve messages from our DynamoDB table.&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;putMessage Lambda function will let us &lt;strong&gt;put messages to our DynamoDB table.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, we’re ready to configure our Lambda functions in serverless.yml file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We configured our function handlers and &lt;strong&gt;connected them to HTTP end-points using API Gateway.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You probably noticed that we put a reference to our Layers within our application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;From AWS documentation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;layer&lt;/strong&gt; is a ZIP archive that contains libraries, a &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html" rel="noopener noreferrer"&gt;custom runtime&lt;/a&gt;, or other dependencies. &lt;strong&gt;With layers, you can use libraries in your function without needing to include them in your deployment package.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important note about Layers:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your function can access the content of the layer &lt;strong&gt;during execution in the&lt;/strong&gt;  &lt;strong&gt;/opt directory&lt;/strong&gt;. Layers are applied in the order that's specified, merging any folders with the same name. If the same file appears in multiple layers, the version in the last applied layer is used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now it’s time to configure our Layers.&lt;/p&gt;

&lt;p&gt;Create layers/reverser directory in the root of your project directory and put the following JavaScript file in it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is just a &lt;strong&gt;simple JavaScript module to reverse the input string&lt;/strong&gt; that we’re going to use to test our Layers implementation.&lt;/p&gt;

&lt;p&gt;Now, let’s add the following lines to serverless.yml file to configure Reverser Layer that we’ve just created.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important note from AWS documentation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function can &lt;strong&gt;use up to 5 layers at a time&lt;/strong&gt;. The total unzipped size of the function and all layers &lt;strong&gt;can’t exceed&lt;/strong&gt; the unzipped deployment package size &lt;strong&gt;limit of 250 MB&lt;/strong&gt;. For more information, see &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/limits.html" rel="noopener noreferrer"&gt;AWS Lambda Limits&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, &lt;strong&gt;we’re ready to deploy our serverless application&lt;/strong&gt; to the AWS platform by executing the following command in our terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result of this execution, you should see a screen similar to the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdgoi7eia2e5eyss7par.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdgoi7eia2e5eyss7par.png" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To test your application you can use endpoints stated in the output of &lt;strong&gt;your&lt;/strong&gt; sls deploy or you can &lt;strong&gt;invoke your Lambda functions&lt;/strong&gt; with sls CLI in the root directory of your project.&lt;/p&gt;

&lt;p&gt;Let’s try with sls CLI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls invoke -f putMessage --data '{"message":"test message"}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result should be as following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbyg7mvykicrso5tbc45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbyg7mvykicrso5tbc45.png" width="729" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s try to get the messages we put to messages table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls invoke -f getMessage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result should be as following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmck0i4eztv9dk9sshvpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmck0i4eztv9dk9sshvpt.png" width="667" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, you can see your messages &lt;strong&gt;in your DynamoDB table&lt;/strong&gt; on your &lt;strong&gt;AWS Console&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F014g7e4eq2xmkzwm0yj3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F014g7e4eq2xmkzwm0yj3.png" width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;String reverse module worked well and the message in the table seems reversed.&lt;/p&gt;

&lt;p&gt;You can find the project containing files used in this tutorial in the &lt;a href="https://github.com/gkhn/ServerlessAppWithLayers" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt; below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gkhn/ServerlessAppWithLayers" rel="noopener noreferrer"&gt;gkhn/ServerlessAppWithLayers&lt;/a&gt;&lt;/p&gt;




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