<?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: SWAPNIL SHARMA</title>
    <description>The latest articles on DEV Community by SWAPNIL SHARMA (@nilinswap).</description>
    <link>https://dev.to/nilinswap</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%2F245357%2F64630765-5887-4a10-8aeb-0da46bd20c8f.jpeg</url>
      <title>DEV Community: SWAPNIL SHARMA</title>
      <link>https://dev.to/nilinswap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nilinswap"/>
    <language>en</language>
    <item>
      <title>A Simple Node App on AWS Lambda</title>
      <dc:creator>SWAPNIL SHARMA</dc:creator>
      <pubDate>Sun, 13 Oct 2019 14:15:25 +0000</pubDate>
      <link>https://dev.to/nilinswap/a-simple-node-app-on-aws-lambda-5gid</link>
      <guid>https://dev.to/nilinswap/a-simple-node-app-on-aws-lambda-5gid</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;This is a small tutorial on how to build a simple node app from scratch and to put that up on AWS-Lambda.&lt;/p&gt;

&lt;h1&gt;
  
  
  Contents
&lt;/h1&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I. Make a Simple Node App
II. Host the app as a function in AWS-Lambda.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;
  
  
  A Simple Node App
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install Node( for Mac)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install nodejs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Install node with binary located at&lt;br&gt;
&lt;code&gt;/usr/local/opt/node&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create Container&lt;br&gt;
 Create the package folder and get inside it. Say the name is 'puppdf.'&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir puppdf&lt;/code&gt;&lt;br&gt;
   &lt;code&gt;cd puppdf&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Initialise the project&lt;/p&gt;

&lt;p&gt;Intialization is the creation of 'package.json', a configuration (json) file for our app.&lt;/p&gt;

&lt;p&gt;a. Using Terminal Prompt&lt;br&gt;
    Run the following and enter appropriate info on prompt&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`npm init`
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;b. Manually Create&lt;br&gt;
    Create a package.json file and enter the fields and keys. For template one can pick from below&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "puppdf",
  "version": "1.0.0",
  "description": "convert html to pdf using puppeteer",
  "main": "src/pdf.js",
  "directories": {
    "src": "src"
  },
  "files": [
    "src"
  ],
  "scripts": {
    "start:dev": "node src/pdf.js",
    "start:build": "npm install &amp;amp;&amp;amp; npm run-script build",
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/nilinswap/puppdf.git"
  },
  "engines": {
    "node": "&amp;gt;=10.16.3"
  },
  "keywords": [
    "pdf",
    "node",
    "puppeteer"
  ],
  "author": "Swapnil Sharma &amp;lt;nilinswap@github.com&amp;gt;",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/nilinswap/puppdf/issues"
  },
  "homepage": "https://github.com/nilinswap/puppdf#readme",
  "dependencies": {
    "puppeteer": "^1.20.0"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;create a folder src and add a file named pdf.rs in it. This file takes an url and converts related html to pdf
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//pdf.rs

const puppeteer = require('puppeteer');
(async () =&amp;gt; {
    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    await page.goto('https://www.medium.com')
    await page.pdf({path: 'medium.pdf', format: 'A4'})
    await browser.close()
})()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this point, The app is in running state. Let's try to put it up in AWS-Lambda.&lt;/p&gt;

&lt;h1&gt;
  
  
  Put it up on Lambda
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install serverless framework&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install -g serverless&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an &lt;a href="https://aws.amazon.com/"&gt;AWS&lt;/a&gt; Account.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a user with programmatic access permission.&lt;br&gt;
follow &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html"&gt;this&lt;/a&gt; tutorial for it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  using serverless
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Configure serverless CLI with your AWS credentials. This is necessary for deployment.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;serverless config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create essentials for serverless &lt;code&gt;serverless.yml&lt;/code&gt; and &lt;code&gt;handler.js&lt;/code&gt;&lt;br&gt;
If there is absence of a template file, run &lt;code&gt;serverless create --template aws-nodejs --path test&lt;/code&gt;&lt;br&gt;
to generate default files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After reviewing serverless.yml, run &lt;code&gt;serverless deploy&lt;/code&gt;. Before running this make sure that node_modules/ folder is part of the package.&lt;br&gt;
If the package is too big one might need to use S3 for loading zip.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  using AWS Console
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;create index.js and write handler functions there.&lt;br&gt;
add package.json (like mentioned above) and have 'node_modules/' prepared (by running &lt;code&gt;npm install&lt;/code&gt;)&lt;br&gt;
and zip up the index.js and node_modules using&lt;br&gt;
&lt;br&gt;
&lt;code&gt;zip -r index.zip index.js node_modules&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;and upload this zip in AWS-lambda create function set.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.helloWorld = function (event, context, callback) {
let requestBody = JSON.parse(event.body);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;p&gt;index.js&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Register a Test Event in Test action and test.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Trigger(I used api-endpoint for trigger.) and use your lambda function in production. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is mostly written for me to refer it in future.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
