Implementing Fauna as infrastructure as code with Serverless Framework

fauna_admin

Fauna

Posted on October 27, 2022

Implementing Fauna as infrastructure as code with Serverless Framework

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

$ npm install @fauna-labs/serverless-fauna --save-dev
Enter fullscreen mode Exit fullscreen mode

or using yarn

$ yarn add @fauna-labs/serverless-fauna
Enter fullscreen mode Exit fullscreen mode

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


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

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.

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
Enter fullscreen mode Exit fullscreen mode

The collection configuration accepts the same configuration as CreateCollection query.

Similarly, you can add functions and indexes.

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
Enter fullscreen mode Exit fullscreen mode

You can review the documentation 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. 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.

Fauna AWS Workshop ~ Building an event-driven app with AWS services and Fauna

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

Got questions? Reach out in our Discord channel or in our community forum.

💖 💪 🙅 🚩
fauna_admin
Fauna

Posted on October 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related