Skip to content

Commit c59ddb4

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

File tree

53 files changed

+1849
-41
lines changed

Some content is hidden

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

53 files changed

+1849
-41
lines changed

aws-node-express-api/README.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,13 @@ This template configures a single function, `api`, which is responsible for hand
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,8 +60,7 @@ 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:
@@ -83,7 +74,7 @@ _Note_: In current form, after deployment, your API is public and can be invoked
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 & 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/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
Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
service: serverless-http-api-dynamodb-local
2+
frameworkVersion: '2'
3+
4+
plugins:
5+
- serverless-dynamodb-local
6+
- serverless-offline
7+
8+
custom:
9+
dynamodb:
10+
stages:
11+
- dev
12+
start:
13+
port: 8000
14+
inMemory: true
15+
migrate: true
16+
# Comment if you don't have a DynamoDB running locally
17+
noStart: true
18+
migration:
19+
dir: offline/migrations
20+
21+
provider:
22+
name: aws
23+
runtime: nodejs12.x
24+
lambdaHashingVersion: '20201221'
25+
environment:
26+
DYNAMODB_TABLE: ${self:service}-${sls:stage}
27+
httpApi:
28+
cors: true
29+
iam:
30+
role:
31+
statements:
32+
- Effect: Allow
33+
Action:
34+
- dynamodb:Query
35+
- dynamodb:Scan
36+
- dynamodb:GetItem
37+
- dynamodb:PutItem
38+
- dynamodb:UpdateItem
39+
- dynamodb:DeleteItem
40+
Resource: "arn:aws:dynamodb:${aws:region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
41+
42+
functions:
43+
create:
44+
handler: todos/create.create
45+
events:
46+
- httpApi:
47+
path: /todos
48+
method: post
49+
50+
list:
51+
handler: todos/list.list
52+
events:
53+
- httpApi:
54+
path: /todos
55+
method: get
56+
57+
get:
58+
handler: todos/get.get
59+
events:
60+
- httpApi:
61+
path: /todos/{id}
62+
method: get
63+
64+
update:
65+
handler: todos/update.update
66+
events:
67+
- httpApi:
68+
path: /todos/{id}
69+
method: put
70+
71+
delete:
72+
handler: todos/delete.delete
73+
events:
74+
- httpApi:
75+
path: /todos/{id}
76+
method: delete
77+
78+
resources:
79+
Resources:
80+
TodosDynamoDbTable:
81+
Type: 'AWS::DynamoDB::Table'
82+
DeletionPolicy: Retain
83+
Properties:
84+
AttributeDefinitions:
85+
-
86+
AttributeName: id
87+
AttributeType: S
88+
KeySchema:
89+
-
90+
AttributeName: id
91+
KeyType: HASH
92+
BillingMode: PAY_PER_REQUEST
93+
TableName: ${self:provider.environment.DYNAMODB_TABLE}

0 commit comments

Comments
 (0)