Skip to content

Commit 5608135

Browse files
committed
updating onboarding AWS node examples
1 parent e4f2613 commit 5608135

22 files changed

+174
-448
lines changed

aws-node-express-api/README.md

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
<!--
22
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.'
44
layout: Doc
5-
framework: v2
5+
framework: v4
66
platform: AWS
77
language: nodeJS
88
priority: 1
99
authorLink: 'https://github.com/serverless'
10-
authorName: 'Serverless, inc.'
10+
authorName: 'Serverless, Inc.'
1111
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
1212
-->
1313

1414
# Serverless Framework Node Express API on AWS
1515

16-
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.
1717

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).
2119

2220
## Usage
2321

@@ -38,34 +36,13 @@ serverless deploy
3836
After running deploy, you should see output similar to:
3937

4038
```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
6544
functions:
66-
api: aws-node-express-api-dev-api
67-
layers:
68-
None
45+
api: aws-node-express-api-dev-api (2.3 kB)
6946
```
7047

7148
_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/).
@@ -80,48 +57,40 @@ curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
8057

8158
Which should result in the following response:
8259

83-
```
84-
{"message":"Hello from root!"}
60+
```json
61+
{ "message": "Hello from root!" }
8562
```
8663

8764
Calling the `/hello` path with:
8865

89-
```bash
66+
```
9067
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/hello
9168
```
9269

9370
Should result in the following response:
9471

95-
```bash
96-
{"message":"Hello from path!"}
97-
```
98-
99-
If you try to invoke a path or method that does not have a configured handler, e.g. with:
100-
101-
```bash
102-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/nonexistent
103-
```
104-
105-
You should receive the following response:
106-
107-
```bash
108-
{"error":"Not Found"}
72+
```json
73+
{ "message": "Hello from path!" }
10974
```
11075

11176
### Local development
11277

113-
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:
11479

115-
```bash
116-
serverless plugin install -n serverless-offline
80+
```
81+
serverless dev
11782
```
11883

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.
12085

121-
After installation, you can start local emulation with:
86+
In another terminal you can invoke your function by using the following command:
12287

12388
```
124-
serverless offline
89+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/hello
12590
```
12691

127-
To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline).
92+
Which should result in response similar to the following:
93+
94+
```json
95+
{ "message": "Hello from path!" }
96+
```

aws-node-express-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"dependencies": {
6-
"express": "^4.18.2",
6+
"express": "^4.19.2",
77
"serverless-http": "^3.2.0"
88
}
99
}

aws-node-express-api/serverless.template.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

aws-node-express-api/serverless.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
service: aws-node-express-api
2-
frameworkVersion: '4'
2+
3+
frameworkVersion: "4"
34

45
provider:
56
name: aws
67
runtime: nodejs20.x
8+
79
functions:
810
api:
911
handler: handler.handler
1012
events:
11-
- httpApi: '*'
13+
- httpApi: "*"
Lines changed: 26 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
<!--
22
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.'
44
layout: Doc
55
framework: v2
66
platform: AWS
77
language: nodeJS
88
priority: 1
99
authorLink: 'https://github.com/serverless'
10-
authorName: 'Serverless, inc.'
10+
authorName: 'Serverless, Inc.'
1111
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
1212
-->
1313

1414
# Serverless Framework Node Express API on AWS
1515

16-
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.
1717

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).
1819

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.
2221

2322
## Usage
2423

@@ -38,35 +37,14 @@ serverless deploy
3837

3938
After running deploy, you should see output similar to:
4039

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
6646
functions:
67-
api: aws-node-express-dynamodb-api-dev-api
68-
layers:
69-
None
47+
api: aws-node-express-dynamodb-api-dev-api (3.8 MB)
7048
```
7149

7250
_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
7553

7654
After successful deployment, you can create a new user by calling the corresponding endpoint:
7755

78-
```bash
56+
```
7957
curl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/users' --header 'Content-Type: application/json' --data-raw '{"name": "John", "userId": "someUserId"}'
8058
```
8159

8260
Which should result in the following response:
8361

84-
```bash
85-
{"userId":"someUserId","name":"John"}
62+
```json
63+
{ "userId": "someUserId", "name": "John" }
8664
```
8765

8866
You can later retrieve the user by `userId` by calling the following endpoint:
8967

90-
```bash
68+
```
9169
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
9270
```
9371

9472
Which should result in the following response:
9573

96-
```bash
97-
{"userId":"someUserId","name":"John"}
98-
```
99-
100-
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" }
10476
```
10577

10678
### Local development
10779

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:
10981

110-
```bash
111-
serverless plugin install -n serverless-dynamodb-local
112-
serverless plugin install -n serverless-offline
11382
```
114-
115-
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
12184
```
12285

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.
13487

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:
13689

137-
```javascript
138-
const dynamoDbClient = new AWS.DynamoDB.DocumentClient();
13990
```
140-
141-
with the following:
142-
143-
```javascript
144-
const dynamoDbClientParams = {};
145-
if (process.env.IS_OFFLINE) {
146-
dynamoDbClientParams.region = 'localhost'
147-
dynamoDbClientParams.endpoint = 'http://localhost:8000'
148-
}
149-
const dynamoDbClient = new AWS.DynamoDB.DocumentClient(dynamoDbClientParams);
91+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
15092
```
15193

152-
After that, running the following command with start both local API Gateway emulator as well as local instance of emulated DynamoDB:
94+
Which should result in response similar to the following:
15395

154-
```bash
155-
serverless offline start
96+
```json
97+
{ "userId": "someUserId", "name": "John" }
15698
```
157-
158-
To learn more about the capabilities of `serverless-offline` and `serverless-dynamodb-local`, please refer to their corresponding GitHub repositories:
159-
- https://github.com/dherault/serverless-offline
160-
- https://github.com/99x/serverless-dynamodb-local

0 commit comments

Comments
 (0)