Implementing Fauna as infrastructure as code with Serverless Framework
Tags: serverless, fauna, tutorial, aws
Published: 2022-10-27 17:26:24

This article demonstrates how to use Fauna as infrastructure as code (IaC) in your application using the Serverless Framework, one of the most popular tools for managing infrastructure as code. Fauna has a dedicated [plugin](https://www.npmjs.com/package/@fauna-labs/serverless-fauna#installation) for the Serverless Framework that gives you complete control to manage your Fauna resources. You can integrate it into your test and CI/CD pipelines to keep your databases in sync across multiple environments.

## Benefits of IaC

Before we dive deep into implementing Fauna as IaC, let's discuss why you might want to integrate IaC.

There are three main benefits of IaC:

- **Decreased risks:** Provisioning all of your infrastructures manually can be risky, especially if you have multiple dependencies among services. Complex deployment is prone to human errors. When you automate the process with IaC, you reduce these. Your infrastructure also becomes testable, and you can spin up multiple environments (exact replicas of production).
- **Efficient software development lifecycle:**  With IaC, infrastructure provisioning becomes more reliable and consistent. Developers get complete control of the infrastructure through code. Developers can script once and use that code multiple times, saving time and effort while keeping full control.
- **Self-documenting and reduced administration:** IaC is self-documenting. And it reduces administrative overhead, allowing your engineering efforts to be focused on new feature development.

## Getting started with the Fauna Serverless Framework plugin

Install the Serverless Framework plugin with the following command

```bash
$ npm install @fauna-labs/serverless-fauna --save-dev
```

or using yarn

```bash
$ yarn add @fauna-labs/serverless-fauna
```

Open your `serverless.yml` file and add the following code to add Fauna to your project.

```yaml

plugins:
  - "@fauna-labs/serverless-fauna"
fauna:
  client:
    secret: ${env:FAUNA_ROOT_KEY}
    # domain: db.fauna.com
    # port: 433
    # scheme: https
```

By default, the domain is set to `[db.fauna.com](http://db.fauna.com)`. You can create new collections by adding the collection name under the `collections` field as demonstrated in the following code. 

```yaml
fauna:
  client:
    secret: ${env:FAUNA_ROOT_KEY}
    # domain: db.fauna.com
    # port: 433
    # scheme: https
  collections:
    Movies:
      name: Movies
      data:
        some_data_key: some_data_value
```

The collection configuration accepts the same configuration as [CreateCollection](https://docs.fauna.com/fauna/current/api/fql/functions/createcollection?lang=javascript#param_object) query. 

Similarly, you can add functions and indexes. 

```yaml
fauna:
  client:
    secret: ${env:FAUNA_ROOT_KEY}
    # domain: db.fauna.com
    # port: 433
    # scheme: https
  collections:
    Movies:
      name: Movies
      data:
        some_data_key: some_data_value

  functions:
    double:
      name: double
      body: ${file(./double.fql)}

  indexes:
    movies_by_type:
      name: movies_by_type
      source: ${self:fauna.collections.Movies.name}
      terms:
        fields: 
          - data.type
```

You can review the [documentation](https://www.npmjs.com/package/@fauna-labs/serverless-fauna#installation) for the Serverless Framework plugin to learn more about different functionalities and implementations. 

We have also created a hands-on tutorial on [building a REST API with Fauna, Serverless, and AWS Lambda](https://fauna.com/blog/building-a-rest-api-with-aws-lambda-fauna-and-serverless-framework). This tutorial will help you get up and running with a simple project with Fauna and Serverless Framework.

If you are searching for something more comprehensive, we have also created a self-paced workshop that will guide you through building a real-world project with Fauna, Serverless Framework, and AWS services. You can find the workshop [here](https://aws.workshops.fauna.com/). 

<blockquote class="embedly-card" data-card-controls="0"><h4><a href="https://aws.workshops.fauna.com/">Fauna AWS Workshop ~ Building an event-driven app with AWS services and Fauna</a></h4><p>This hands-on guide walks you through building a real-world event-driven serverless application using AWS services (i.e., AWS Lambda, Step Functions, API Gateway) and Fauna. In this workshop, you build a vacation booking application (Similar to Kayak or Redtag deals).</p></blockquote>
<script async src="//cdn.embedly.com/widgets/platform.js" charset="UTF-8"></script>

Got questions? Reach out in our [Discord channel](https://discord.gg/NHwJFdG2B2) or in our [community forum](https://forums.fauna.com/c/help-general).