Chapter 1: Configuration
Project Configuration

Project setup

Make sure you have the serverless framework installed. You can install it by running the following command in a terminal:

$ npm i serverless -g

AWS environment variables setup

Login to your AWS account. Select programmatic access from the menu and copy the environment variables.

💡 If you are new to AWS, follow this guide to configure your AWS credentials.

Set the environment variables by running the following command in your terminal:

export AWS_ACCESS_KEY_ID="ASIA......"
export AWS_SECRET_ACCESS_KEY="iaGNguG/RYA2zFT6...."

You can also set up an AWS profile. AWS allows you to set up multiple profiles. Follow this guide to learn more.

Create a new AWS Serverless project

Create a new project by running the following command in your terminal:

$ serverless create --template aws-nodejs

Find the serverles.yml file in the code and add the following changes:

service: fauna-aws-workshop
 
frameworkVersion: '3'
 
provider:
  name: aws
  runtime: nodejs14.x
  httpApi:
    cors: true
 
functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          method: POST
          path: /book-vacation

In the code above, we are creating a simple REST API using the serverless framework infrastructure as code.

Next, run the following command to ensure you can deploy your stack to AWS:

$ sls deploy

Once the deployment is complete, you get a URL for your API. Make a POST call to the endpoint to ensure everything works as expected.

Untitled

💡 Pro Tip:

The serverless framework creates all the cloud formation files under a folder called .serverless. You can port this to your solution if you want to use another IoC (SAM, CDK, etc.). The workshop demo app repository includes the cloud formation files.

Configure Fauna

Head over to dashboard.fauna.com and create a new database.

Then select Security > Keys > New Key option to generate a new key.

Untitled

Select Admin for the Role, give your key a name, and select Save.

Untitled

Next, head back to the code. Add the fauna-serverless plugin and dot-env plugin to your project. Run the following command in your terminal to install these packages:

$ npm i @fauna-labs/serverless-fauna serverless-dotenv-plugin --save

Create a .env file in the root of your application and add the Fauna key and domain:

FAUNA_ROOT_KEY='fnAEsUSh...' # Your Admin Key
FAUNA_DOMAIN='db.fauna.com' # Your database's Region Group Domain

Region groups in Fauna

Region Groups provide control over where your data resides in Fauna. Each database, its storage, and compute services exist in a specific geographic region. You control the region group by specifying the domain.

  • Here are some options for different Region Groups and their domains in Fauna.

    RegionDomain
    US region groupdb.us.fauna.com
    EU region groupdb.eu.fauna.com
    Classic region groupdb.fauna.com

We want to create a new collection called Vacation and an index vacations_by_userId to find vacations by userId. You can do this through the serverless framework without touching any UI. Add the following changes to your serverless.yml file.

service: fauna-aws-workshop
 
frameworkVersion: '2.72.3'
 
**# Specify dotenv
useDotenv: true
 
# Add Fauna plugin
plugins:
  - serverless-dotenv-plugin
  - fauna-labs/serverless-fauna
 
fauna:
  client:
    secret: ${env:FAUNA_ROOT_KEY}
    domain: ${env:FAUNA_DOMAIN}
  collections:
    Vacation: 
      name: Vacation
  indexes:
    vacations_by_userId:
      name: vacations_by_userId
      source: Vacation
      terms:
        fields:
          - data.userId**
 
provider:
  ... # rest of the file

Run the sls deploy command to push the changes. A new collection and an index in your Fauna database is created once the deploy script finishes running.

Last updated on August 5, 2022