Building an Edge API Gateway with Fauna and Securing It with Auth0
Tags: security, programming, database
Published: 2021-12-22 19:56:45

#### In this tutorial, we’ll explore architecting REST APIs in a fully serverless manner by leveraging Fastly’s Compute@Edge, Fauna, and Auth0.

### <br>

When building an application, ensuring a great user experience is a key to user engagement, ultimately impacting how happy your users are with your product. A critical aspect of how a user experiences your application is latency. This is especially true when end-users are globally distributed, as latency is dependent on the distance between the end-user and where the application is being served.

Yesterday’s applications didn’t need to provide millisecond response times for users anywhere in the world, so most compute and database options found in production today cannot handle the use case. But with edge computing, there may finally be an easy way to move applications closer to the end-user, if only databases were quick to adapt to this new architecture. If you’re building an API today, you cannot simply “deploy” any database at the edge like the picture shown below, as the edge cannot fully replace the function of a true server.

![Databases Are Meant To Be Connected To Servers.drawio](https://images.ctfassets.net/23aumh6u8s0i/67auSB1QgE4vXfTty0LrkL/754afaac91ac10e814bad62a34ed7127/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

So what you’re left with is placing the API (still requiring the app server, database, and all) behind the edge, in the configuration shown below:

![better](https://images.ctfassets.net/23aumh6u8s0i/2FdjMAEVIlFKASbIqLqHf8/b7862361c1e09bbfa0ca9c2ad9618e7b/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

This is still an improvement, as it adds resiliency and reduces latency by caching results where possible. But if speed is the goal, can you do better? Notice the database still has to sit somewhere relative to the edge locations everywhere. What if the database is far from the user? Engineers have solved this by implementing globally [sharded](https://www.digitalocean.com/community/tutorials/understanding-database-sharding) databases. But that’s complicated and expensive.

If you have the luxury of starting fresh and want to realize the full promise of scale, resiliency, and performance that edge computing provides, you need a globally distributed, serverless database that edge functions simply access using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch). This is where [Fauna](https://fauna.com/) comes into the picture.

![Edge to Cloud API.drawio](https://images.ctfassets.net/23aumh6u8s0i/kq1p1t1Cp2wAzs3o6aVdm/912fefb267f3a76db421694502a1d2dc/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

## What We’re Building

> See the full sample of this [tutorial’s content](https://github.com/fauna-labs/edge-gateway-sample-fastly).

In this tutorial, we’ll explore architecting REST APIs in a fully serverless manner by leveraging [](https://www.fastly.com/products/edge-compute/serverless)edge computing and Fauna, and we’re going to secure the API requests with Auth0. In the interest of emphasizing speed, we’ll use Fastly’s Compute@Edge, which provides ultralow startup times and a low memory footprint via its WASI runtime. The examples below will walk you through building a user registration flow for a website. You’ll be implementing the /users resource and its GET, POST, and PUT methods.

![C@E demo registration.drawio](https://images.ctfassets.net/23aumh6u8s0i/pwn6amsnHoKAieO3LzHVH/2980da0f6f919c8b3ba5061395335843/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

We’ll also demonstrate two powerful features of Fauna:

1.  With [Region Groups](https://docs.fauna.com/fauna/current/api/fql/region_groups) you can create your databases in geographic regions of your choice. You can then easily build GDPR compliant architectures by routing requests to specific region groups — with the help of the edge — depending on where the user is making the request from. The performance also benefits by having the request from the client to edge to the database being as close to each other as possible.
2.  Fauna supports [external authentication](https://docs.fauna.com/fauna/current/security/external/) with any Identity Provider (IdP) that supports the OpenID Connect protocol, allowing you to leverage a service such as [Auth0](https://auth0.com/) so that you can focus on developing features instead of worrying about security.

![Fastly Fauna Auth0.drawio](https://images.ctfassets.net/23aumh6u8s0i/30QRsNGnCJtPorHVzoMaUB/65e172cb8cebe11ecd9dc0ab49135f7f/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

## What’s Needed to Complete This Tutorial?

*   An [Auth0](https://auth0.com/sign-up) account
*   A [Fauna](https://dashboard.fauna.com/accounts/register) account
*   A [Fastly](https://www.fastly.com/signup/) account
*   An API Client such as [Postman](https://www.postman.com/) or curl
*   [Node.js](https://nodejs.org/en/) installed on your system

...and nothing else! No infrastructure is required because you won’t be standing up or deploying any servers.

## Setting up Fauna

Your first step will be to configure a database through Fauna's dashboard. [Login to the dashboard](https://dashboard.fauna.com/), and create a new database named _client-serverless-api_:

![fastly_create_database_eu450](https://images.ctfassets.net/23aumh6u8s0i/58Z4Tv7J4SYOaPnfqKdH0m/ef9c60ff3a6cb87b2266c9e384086b41/Uploaded_from_Building_an_edge_API_gateway_with_Fauna_and_securing_it_with_Auth0)

*   For Region Group, choose Europe (EU) (for the sake of demonstrating Fauna’s multi Region Group capability).

[Read more...](https://auth0.com/blog/building-an-edge-api-gateway-with-fauna-and-securing-it-with-auth0/?utm_source=content_synd&utm_medium=sc&utm_campaign=fauna)