Walkthrough application for using the Event Gateway. -- https://www.serverless.com
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Event Gateway REST API

This example shows a simple way to deploy a REST API via the Event Gateway and AWS Lambda. It uses the hosted version provided by Serverless, Inc. -- Sign up here!

This service will deploy three functions:

  • createUser: an HTTP endpoint for creating and storing a User in a DynamoDB table;
  • getUser: an HTTP endpoint for retrieving a User from DynamoDB; and
  • emailUser: a function that is triggered by a user.created event and pretends to email the user with a welcome message.

The createUser function allows you to create a new User by sending a JSON payload to /users, and the getUser function lets you retrieve that User by making a GET request to /users/{id}.

In the createUser function, we're using the Event Gateway SDK to emit a custom event of user.created into the Event Gateway. You can then subscribe functions to react to this custom event. The emailUser function is subscribed to this event as an example -- imagine a Marketing department that wants to handle emails.

Let's get started!


Follow this guide to get the Serverless Framework & Event Gateway set up.

Clone this repository, cd into it and run npm i

Make sure you have created an Application in the Serverless Dashboard and filled in your tenant and app in your serverless.yml file.

# serverless.yml

tenant: mytenant # Insert your tenant
app: demos # Insert your app
service: v1-eg-rest-api # Come up with a service name

Deploy your service

$ serverless deploy

Create a new user by hitting the createUser endpoint:

$ APP="<appURL>"
$ curl -X POST -H "Content-Type: application/json" https://${APP}/users \
    --data '{
    	"id": "10",
    	"firstName": "Donald",
    	"lastName": "Duck",
    	"email": "donald.duck@disney.com"

# {"id":10,"firstName":"Donald","lastName":"Duck","email":"donald.duck@disney.com"}

You can now retrieve your user by using the getUser endpoint:

$ APP="<appURL>"
$ curl -X GET https://${APP}/users/10

# {"id":"10","email":"donald.duck@disney.com","firstName":"Donald","lastName":"Duck"}

In your createUser code, it emits a user.created event to the Event Gateway, which triggers the emailUser function, which then emits a email.sent event. You can check the logs for the Event Gateway in the Dashboard, just navigate to your Service and click the "logs" tab.

Additional Resources: