Skip to content

Commit 3343a4c

Browse files
committed
Switch to HTTP APIs by default for Node examples
1 parent 7f4ad28 commit 3343a4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1861
-62
lines changed

aws-node-express-api/README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,19 @@ This template demonstrates how to develop and deploy a simple Node Express API s
1616

1717
## Anatomy of the template
1818

19-
This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to configured `http` events. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). As the events are 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).
19+
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).
2020

2121
## Usage
2222

2323
### Deployment
2424

25-
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
26-
27-
In order to deploy with dashboard, you need to first login with:
28-
29-
```
30-
serverless login
31-
```
32-
33-
install dependencies with:
25+
Install dependencies with:
3426

3527
```
3628
npm install
3729
```
3830

39-
and then perform deployment with:
31+
and then deploy with:
4032

4133
```
4234
serverless deploy
@@ -68,22 +60,21 @@ resources: 12
6860
api keys:
6961
None
7062
endpoints:
71-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
72-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
63+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
7364
functions:
7465
api: aws-node-express-api-dev-api
7566
layers:
7667
None
7768
```
7869

79-
_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/).
70+
_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/).
8071

8172
### Invocation
8273

8374
After successful deployment, you can call the created application via HTTP:
8475

8576
```bash
86-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
77+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
8778
```
8879

8980
Which should result in the following response:
@@ -95,7 +86,7 @@ Which should result in the following response:
9586
Calling the `/hello` path with:
9687

9788
```bash
98-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
89+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/hello
9990
```
10091

10192
Should result in the following response:
@@ -107,7 +98,7 @@ Should result in the following response:
10798
If you try to invoke a path or method that does not have a configured handler, e.g. with:
10899

109100
```bash
110-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent
101+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/nonexistent
111102
```
112103

113104
You should receive the following response:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: aws-node-express-api
22
org: serverlessinc
3-
description: Deploys a Node Express API service with traditional Serverless Framework
3+
description: Deploys a Node Express API service with Serverless Framework
44
keywords: aws, serverless, faas, lambda, node, express
55
repo: https://github.com/serverless/examples/aws-node-express-api
66
license: MIT
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
service: aws-node-express-api
2-
32
frameworkVersion: '2'
43

5-
64
provider:
75
name: aws
86
runtime: nodejs12.x
@@ -12,9 +10,4 @@ functions:
1210
api:
1311
handler: handler.handler
1412
events:
15-
- http:
16-
path: /
17-
method: ANY
18-
- http:
19-
path: /{proxy+}
20-
method: ANY
13+
- httpApi: '*'

aws-node-express-dynamodb-api/README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,19 @@ This template demonstrates how to develop and deploy a simple Node Express API s
1717

1818
## Anatomy of the template
1919

20-
This template configures a single function, `api`, in `serverless.yml` which is responsible for handling all incoming requests thanks to configured `http` events. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). As the events are 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+
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.
2121

2222
## Usage
2323

2424
### Deployment
2525

26-
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
27-
28-
In order to deploy with dashboard, you need to first login with:
29-
30-
```
31-
serverless login
32-
```
33-
34-
install dependencies with:
26+
Install dependencies with:
3527

3628
```
3729
npm install
3830
```
3931

40-
and then perform deployment with:
32+
and then deploy with:
4133

4234
```
4335
serverless deploy
@@ -69,22 +61,21 @@ resources: 13
6961
api keys:
7062
None
7163
endpoints:
72-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
73-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
64+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
7465
functions:
7566
api: aws-node-express-dynamodb-api-dev-api
7667
layers:
7768
None
7869
```
7970

80-
_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/). Additionally, in current configuration, DynamoDB Table will be removed when running `serverless remove`. To retain DynamoDB Table even after removal of the stack, add `DeletionPolicy: Retain` to its resource definition.
71+
_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.
8172

8273
### Invocation
8374

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

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

9081
Which should result in the following response:
@@ -96,7 +87,7 @@ Which should result in the following response:
9687
You can later retrieve the user by `userId` by calling the following endpoint:
9788

9889
```bash
99-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users/someUserId
90+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
10091
```
10192

10293
Which should result in the following response:
@@ -113,7 +104,7 @@ If you try to retrieve user that does not exist, you should receive the followin
113104

114105
### Local development
115106

116-
It is also possible to emulate DynamodB, API Gateway and Lambda locally by using `serverless-dynamodb-local` and `serverless-offline` plugins. In order to do that, execute the following commands:
107+
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:
117108

118109
```bash
119110
serverless plugin install -n serverless-dynamodb-local
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: aws-node-express-dynamodb-api
22
org: serverlessinc
3-
description: Deploys a Node Express API service backed by DynamoDB with traditional Serverless Framework
3+
description: Deploys a Node Express API service backed by DynamoDB with Serverless Framework
44
keywords: aws, serverless, faas, lambda, node, express, dynamodb
55
repo: https://github.com/serverless/examples/aws-node-express-dynamodb-api
66
license: MIT

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
service: aws-node-express-dynamodb-api
2-
32
frameworkVersion: '2'
43

54
custom:
6-
tableName: 'users-table-${self:provider.stage}'
5+
tableName: 'users-table-${sls:stage}'
76

87
provider:
98
name: aws
109
runtime: nodejs12.x
1110
lambdaHashingVersion: '20201221'
12-
stage: dev
1311
iam:
1412
role:
1513
statements:
@@ -30,12 +28,7 @@ functions:
3028
api:
3129
handler: handler.handler
3230
events:
33-
- http:
34-
path: /
35-
method: ANY
36-
- http:
37-
path: /{proxy+}
38-
method: ANY
31+
- httpApi: '*'
3932

4033
resources:
4134
Resources:
@@ -48,7 +41,5 @@ resources:
4841
KeySchema:
4942
- AttributeName: userId
5043
KeyType: HASH
51-
ProvisionedThroughput:
52-
ReadCapacityUnits: 1
53-
WriteCapacityUnits: 1
44+
BillingMode: PAY_PER_REQUEST
5445
TableName: ${self:custom.tableName}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.serverless
2+
node_modules
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!--
2+
title: 'AWS Serverless HTTP API with DynamoDB and offline support example in NodeJS'
3+
description: 'This example demonstrates how to run a service locally, using the ''serverless-offline'' plugin. It provides an HTTP API to manage Todos stored in DynamoDB.'
4+
layout: Doc
5+
framework: v1
6+
platform: AWS
7+
language: nodeJS
8+
authorLink: 'https://github.com/adambrgmn'
9+
authorName: 'Adam Bergman'
10+
authorAvatar: 'https://avatars1.githubusercontent.com/u/13746650?v=4&s=140'
11+
-->
12+
# Serverless HTTP API with DynamoDB and offline support
13+
14+
This example demonstrates how to run a service locally, using the
15+
[serverless-offline](https://github.com/dherault/serverless-offline) plugin. It
16+
provides an HTTP API to manage Todos stored in a DynamoDB, similar to the
17+
[aws-node-http-api-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-http-api-dynamodb)
18+
example. A local DynamoDB instance is provided by the
19+
[serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local)
20+
plugin.
21+
22+
## Use-case
23+
24+
Test your service locally, without having to deploy it first.
25+
26+
## Setup
27+
28+
```bash
29+
npm install
30+
serverless dynamodb install (or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d)
31+
serverless offline start
32+
serverless dynamodb migrate (this imports schema)
33+
```
34+
35+
## Run service offline
36+
37+
```bash
38+
serverless offline start
39+
```
40+
41+
## Usage
42+
43+
You can create, retrieve, update, or delete todos with the following commands:
44+
45+
### Create a Todo
46+
47+
```bash
48+
curl -X POST -H "Content-Type:application/json" http://localhost:3000/todos --data '{ "text": "Learn Serverless" }'
49+
```
50+
51+
Example Result:
52+
```bash
53+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
54+
```
55+
56+
### List all Todos
57+
58+
```bash
59+
curl -H "Content-Type:application/json" http://localhost:3000/todos
60+
```
61+
62+
Example output:
63+
```bash
64+
[{"text":"Deploy my first service","id":"ac90feaa11e6-9ede-afdfa051af86","checked":true,"updatedAt":1479139961304},{"text":"Learn Serverless","id":"206793aa11e6-9ede-afdfa051af86","createdAt":1479139943241,"checked":false,"updatedAt":1479139943241}]%
65+
```
66+
67+
### Get one Todo
68+
69+
```bash
70+
# Replace the <id> part with a real id from your todos table
71+
curl -H "Content-Type:application/json" http://localhost:3000/todos/<id>
72+
```
73+
74+
Example Result:
75+
```bash
76+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
77+
```
78+
79+
### Update a Todo
80+
81+
```bash
82+
# Replace the <id> part with a real id from your todos table
83+
curl -X PUT -H "Content-Type:application/json" http://localhost:3000/todos/<id> --data '{ "text": "Learn Serverless", "checked": true }'
84+
```
85+
86+
Example Result:
87+
```bash
88+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":true,"updatedAt":1479138570824}%
89+
```
90+
91+
### Delete a Todo
92+
93+
```bash
94+
# Replace the <id> part with a real id from your todos table
95+
curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/todos/<id>
96+
```
97+
98+
No output
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM amazon/dynamodb-local
2+
3+
WORKDIR /home/dynamodblocal
4+
5+
RUN mkdir ./db && chown -R 1000 ./db
6+
7+
CMD ["-jar", "DynamoDBLocal.jar", "-dbPath", "./db", "-sharedDb"]
8+
VOLUME ["./db"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "3"
2+
3+
services:
4+
dynamodb:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
ports:
9+
- 8000:8000
10+
volumes:
11+
- aws-http-api-dynamodb:/home/dynamodblocal/db
12+
13+
volumes:
14+
aws-http-api-dynamodb:
15+
driver: local

0 commit comments

Comments
 (0)