Skip to content

Commit 8786b2f

Browse files
mnapolipgrzesik
authored andcommitted
Switch to HTTP APIs by default for Node examples
1 parent ef8c8cb commit 8786b2f

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

+8-17
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`, 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).
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).
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: 12
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-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/).
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/).
8172

8273
### Invocation
8374

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

8677
```bash
87-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
78+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
8879
```
8980

9081
Which should result in the following response:
@@ -96,7 +87,7 @@ Which should result in the following response:
9687
Calling the `/hello` path with:
9788

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

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

110101
```bash
111-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent
102+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/nonexistent
112103
```
113104

114105
You should receive the following response:
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

aws-node-express-api/serverless.yml

+1-8
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

+8-17
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,19 @@ This template demonstrates how to develop and deploy a simple Node Express API s
1818

1919
## Anatomy of the template
2020

21-
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.
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.
2222

2323
## Usage
2424

2525
### Deployment
2626

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

3729
```
3830
npm install
3931
```
4032

41-
and then perform deployment with:
33+
and then deploy with:
4234

4335
```
4436
serverless deploy
@@ -70,22 +62,21 @@ resources: 13
7062
api keys:
7163
None
7264
endpoints:
73-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
74-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
65+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
7566
functions:
7667
api: aws-node-express-dynamodb-api-dev-api
7768
layers:
7869
None
7970
```
8071

81-
_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.
72+
_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.
8273

8374
### Invocation
8475

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

8778
```bash
88-
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"}'
79+
curl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/users' --header 'Content-Type: application/json' --data-raw '{"name": "John", "userId": "someUserId"}'
8980
```
9081

9182
Which should result in the following response:
@@ -97,7 +88,7 @@ Which should result in the following response:
9788
You can later retrieve the user by `userId` by calling the following endpoint:
9889

9990
```bash
100-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users/someUserId
91+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
10192
```
10293

10394
Which should result in the following response:
@@ -114,7 +105,7 @@ If you try to retrieve user that does not exist, you should receive the followin
114105

115106
### Local development
116107

117-
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:
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:
118109

119110
```bash
120111
serverless plugin install -n serverless-dynamodb-local
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

+3-12
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}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.serverless
2+
node_modules
+98
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
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"]
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Table": {
3+
"TableName": "serverless-http-api-dynamodb-local-dev",
4+
"KeySchema": [
5+
{
6+
"AttributeName": "id",
7+
"KeyType": "HASH"
8+
}
9+
],
10+
"AttributeDefinitions": [
11+
{
12+
"AttributeName": "id",
13+
"AttributeType": "S"
14+
}
15+
],
16+
"ProvisionedThroughput": {
17+
"ReadCapacityUnits": 1,
18+
"WriteCapacityUnits": 1
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "serverless-http-api-dynamodb-local",
3+
"version": "1.0.0",
4+
"description": "Serverless HTTP API with DynamoDB and offline support",
5+
"repository": "",
6+
"author": "Christoph Gysin <[email protected]>",
7+
"license": "MIT",
8+
"dependencies": {
9+
"uuid": "^2.0.3"
10+
},
11+
"devDependencies": {
12+
"aws-sdk": "^2.12.0",
13+
"serverless-dynamodb-local": "^0.2.18",
14+
"serverless-offline": "^6.8.0"
15+
}
16+
}

0 commit comments

Comments
 (0)