🚀 Fauna Architectural Overview White Paper: Learn how Fauna's database engine scales with zero ops required
Download free
Fauna logo
Product
Solutions
Pricing
Resources
Company
Log InContact usStart for free
Fauna logo
Pricing
Customers
Log InContact usStart for free
© 0 Fauna, Inc. All Rights Reserved.

Related posts

Building a REST API with AWS Lambda, Fauna, and Serverless FrameworkAchieving multi-region reliability with AWS Step Functions and FaunaHow to maintain high availability in a multi-tenant SaaS app

Start for free

Sign up and claim your forever-free Fauna account
Sign up and get started

Table of Contents

Fauna Serverless Framework AWS

Implementing Fauna as infrastructure as code with Serverless Framework

Shadid Haque|Oct 20th, 2022|

Categories:

ServerlessTutorialAmazon Web Services
⚠️ Disclaimer ⚠️

This post refers to a previous version of FQL.

This post refers to a previous version of FQL (v4). For the most current version of the language, visit our FQL documentation.

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
or using yarn
$ yarn add @fauna-labs/serverless-fauna
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
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
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
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.

Building a REST API with AWS Lambda, Fauna, and Serverless Framework

This tutorial teaches you how to build a serverless REST API using AWS Lambda and Fauna (as your database). You use the Serverless Framework as your infrastructure as code for this tutorial. Create a new serverless project by running the following command to get started.

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.

If you enjoyed our blog, and want to work on systems and challenges related to globally distributed systems, and serverless databases, Fauna is hiring

Share this post

‹︁ PreviousNext ›︁

Subscribe to Fauna's newsletter

Get latest blog posts, development tips & tricks, and latest learning material delivered right to your inbox.