├── .gitignore ├── README.md ├── handler.js └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # package directories 2 | node_modules 3 | jspm_packages 4 | 5 | # Serverless directories 6 | .serverless -------------------------------------------------------------------------------- /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 | Deploying aws-node-http-api-project to stage dev (us-east-1) 31 | 32 | ✔ Service deployed to stack aws-node-http-api-project-dev (152s) 33 | 34 | endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/ 35 | functions: 36 | hello: aws-node-http-api-project-dev-hello (1.9 kB) 37 | ``` 38 | 39 | _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/). 40 | 41 | ### Invocation 42 | 43 | After successful deployment, you can call the created application via HTTP: 44 | 45 | ```bash 46 | curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/ 47 | ``` 48 | 49 | Which should result in response similar to the following (removed `input` content for brevity): 50 | 51 | ```json 52 | { 53 | "message": "Go Serverless v2.0! Your function executed successfully!", 54 | "input": { 55 | ... 56 | } 57 | } 58 | ``` 59 | 60 | ### Local development 61 | 62 | You can invoke your function locally by using the following command: 63 | 64 | ```bash 65 | serverless invoke local --function hello 66 | ``` 67 | 68 | Which should result in response similar to the following: 69 | 70 | ``` 71 | { 72 | "statusCode": 200, 73 | "body": "{\n \"message\": \"Go Serverless v3.0! Your function executed successfully!\",\n \"input\": \"\"\n}" 74 | } 75 | ``` 76 | 77 | 78 | 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: 79 | 80 | ```bash 81 | serverless plugin install -n serverless-offline 82 | ``` 83 | 84 | It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`. 85 | 86 | After installation, you can start local emulation with: 87 | 88 | ``` 89 | serverless offline 90 | ``` 91 | 92 | To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline). 93 | -------------------------------------------------------------------------------- /handler.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 v3.0! Your function executed successfully!", 9 | input: event, 10 | }, 11 | null, 12 | 2 13 | ), 14 | }; 15 | }; 16 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | org: trainwithshubham 2 | app: aws-node-http-api-project 3 | service: aws-node-http-api-project 4 | frameworkVersion: '3' 5 | 6 | provider: 7 | name: aws 8 | runtime: nodejs14.x 9 | region: us-east-1 10 | 11 | functions: 12 | hello: 13 | handler: handler.hello 14 | events: 15 | - httpApi: 16 | path: / 17 | method: get 18 | --------------------------------------------------------------------------------