├── .gitignore ├── package.json ├── src ├── hello.js ├── getTasks.js ├── getTask.js ├── deleteTask.js ├── updateTask.js └── addTask.js ├── requests └── test.http ├── serverless.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # package directories 2 | node_modules 3 | jspm_packages 4 | 5 | # Serverless directories 6 | .serverless -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@middy/core": "^2.5.3", 4 | "@middy/http-json-body-parser": "^2.5.3", 5 | "aws-sdk": "^2.1044.0", 6 | "uuid": "^8.3.2" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/hello.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports.hello = async (event) => { 4 | return { 5 | statusCode: 200, 6 | body: JSON.stringify( 7 | { 8 | message: "Go Serverless v2.0! Your function executed successfully!", 9 | input: event, 10 | }, 11 | null, 12 | 2 13 | ), 14 | }; 15 | }; 16 | -------------------------------------------------------------------------------- /src/getTasks.js: -------------------------------------------------------------------------------- 1 | const AWS = require("aws-sdk"); 2 | 3 | const getTasks = async (event) => { 4 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 5 | 6 | const result = await dynamodb.scan({ TableName: "TaskTable" }).promise(); 7 | 8 | const tasks = result.Items; 9 | 10 | return { 11 | status: 200, 12 | body: { 13 | tasks, 14 | }, 15 | }; 16 | }; 17 | 18 | module.exports = { 19 | getTasks, 20 | }; 21 | -------------------------------------------------------------------------------- /src/getTask.js: -------------------------------------------------------------------------------- 1 | const AWS = require("aws-sdk"); 2 | 3 | const getTask = async (event) => { 4 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 5 | 6 | const { id } = event.pathParameters; 7 | 8 | const result = await dynamodb 9 | .get({ 10 | TableName: "TaskTable", 11 | Key: { id }, 12 | }) 13 | .promise(); 14 | 15 | const task = result.Item; 16 | 17 | return { 18 | status: 200, 19 | body: task, 20 | }; 21 | }; 22 | 23 | module.exports = { 24 | getTask, 25 | }; 26 | -------------------------------------------------------------------------------- /src/deleteTask.js: -------------------------------------------------------------------------------- 1 | const AWS = require("aws-sdk"); 2 | 3 | const deleteTask = async (event) => { 4 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 5 | const { id } = event.pathParameters; 6 | 7 | await dynamodb 8 | .delete({ 9 | TableName: "TaskTable", 10 | Key: { 11 | id, 12 | }, 13 | }) 14 | .promise(); 15 | 16 | return { 17 | status: 200, 18 | body: { 19 | message: 'Deleted Task' 20 | } 21 | }; 22 | }; 23 | 24 | module.exports = { 25 | deleteTask, 26 | }; 27 | -------------------------------------------------------------------------------- /requests/test.http: -------------------------------------------------------------------------------- 1 | @api = https://c7nftp7820.execute-api.us-west-2.amazonaws.com 2 | 3 | GET {{api}} 4 | 5 | ### create task 6 | POST {{api}}/tasks 7 | Content-Type: application/json 8 | 9 | { 10 | "title": "my four task", 11 | "description": "some four created" 12 | } 13 | 14 | ### get tasks 15 | GET {{api}}/tasks 16 | 17 | ### get single task 18 | GET {{api}}/tasks/0c3378f1-d857-4db4-a283-e1ca2074ab2d 19 | 20 | ### update single task 21 | PUT {{api}}/tasks/0c3378f1-d857-4db4-a283-e1ca2074ab2d 22 | Content-Type: application/json 23 | 24 | { 25 | "done": true 26 | } 27 | 28 | ### delete single task 29 | DELETE {{api}}/tasks/5a2aefd9-7d78-4bbb-a02b-78505679db91 -------------------------------------------------------------------------------- /src/updateTask.js: -------------------------------------------------------------------------------- 1 | const uuid = require("uuid"); 2 | const AWS = require("aws-sdk"); 3 | 4 | const updateTask = async (event) => { 5 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 6 | const { id } = event.pathParameters; 7 | 8 | const { done } = JSON.parse(event.body); 9 | 10 | await dynamodb 11 | .update({ 12 | TableName: "TaskTable", 13 | Key: { id }, 14 | UpdateExpression: "set done = :done", 15 | ExpressionAttributeValues: { 16 | ":done": done, 17 | }, 18 | ReturnValues: "ALL_NEW", 19 | }) 20 | .promise(); 21 | 22 | return { 23 | statusCode: 200, 24 | body: JSON.stringify({ 25 | message: "task updated", 26 | }), 27 | }; 28 | }; 29 | 30 | module.exports = { 31 | updateTask, 32 | }; 33 | -------------------------------------------------------------------------------- /src/addTask.js: -------------------------------------------------------------------------------- 1 | const { v4 } = require("uuid"); 2 | const AWS = require("aws-sdk"); 3 | 4 | const middy = require("@middy/core"); 5 | const httpJSONBodyParser = require("@middy/http-json-body-parser"); 6 | 7 | const addTask = async (event) => { 8 | const dynamodb = new AWS.DynamoDB.DocumentClient(); 9 | 10 | const { title, description } = event.body; 11 | const createdAt = new Date(); 12 | const id = v4(); 13 | 14 | console.log("created id: ", id); 15 | 16 | const newTask = { 17 | id, 18 | title, 19 | description, 20 | createdAt, 21 | done: false, 22 | }; 23 | 24 | await dynamodb 25 | .put({ 26 | TableName: "TaskTable", 27 | Item: newTask, 28 | }) 29 | .promise(); 30 | 31 | return { 32 | statusCode: 200, 33 | body: JSON.stringify(newTask), 34 | }; 35 | }; 36 | 37 | module.exports = { 38 | addTask: middy(addTask).use(httpJSONBodyParser()), 39 | }; 40 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: node-aws-lambda-crud 2 | frameworkVersion: '2 || 3' 3 | 4 | provider: 5 | name: aws 6 | runtime: nodejs12.x 7 | lambdaHashingVersion: '20201221' 8 | region: us-west-2 9 | iamRoleStatements: 10 | - Effect: Allow 11 | Action: 12 | - dynamodb:* 13 | Resource: 14 | - arn:aws:dynamodb:us-west-2:793282269851:table/TaskTable 15 | 16 | functions: 17 | hello: 18 | handler: src/hello.hello 19 | events: 20 | - httpApi: 21 | path: / 22 | method: get 23 | createTask: 24 | handler: src/addTask.addTask 25 | events: 26 | - httpApi: 27 | path: /tasks 28 | method: post 29 | getTasks: 30 | handler: src/getTasks.getTasks 31 | events: 32 | - httpApi: 33 | path: /tasks 34 | method: get 35 | getTask: 36 | handler: src/getTask.getTask 37 | events: 38 | - httpApi: 39 | path: /tasks/{id} 40 | method: get 41 | updateTask: 42 | handler: src/updateTask.updateTask 43 | events: 44 | - httpApi: 45 | path: /tasks/{id} 46 | method: put 47 | deleteTask: 48 | handler: src/deleteTask.deleteTask 49 | events: 50 | - httpApi: 51 | path: /tasks/{id} 52 | method: delete 53 | 54 | resources: 55 | Resources: 56 | TaskTable: 57 | Type: AWS::DynamoDB::Table 58 | Properties: 59 | TableName: TaskTable 60 | BillingMode: PAY_PER_REQUEST 61 | AttributeDefinitions: 62 | - AttributeName: id 63 | AttributeType: S 64 | KeySchema: 65 | - AttributeName: id 66 | KeyType: HASH -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 12 | 13 | # Serverless Framework Node HTTP API on AWS 14 | 15 | This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework. 16 | 17 | This template does not include any kind of persistence (database). For more advanced examples, check out the [serverless/examples repository](https://github.com/serverless/examples/) which includes Typescript, Mongo, DynamoDB and other examples. 18 | 19 | ## Usage 20 | 21 | ### Deployment 22 | 23 | ``` 24 | $ serverless deploy 25 | ``` 26 | 27 | After deploying, you should see output similar to: 28 | 29 | ```bash 30 | Serverless: Packaging service... 31 | Serverless: Excluding development dependencies... 32 | Serverless: Creating Stack... 33 | Serverless: Checking Stack create progress... 34 | ........ 35 | Serverless: Stack create finished... 36 | Serverless: Uploading CloudFormation file to S3... 37 | Serverless: Uploading artifacts... 38 | Serverless: Uploading service aws-node-http-api.zip file to S3 (711.23 KB)... 39 | Serverless: Validating template... 40 | Serverless: Updating Stack... 41 | Serverless: Checking Stack update progress... 42 | ................................. 43 | Serverless: Stack update finished... 44 | Service Information 45 | service: serverless-http-api 46 | stage: dev 47 | region: us-east-1 48 | stack: serverless-http-api-dev 49 | resources: 12 50 | api keys: 51 | None 52 | endpoints: 53 | ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/ 54 | functions: 55 | api: serverless-http-api-dev-hello 56 | layers: 57 | None 58 | ``` 59 | 60 | _Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). 61 | 62 | ### Invocation 63 | 64 | After successful deployment, you can call the created application via HTTP: 65 | 66 | ```bash 67 | curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/ 68 | ``` 69 | 70 | Which should result in response similar to the following (removed `input` content for brevity): 71 | 72 | ```json 73 | { 74 | "message": "Go Serverless v2.0! Your function executed successfully!", 75 | "input": { 76 | ... 77 | } 78 | } 79 | ``` 80 | 81 | ### Local development 82 | 83 | You can invoke your function locally by using the following command: 84 | 85 | ```bash 86 | serverless invoke local --function hello 87 | ``` 88 | 89 | Which should result in response similar to the following: 90 | 91 | ``` 92 | { 93 | "statusCode": 200, 94 | "body": "{\n \"message\": \"Go Serverless v2.0! Your function executed successfully!\",\n \"input\": \"\"\n}" 95 | } 96 | ``` 97 | 98 | 99 | Alternatively, it is also possible to emulate API Gateway and Lambda locally by using `serverless-offline` plugin. In order to do that, execute the following command: 100 | 101 | ```bash 102 | serverless plugin install -n serverless-offline 103 | ``` 104 | 105 | It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`. 106 | 107 | After installation, you can start local emulation with: 108 | 109 | ``` 110 | serverless offline 111 | ``` 112 | 113 | To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline). 114 | --------------------------------------------------------------------------------