Skip to content

Commit

Permalink
Switch to HTTP APIs by default for Node examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored and pgrzesik committed Sep 14, 2021
1 parent ef8c8cb commit 8786b2f
Show file tree
Hide file tree
Showing 56 changed files with 1,861 additions and 62 deletions.
25 changes: 8 additions & 17 deletions aws-node-express-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,19 @@ This template demonstrates how to develop and deploy a simple Node Express API s

## Anatomy of the template

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

## Usage

### Deployment

This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.

In order to deploy with dashboard, you need to first login with:

```
serverless login
```

install dependencies with:
Install dependencies with:

```
npm install
```

and then perform deployment with:
and then deploy with:

```
serverless deploy
Expand Down Expand Up @@ -69,22 +61,21 @@ resources: 12
api keys:
None
endpoints:
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
api: aws-node-express-api-dev-api
layers:
None
```

_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/).
_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/).

### Invocation

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

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
```

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

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/hello
```

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

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/nonexistent
```

You should receive the following response:
Expand Down
2 changes: 1 addition & 1 deletion aws-node-express-api/serverless.template.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: aws-node-express-api
org: serverlessinc
description: Deploys a Node Express API service with traditional Serverless Framework
description: Deploys a Node Express API service with Serverless Framework
keywords: aws, serverless, faas, lambda, node, express
repo: https://github.com/serverless/examples/aws-node-express-api
license: MIT
9 changes: 1 addition & 8 deletions aws-node-express-api/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
service: aws-node-express-api

frameworkVersion: '2'


provider:
name: aws
runtime: nodejs12.x
Expand All @@ -12,9 +10,4 @@ functions:
api:
handler: handler.handler
events:
- http:
path: /
method: ANY
- http:
path: /{proxy+}
method: ANY
- httpApi: '*'
25 changes: 8 additions & 17 deletions aws-node-express-dynamodb-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,19 @@ This template demonstrates how to develop and deploy a simple Node Express API s

## Anatomy of the template

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

## Usage

### Deployment

This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.

In order to deploy with dashboard, you need to first login with:

```
serverless login
```

install dependencies with:
Install dependencies with:

```
npm install
```

and then perform deployment with:
and then deploy with:

```
serverless deploy
Expand Down Expand Up @@ -70,22 +62,21 @@ resources: 13
api keys:
None
endpoints:
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
api: aws-node-express-dynamodb-api-dev-api
layers:
None
```

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

### Invocation

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

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

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

```bash
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users/someUserId
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
```

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

### Local development

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

```bash
serverless plugin install -n serverless-dynamodb-local
Expand Down
2 changes: 1 addition & 1 deletion aws-node-express-dynamodb-api/serverless.template.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: aws-node-express-dynamodb-api
org: serverlessinc
description: Deploys a Node Express API service backed by DynamoDB with traditional Serverless Framework
description: Deploys a Node Express API service backed by DynamoDB with Serverless Framework
keywords: aws, serverless, faas, lambda, node, express, dynamodb
repo: https://github.com/serverless/examples/aws-node-express-dynamodb-api
license: MIT
15 changes: 3 additions & 12 deletions aws-node-express-dynamodb-api/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
service: aws-node-express-dynamodb-api

frameworkVersion: '2'

custom:
tableName: 'users-table-${self:provider.stage}'
tableName: 'users-table-${sls:stage}'

provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: '20201221'
stage: dev
iam:
role:
statements:
Expand All @@ -30,12 +28,7 @@ functions:
api:
handler: handler.handler
events:
- http:
path: /
method: ANY
- http:
path: /{proxy+}
method: ANY
- httpApi: '*'

resources:
Resources:
Expand All @@ -48,7 +41,5 @@ resources:
KeySchema:
- AttributeName: userId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
BillingMode: PAY_PER_REQUEST
TableName: ${self:custom.tableName}
2 changes: 2 additions & 0 deletions aws-node-http-api-dynamodb-local/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.serverless
node_modules
98 changes: 98 additions & 0 deletions aws-node-http-api-dynamodb-local/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!--
title: 'AWS Serverless HTTP API with DynamoDB and offline support example in NodeJS'
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.'
layout: Doc
framework: v1
platform: AWS
language: nodeJS
authorLink: 'https://github.com/adambrgmn'
authorName: 'Adam Bergman'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13746650?v=4&s=140'
-->
# Serverless HTTP API with DynamoDB and offline support

This example demonstrates how to run a service locally, using the
[serverless-offline](https://github.com/dherault/serverless-offline) plugin. It
provides an HTTP API to manage Todos stored in a DynamoDB, similar to the
[aws-node-http-api-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-http-api-dynamodb)
example. A local DynamoDB instance is provided by the
[serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local)
plugin.

## Use-case

Test your service locally, without having to deploy it first.

## Setup

```bash
npm install
serverless dynamodb install (or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d)
serverless offline start
serverless dynamodb migrate (this imports schema)
```

## Run service offline

```bash
serverless offline start
```

## Usage

You can create, retrieve, update, or delete todos with the following commands:

### Create a Todo

```bash
curl -X POST -H "Content-Type:application/json" http://localhost:3000/todos --data '{ "text": "Learn Serverless" }'
```

Example Result:
```bash
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
```

### List all Todos

```bash
curl -H "Content-Type:application/json" http://localhost:3000/todos
```

Example output:
```bash
[{"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}]%
```

### Get one Todo

```bash
# Replace the <id> part with a real id from your todos table
curl -H "Content-Type:application/json" http://localhost:3000/todos/<id>
```

Example Result:
```bash
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
```

### Update a Todo

```bash
# Replace the <id> part with a real id from your todos table
curl -X PUT -H "Content-Type:application/json" http://localhost:3000/todos/<id> --data '{ "text": "Learn Serverless", "checked": true }'
```

Example Result:
```bash
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":true,"updatedAt":1479138570824}%
```

### Delete a Todo

```bash
# Replace the <id> part with a real id from your todos table
curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/todos/<id>
```

No output
8 changes: 8 additions & 0 deletions aws-node-http-api-dynamodb-local/dynamodb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM amazon/dynamodb-local

WORKDIR /home/dynamodblocal

RUN mkdir ./db && chown -R 1000 ./db

CMD ["-jar", "DynamoDBLocal.jar", "-dbPath", "./db", "-sharedDb"]
VOLUME ["./db"]
15 changes: 15 additions & 0 deletions aws-node-http-api-dynamodb-local/dynamodb/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3"

services:
dynamodb:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- aws-http-api-dynamodb:/home/dynamodblocal/db

volumes:
aws-http-api-dynamodb:
driver: local
21 changes: 21 additions & 0 deletions aws-node-http-api-dynamodb-local/offline/migrations/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Table": {
"TableName": "serverless-http-api-dynamodb-local-dev",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
}
16 changes: 16 additions & 0 deletions aws-node-http-api-dynamodb-local/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "serverless-http-api-dynamodb-local",
"version": "1.0.0",
"description": "Serverless HTTP API with DynamoDB and offline support",
"repository": "",
"author": "Christoph Gysin <[email protected]>",
"license": "MIT",
"dependencies": {
"uuid": "^2.0.3"
},
"devDependencies": {
"aws-sdk": "^2.12.0",
"serverless-dynamodb-local": "^0.2.18",
"serverless-offline": "^6.8.0"
}
}
Loading

0 comments on commit 8786b2f

Please sign in to comment.