├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── diagram ├── architecture.png ├── create_method.png ├── create_resource.png ├── create_zip.png └── invoke-url.png └── sample_code ├── app.js ├── index.html ├── lambda.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build 3 | npm-debug.log 4 | .env 5 | .gitignore 6 | .DS_Store 7 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to Deploy a Node-Express App on AWS Lambda 2 | 3 | The purpose of this repository is to demonstrate how to deploy a simple web 4 | application built by Express - Node.js web application framework on AWS Lambda. 5 | 6 | 7 | ### Requirements 8 | - `Node.js with Node Package Manager(npm)` should be installed on your local system. 9 | 10 | 11 | ### Architecture 12 | 13 | **Request Response Cycle :** 14 | 15 |

16 | aws-lambda-demo-with-node-express 17 |

18 | 19 | 20 | ### Prepare the code 21 | 22 | - Download the `sample_code` directory to your local system. 23 | 24 | - Run `npm install` command in the same directory of the downladed folder. `package.json` file includes necessary dependencies to generate `node_modules/` 25 | 26 | - Create a zip file that will include all files in the directory. 27 | 28 | 29 | **Note:** Don't zip the folder itself, but select all files individually. Refer to below image if needed: 30 | 31 |

32 | example_zip 33 |

34 | 35 | 36 | ### Create a Lambda function 37 | 38 | - Give your function a name like "aws-lambda-demo-with-node-express". Basic permissions will be sufficient for the purpose of this demonstration 39 | 40 | - Upload zip file created on previous step through the console and click DEPLOY 41 | 42 | - Rename your handler as "lambda.handler". It is the main module that uses [`aws-serverless-express`](https://github.com/awslabs/aws-serverless-express) package to easily get the event object Lambda receives from API Gateway 43 | 44 | 45 | ### Integration with API Gateway 46 | 47 | - Create a new REST API and name it "aws-lambda-demo-with-node-express" 48 | 49 | - Create a method with ANY, use Lambda Proxy integration and select your function. Refer to below image if needed: 50 | 51 |

52 | example_apigw_method 53 |

54 | 55 | 56 | - Create a resource, configure as proxy resource. Refer to below image if needed: 57 | 58 |

59 | example_apigw_resource 60 |

61 | 62 | - Use Lambda Function Proxy and select your function. 63 | 64 | - Deploy your API and name the stage "calculator". 65 | 66 | **Note:** This will be URL extension for our API, which needs to match with the html form inside our code. 67 | 68 | `
` 69 | 70 | - Now you can **[Invoke URL]** provided by 71 | Amazon API Gateway. You can find this on Stages column of the console. 72 | 73 | 74 |

75 | invoke-url 76 |

77 | 78 | 79 | ## Security 80 | 81 | See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. 82 | 83 | ## License 84 | 85 | This library is licensed under the MIT-0 License. See the LICENSE file. 86 | -------------------------------------------------------------------------------- /diagram/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-demo-with-node-express/048de0d7e9a4d2880e2a87a4f10c6f443fa69e83/diagram/architecture.png -------------------------------------------------------------------------------- /diagram/create_method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-demo-with-node-express/048de0d7e9a4d2880e2a87a4f10c6f443fa69e83/diagram/create_method.png -------------------------------------------------------------------------------- /diagram/create_resource.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-demo-with-node-express/048de0d7e9a4d2880e2a87a4f10c6f443fa69e83/diagram/create_resource.png -------------------------------------------------------------------------------- /diagram/create_zip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-demo-with-node-express/048de0d7e9a4d2880e2a87a4f10c6f443fa69e83/diagram/create_zip.png -------------------------------------------------------------------------------- /diagram/invoke-url.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-demo-with-node-express/048de0d7e9a4d2880e2a87a4f10c6f443fa69e83/diagram/invoke-url.png -------------------------------------------------------------------------------- /sample_code/app.js: -------------------------------------------------------------------------------- 1 | // jshint esversion: 6 2 | 3 | const express = require("express"); 4 | var bodyParser = require("body-parser"); 5 | const app = express(); 6 | const port =3000 7 | app.use( 8 | bodyParser.urlencoded({ 9 | extended: true, 10 | }) 11 | ); 12 | 13 | app.get("/", function (req, res) { 14 | res.sendFile(__dirname + "/index.html"); 15 | }); 16 | 17 | app.post("/", function (req, res) { 18 | let num1 = Number(req.body.num1); 19 | let num2 = Number(req.body["num2"]); 20 | let operator = ""; 21 | switch (req.body.operator) { 22 | case "+": 23 | operator = add; 24 | break; 25 | case "x": 26 | operator = multiply; 27 | break; 28 | case "-": 29 | operator = subtract; 30 | break; 31 | case "/": 32 | operator = divide; 33 | break; 34 | } 35 | console.log(calculator(num1, num2, operator)); 36 | res.send(` 37 |

38 | That was easy, your result is: ${calculator(num1, num2, operator)} 39 |

40 | Lambda-Icon 41 |

42 | There's no need for compliments

43 |

I already know i'm the smartest app in the world hahaha ;)

44 |

45 | By the way I'm running on a Lambda Function 46 |

47 | 48 | ` ); 49 | }); 50 | 51 | 52 | function add(num1, num2) { 53 | return num1 + num2; 54 | } 55 | 56 | function subtract(num1, num2) { 57 | return num1 - num2; 58 | } 59 | 60 | function multiply(num1, num2) { 61 | return num1 * num2; 62 | } 63 | function divide(num1, num2) { 64 | return num1 / num2; 65 | } 66 | function calculator(num1, num2, operator) { 67 | return operator(num1, num2); 68 | } 69 | 70 | app.listen(port, () => console.log(`calculator listening on port {port}!`)) 71 | 72 | module.exports = app 73 | -------------------------------------------------------------------------------- /sample_code/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Simple Calculator 7 | 46 | 47 | 48 | 49 |
50 |

Some Simple Math on Lambda

51 |
52 | 53 | 54 |

55 | 56 |

57 | 58 |

59 | 60 | 61 | 62 | 63 | 64 | 65 |
66 |
67 | EC2-Icon 68 | 69 | 70 | -------------------------------------------------------------------------------- /sample_code/lambda.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const awsServerlessExpress = require("aws-serverless-express") 3 | const app = require("./app") 4 | const server = awsServerlessExpress.createServer(app) 5 | 6 | exports.handler = (event, context) => { 7 | awsServerlessExpress.proxy(server, event, context); 8 | }; 9 | -------------------------------------------------------------------------------- /sample_code/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-calculator", 3 | "version": "1.0.0", 4 | "description": "Sample Node-Express app running on AWS Lambda", 5 | "main": "calculator.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "buraku", 10 | "license": "MIT-O", 11 | "dependencies": { 12 | "aws-serverless-express": "^3.3.8", 13 | "express": "^4.17.1" 14 | } 15 | } 16 | --------------------------------------------------------------------------------