diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..5b627cf --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,4 @@ +## Code of Conduct +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact +opensource-codeofconduct@amazon.com with any additional questions or comments. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c4b6a1c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,59 @@ +# Contributing Guidelines + +Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional +documentation, we greatly value feedback and contributions from our community. + +Please read through this document before submitting any issues or pull requests to ensure we have all the necessary +information to effectively respond to your bug report or contribution. + + +## Reporting Bugs/Feature Requests + +We welcome you to use the GitHub issue tracker to report bugs or suggest features. + +When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already +reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: + +* A reproducible test case or series of steps +* The version of our code being used +* Any modifications you've made relevant to the bug +* Anything unusual about your environment or deployment + + +## Contributing via Pull Requests +Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: + +1. You are working against the latest source on the *main* branch. +2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. +3. You open an issue to discuss any significant work - we would hate for your time to be wasted. + +To send us a pull request, please: + +1. Fork the repository. +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. +3. Ensure local tests pass. +4. Commit to your fork using clear commit messages. +5. Send us a pull request, answering any default questions in the pull request interface. +6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. + +GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and +[creating a pull request](https://help.github.com/articles/creating-a-pull-request/). + + +## Finding contributions to work on +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. + + +## Code of Conduct +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact +opensource-codeofconduct@amazon.com with any additional questions or comments. + + +## Security issue notifications +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. + + +## Licensing + +See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. diff --git a/README.md b/README.md index 490bb21..a6851d0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,85 @@ -# private +# How to Deploy a Node-Express App on AWS Lambda -hi +The purpose of this repository is to demonstrate how to deploy a simple web +application built by Express - Node.js web application framework on AWS Lambda. -This is Emin, Senior Software Engineer + +### Requirements + - `Node.js with Node Package Manager(npm)` should be installed on your local system. + + +### Architecture + +**Request Response Cycle :** + +

+ aws-lambda-demo-with-node-express +

+ + +### Prepare the code + + - Download the `sample_code` directory to your local system. + + - Run `npm install` command in the same directory of the downladed folder. `package.json` file includes necessary dependencies to generate `node_modules/` + + - Create a zip file that will include all files in the directory. + + + **Note:** Don't zip the folder itself, but select all files individually. Refer to below image if needed: + +

+ example_zip +

+ + +### Create a Lambda function + +- Give your function a name like "aws-lambda-demo-with-node-express". Basic permissions will be sufficient for the purpose of this demonstration + +- Upload zip file created on previous step through the console and click DEPLOY + +- 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 + + +### Integration with API Gateway + +- Create a new REST API and name it "aws-lambda-demo-with-node-express" + +- Create a method with ANY, use Lambda Proxy integration and select your function. Refer to below image if needed: + +

+ example_apigw_method +

+ + +- Create a resource, configure as proxy resource. Refer to below image if needed: + +

+ example_apigw_resource +

+ +- Use Lambda Function Proxy and select your function. + +- Deploy your API and name the stage "calculator". + + **Note:** This will be URL extension for our API, which needs to match with the html form inside our code. + + `
` + +- Now you can **[Invoke URL]** provided by +Amazon API Gateway. You can find this on Stages column of the console. + + +

+ invoke-url +

+ + +## Security + +See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. + +## License + +This library is licensed under the MIT-0 License. See the LICENSE file. diff --git a/diagram/architecture.png b/diagram/architecture.png new file mode 100644 index 0000000..3c868b8 Binary files /dev/null and b/diagram/architecture.png differ diff --git a/diagram/create_method.png b/diagram/create_method.png new file mode 100644 index 0000000..4247073 Binary files /dev/null and b/diagram/create_method.png differ diff --git a/diagram/create_resource.png b/diagram/create_resource.png new file mode 100644 index 0000000..bfdd161 Binary files /dev/null and b/diagram/create_resource.png differ diff --git a/diagram/create_zip.png b/diagram/create_zip.png new file mode 100644 index 0000000..212e14e Binary files /dev/null and b/diagram/create_zip.png differ diff --git a/diagram/invoke-url.png b/diagram/invoke-url.png new file mode 100644 index 0000000..606d88a Binary files /dev/null and b/diagram/invoke-url.png differ diff --git a/sample_code/app.js b/sample_code/app.js new file mode 100644 index 0000000..5628ee3 --- /dev/null +++ b/sample_code/app.js @@ -0,0 +1,72 @@ +// jshint esversion: 6 + +const express = require("express"); +var bodyParser = require("body-parser"); +const app = express(); +const port =3000 +app.use( + bodyParser.urlencoded({ + extended: true, + }) +); + +app.get("/", function (req, res) { + res.sendFile(__dirname + "/index.html"); +}); + +app.post("/", function (req, res) { + let num1 = Number(req.body.num1); + let num2 = Number(req.body["num2"]); + let operator = ""; + switch (req.body.operator) { + case "+": + operator = add; + break; + case "x": + operator = multiply; + break; + case "-": + operator = subtract; + break; + case "/": + operator = divide; + break; + } + console.log(calculator(num1, num2, operator)); + res.send(` +

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

+ Lambda-Icon +

+ There's no need for compliments

+

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

+

+ By the way I'm running on a Lambda Function +

+ + ` ); +}); + + +function add(num1, num2) { + return num1 + num2; +} + +function subtract(num1, num2) { + return num1 - num2; +} + +function multiply(num1, num2) { + return num1 * num2; +} +function divide(num1, num2) { + return num1 / num2; +} +function calculator(num1, num2, operator) { + return operator(num1, num2); +} + +app.listen(port, () => console.log(`calculator listening on port {port}!`)) + +module.exports = app diff --git a/sample_code/index.html b/sample_code/index.html new file mode 100644 index 0000000..a4e7f15 --- /dev/null +++ b/sample_code/index.html @@ -0,0 +1,69 @@ + + + + + + Simple Calculator + + + + +
+

Some Simple Math on Lambda

+
+ + +

+ +

+ +

+ + + + + + +
+
+ EC2-Icon + + diff --git a/sample_code/lambda.js b/sample_code/lambda.js new file mode 100644 index 0000000..fde7539 --- /dev/null +++ b/sample_code/lambda.js @@ -0,0 +1,8 @@ +"use strict"; +const awsServerlessExpress = require("aws-serverless-express") +const app = require("./app") +const server = awsServerlessExpress.createServer(app) + +exports.handler = (event, context) => { + awsServerlessExpress.proxy(server, event, context); +}; diff --git a/sample_code/package.json b/sample_code/package.json new file mode 100644 index 0000000..4511532 --- /dev/null +++ b/sample_code/package.json @@ -0,0 +1,15 @@ +{ + "name": "simple-calculator", + "version": "1.0.0", + "description": "Sample Node-Express app running on AWS Lambda", + "main": "calculator.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "buraku", + "license": "MIT-O", + "dependencies": { + "aws-serverless-express": "^3.3.8", + "express": "^4.17.1" + } +}