You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: 'Serverless Framework Node Express API on AWS'
3
-
description: 'This template demonstrates how to develop and deploy a simple Node Express API running on AWS Lambda using the traditional Serverless Framework.'
3
+
description: 'This template demonstrates how to develop and deploy a simple Node Express API running on AWS Lambda using the Serverless Framework.'
This template demonstrates how to develop and deploy a simple Node Express API service running on AWS Lambda using the traditional Serverless Framework.
16
+
This template demonstrates how to develop and deploy a simple Node Express API service running on AWS Lambda using the Serverless Framework.
17
17
18
-
## Anatomy of the template
19
-
20
-
This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, `express` framework is responsible for routing and handling requests internally. Implementation takes advantage of `serverless-http` package, which allows you to wrap existing `express` applications. To learn more about `serverless-http`, please refer to corresponding [GitHub repository](https://github.com/dougmoscrop/serverless-http).
18
+
This template configures a single function, `api`, which is responsible for handling all incoming requests using the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, the Express.js framework is responsible for routing and handling requests internally. This implementation uses the `serverless-http` package to transform the incoming event request payloads to payloads compatible with Express.js. To learn more about `serverless-http`, please refer to corresponding [GitHub repository](https://github.com/dougmoscrop/serverless-http).
21
19
22
20
## Usage
23
21
@@ -38,34 +36,13 @@ serverless deploy
38
36
After running deploy, you should see output similar to:
39
37
40
38
```bash
41
-
Serverless: Packaging service...
42
-
Serverless: Excluding development dependencies...
43
-
Serverless: Creating Stack...
44
-
Serverless: Checking Stack create progress...
45
-
........
46
-
Serverless: Stack create finished...
47
-
Serverless: Uploading CloudFormation file to S3...
48
-
Serverless: Uploading artifacts...
49
-
Serverless: Uploading service aws-node-express-api.zip file to S3 (711.23 KB)...
50
-
Serverless: Validating template...
51
-
Serverless: Updating Stack...
52
-
Serverless: Checking Stack update progress...
53
-
.................................
54
-
Serverless: Stack update finished...
55
-
Service Information
56
-
service: aws-node-express-api
57
-
stage: dev
58
-
region: us-east-1
59
-
stack: aws-node-express-api-dev
60
-
resources: 12
61
-
api keys:
62
-
None
63
-
endpoints:
64
-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
39
+
Deploying "aws-node-express-api" to stage "dev" (us-east-1)
40
+
41
+
✔ Service deployed to stack aws-node-express-api-dev (96s)
42
+
43
+
endpoint: ANY - https://al7bkh7f57.execute-api.us-east-1.amazonaws.com
65
44
functions:
66
-
api: aws-node-express-api-dev-api
67
-
layers:
68
-
None
45
+
api: aws-node-express-api-dev-api (2.3 kB)
69
46
```
70
47
71
48
_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 [`httpApi` event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/).
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:
78
+
The easiest way to develop and test your function is to use the `dev` command:
114
79
115
-
```bash
116
-
serverless plugin install -n serverless-offline
80
+
```
81
+
serverless dev
117
82
```
118
83
119
-
It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`.
84
+
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.
120
85
121
-
After installation, you can start local emulation with:
86
+
In another terminal you can invoke your function by using the following command:
title: 'Serverless Framework Node Express API service backed by DynamoDB on AWS'
3
-
description: 'This template demonstrates how to develop and deploy a simple Node Express API service backed by DynamoDB running on AWS Lambda using the traditional Serverless Framework.'
3
+
description: 'This template demonstrates how to develop and deploy a simple Node Express API service backed by DynamoDB running on AWS Lambda using the Serverless Framework.'
This template demonstrates how to develop and deploy a simple Node Express API service, backed by DynamoDB database, running on AWS Lambda using the traditional Serverless Framework.
16
+
This template demonstrates how to develop and deploy a simple Node Express API service, backed by DynamoDB table, running on AWS Lambda using the Serverless Framework.
17
17
18
+
This template configures a single function, `api`, which is responsible for handling all incoming requests using the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, the Express.js framework is responsible for routing and handling requests internally. This implementation uses the `serverless-http` package to transform the incoming event request payloads to payloads compatible with Express.js. To learn more about `serverless-http`, please refer to corresponding [GitHub repository](https://github.com/dougmoscrop/serverless-http).
18
19
19
-
## Anatomy of the template
20
-
21
-
This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, `express` framework is responsible for routing and handling requests internally. Implementation takes advantage of `serverless-http` package, which allows you to wrap existing `express` applications. To learn more about `serverless-http`, please refer to corresponding [GitHub repository](https://github.com/dougmoscrop/serverless-http). Additionally, it also handles provisioning of a DynamoDB database that is used for storing data about users. The `express` application exposes two endpoints, `POST /users` and `GET /user/{userId}`, which allow to create and retrieve users.
20
+
Additionally, it also handles provisioning of a DynamoDB database that is used for storing data about users. The Express.js application exposes two endpoints, `POST /users` and `GET /user/:userId`, which create and retrieve a user record.
22
21
23
22
## Usage
24
23
@@ -38,35 +37,14 @@ serverless deploy
38
37
39
38
After running deploy, you should see output similar to:
40
39
41
-
```bash
42
-
Serverless: Packaging service...
43
-
Serverless: Excluding development dependencies...
44
-
Serverless: Creating Stack...
45
-
Serverless: Checking Stack create progress...
46
-
........
47
-
Serverless: Stack create finished...
48
-
Serverless: Uploading CloudFormation file to S3...
49
-
Serverless: Uploading artifacts...
50
-
Serverless: Uploading service aws-node-express-dynamodb-api.zip file to S3 (718.53 KB)...
51
-
Serverless: Validating template...
52
-
Serverless: Updating Stack...
53
-
Serverless: Checking Stack update progress...
54
-
....................................
55
-
Serverless: Stack update finished...
56
-
Service Information
57
-
service: aws-node-express-dynamodb-api
58
-
stage: dev
59
-
region: us-east-1
60
-
stack: aws-node-express-dynamodb-api-dev
61
-
resources: 13
62
-
api keys:
63
-
None
64
-
endpoints:
65
-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
40
+
```
41
+
Deploying "aws-node-express-dynamodb-api" to stage "dev" (us-east-1)
42
+
43
+
✔ Service deployed to stack aws-node-express-dynamodb-api-dev (109s)
44
+
45
+
endpoint: ANY - https://jhssgx6g93.execute-api.us-east-1.amazonaws.com
_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 [`httpApi` event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). Additionally, in current configuration, the DynamoDB table will be removed when running `serverless remove`. To retain the DynamoDB table even after removal of the stack, add `DeletionPolicy: Retain` to its resource definition.
@@ -75,86 +53,46 @@ _Note_: In current form, after deployment, your API is public and can be invoked
75
53
76
54
After successful deployment, you can create a new user by calling the corresponding endpoint:
If you try to retrieve user that does not exist, you should receive the following response:
101
-
102
-
```bash
103
-
{"error":"Could not find user with provided \"userId\""}
74
+
```json
75
+
{ "userId": "someUserId", "name": "John" }
104
76
```
105
77
106
78
### Local development
107
79
108
-
It is also possible to emulate DynamoDB, API Gateway and Lambda locally using the `serverless-dynamodb-local` and `serverless-offline` plugins. In order to do that, run:
80
+
The easiest way to develop and test your function is to use the `dev` command:
It will add both plugins to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`. Make sure that `serverless-offline` is listed as last plugin in `plugins` section:
116
-
117
-
```
118
-
plugins:
119
-
- serverless-dynamodb-local
120
-
- serverless-offline
83
+
serverless dev
121
84
```
122
85
123
-
You should also add the following config to `custom` section in `serverless.yml`:
124
-
125
-
```
126
-
custom:
127
-
(...)
128
-
dynamodb:
129
-
start:
130
-
migrate: true
131
-
stages:
132
-
- dev
133
-
```
86
+
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.
134
87
135
-
Additionally, we need to reconfigure `AWS.DynamoDB.DocumentClient` to connect to our local instance of DynamoDB. We can take advantage of `IS_OFFLINE` environment variable set by `serverless-offline` plugin and replace:
88
+
In another terminal you can invoke your function by using the following command:
0 commit comments