<?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: usufjameel</title>
    <description>The latest articles on DEV Community by usufjameel (@usufjameel).</description>
    <link>https://dev.to/usufjameel</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%2F789285%2F5cb59059-5456-4493-8e2d-2c9dd8227b9c.jpg</url>
      <title>DEV Community: usufjameel</title>
      <link>https://dev.to/usufjameel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/usufjameel"/>
    <language>en</language>
    <item>
      <title>How to Deploy Existing NodeJS Express application as an AWS Lambda Function using Serverless Framework</title>
      <dc:creator>usufjameel</dc:creator>
      <pubDate>Wed, 09 Nov 2022 06:52:19 +0000</pubDate>
      <link>https://dev.to/usufjameel/how-to-deploy-existing-nodejs-express-application-as-an-aws-lambda-function-using-serverless-framework-21b3</link>
      <guid>https://dev.to/usufjameel/how-to-deploy-existing-nodejs-express-application-as-an-aws-lambda-function-using-serverless-framework-21b3</guid>
      <description>&lt;p&gt;There are seven easy steps to deploy your existing NodeJS ExpressJS application as an AWS Lambda Function using Serverless Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Installing Dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init -y
$ npm install --save serverless-http
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Do not start the server instead export it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const serverless = require("serverless-http");
...
...
...
// app.listen(port, () =&amp;gt; {
//     console.log(`listening On PORT -&amp;gt; ${port} `);
// });

// Export your Express configuration wrapped into serverless function
module.exports.handler = serverless(app);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;install and Configure the Serverless Framework&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install -g serverless&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Prefix the command with sudo if you’re running this command on Linux.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sls config credentials --provider aws --key &amp;lt;aws_access_key_id&amp;gt; --secret &amp;lt;aws_secret_access_key&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;p&gt;Add &lt;a href="https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml"&gt;serverless.yml&lt;/a&gt; file in your project. Follow the &lt;a href="https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml"&gt;link&lt;/a&gt; for more details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: example-project

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1

  # you can add statements to the Lambda function's IAM Role here
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:PutObject"
      Resource:
        - "arn:aws:s3:::${file(config.json):S3_BUCKET_NAME}/*"

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: "ANY /{proxy+}"

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;p&gt;Deploy the project&lt;br&gt;
&lt;code&gt;$ sls deploy&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5
&lt;/h2&gt;

&lt;p&gt;We have to follow this step for production environment.&lt;br&gt;
Add the &lt;code&gt;NODE_ENV&lt;/code&gt; in the &lt;code&gt;secrets.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "NODE_ENV": "production"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6
&lt;/h2&gt;

&lt;p&gt;Add a reference for the &lt;code&gt;secrets.json&lt;/code&gt; in the &lt;code&gt;serverless.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: example-project

custom: # add these two lines
  secrets: ${file(secrets.json)} # reference the secrets.json file

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1

  # you can add statements to the Lambda function's IAM Role here
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:PutObject"
      Resource:
        - "arn:aws:s3:::${file(config.json):S3_BUCKET_NAME}/*"

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: "ANY /{proxy+}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 7
&lt;/h2&gt;

&lt;p&gt;That’s it! Delete the &lt;code&gt;node_modules&lt;/code&gt; and &lt;code&gt;.serverless&lt;/code&gt; folders from the service and run &lt;code&gt;npm install&lt;/code&gt; again, but this time with the &lt;code&gt;--production&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



</description>
      <category>node</category>
      <category>lambda</category>
      <category>aws</category>
      <category>serverless</category>
    </item>
    <item>
      <title>How to Deploy Existing NodeJS Express application as an AWS Lambda Function using ClaudiaJS</title>
      <dc:creator>usufjameel</dc:creator>
      <pubDate>Mon, 10 Jan 2022 06:45:07 +0000</pubDate>
      <link>https://dev.to/usufjameel/how-to-deploy-existing-nodejs-express-application-as-an-aws-lambda-function-using-claudiajs-2099</link>
      <guid>https://dev.to/usufjameel/how-to-deploy-existing-nodejs-express-application-as-an-aws-lambda-function-using-claudiajs-2099</guid>
      <description>&lt;p&gt;There are five easy steps to deploy your existing NodeJS ExpressJS application as an AWS Lambda Function using ClaudiaJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Do not start the server instead export it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app.listen(port, () =&amp;gt; {
//     console.log(`listening On PORT -&amp;gt; ${port} `);
// });

// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Create lambda.js file for creating a lambda handler&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eEN9E5y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws7xegk2ysy4daa1us2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eEN9E5y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws7xegk2ysy4daa1us2b.png" alt="File structure" width="310" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Use &lt;a href="https://www.npmjs.com/package/aws-serverless-express"&gt;aws-serverless-express&lt;/a&gt; for creating a lambda handler&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) =&amp;gt; awsServerlessExpress.proxy(server, event, context)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;p&gt;Install &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"&gt;aws-cli&lt;/a&gt;. Follow the link for details description for installation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MacOS(Commandline installer)&lt;/strong&gt;&lt;br&gt;
1) Download the file using the &lt;code&gt;curl&lt;/code&gt; command. The &lt;code&gt;-o&lt;/code&gt; option specifies the file name that the downloaded package is written to. In this example, the file is written to &lt;code&gt;AWSCLIV2.pkg&lt;/code&gt; in the current folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Run the standard macOS &lt;code&gt;installer&lt;/code&gt; program, specifying the downloaded &lt;code&gt;.pkg&lt;/code&gt; file as the source. Use the &lt;code&gt;-pkg&lt;/code&gt; parameter to specify the name of the package to install, and the &lt;code&gt;-target /&lt;/code&gt; parameter for which drive to install the package to. The files are installed to &lt;code&gt;/usr/local/aws-cli&lt;/code&gt;, and a symlink is automatically created in &lt;code&gt;/usr/local/bin&lt;/code&gt;. You must include sudo on the command to grant write permissions to those folders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo installer -pkg ./AWSCLIV2.pkg -target /
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation is complete, debug logs are written to &lt;code&gt;/var/log/install.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;3) To verify that the shell can find and run the &lt;code&gt;aws&lt;/code&gt; command in your &lt;code&gt;$PATH&lt;/code&gt;, use the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ which aws
/usr/local/bin/aws 
$ aws --version
aws-cli/2.4.5 Python/3.8.8 Darwin/18.7.0 botocore/2.4.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;aws&lt;/code&gt; command cannot be found, you may need to restart your terminal or follow the instructions in &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-path.html"&gt;Adding the AWS CLI to your path&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;4) &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html"&gt;Configure aws-cli&lt;/a&gt;&lt;br&gt;
The following example shows sample values. Replace them with your own values as described in the following sections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5
&lt;/h2&gt;

&lt;p&gt;Deploy your application using &lt;a href="https://claudiajs.com/tutorials/installing.html"&gt;ClaudiaJS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;1) Install claudiajs&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;2) Claudia was installed correctly by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   claudia --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) The following command will create the lambda function and api gateway application for you to access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   claudia create --deploy-proxy-api --region &amp;lt;region_name 
eg. us-east-1&amp;gt; --handler lambda.handler --name &amp;lt;name_of_the_function&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--deploy-proxy-api&lt;/code&gt; : If specified, a proxy API will be created for the Lambda function on API Gateway, and forward all requests to function.This is an alternative way to create web APIs to &lt;code&gt;--api-module&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--region&lt;/code&gt; : AWS region where to create the lambda. For supported values, see &lt;a href="https://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region"&gt;https://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region&lt;/a&gt; for example: us-east-1&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--handler&lt;/code&gt; : Main function for Lambda to execute, as &lt;code&gt;module.function&lt;/code&gt; for example: if it is in the &lt;code&gt;main.js&lt;/code&gt; file and exported as router, this would be &lt;code&gt;main.router&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name&lt;/code&gt; : Lambda function name for example: awesome-microservice defaults to: the project name from &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Command for updating the lambda function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;claudia update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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