Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now GA.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

Migrating from v3 of the CLI? See the CLI migration guide.

AWS Lambda quick start

Use Fauna’s JavaScript driver to query e-commerce demo data using an AWS Lambda function.

 

  1. Create a database with demo data

    Log in to the Fauna Dashboard, and create a database with the following configuration:

    • Region group: Choose your preferred region group, which determines where your data resides.

    • Use demo data: Enabled.

    • Enable backups: Disabled.

    Leave the other options as is.

    Create demo database

  2. Create an authentication secret

    Fauna supports several types of authentication secrets. For this quick start, create a key, which is a type of secret, with the built-in server-readonly role:

    1. In the Dashboard’s Explorer page, select your demo database.

    2. In the database’s Keys tab, click Create Key.

    3. Choose a Role of server (read-only).

    4. Click Save.

    5. Copy the Key Secret.

    Create a key

  3. Create the Lambda function

    1. Open the Functions page of the Lambda console.

    2. Click Create function.

    3. Select Author from scratch.

    4. In the Basic information pane, for Function name, enter myFaunaLambda.

    5. For Runtime, click Node.js 22.x.

    6. Leave architecture set to x86_64.

    7. Under Additional Configurations, select Enable function URL. Under Auth type, select NONE.

    8. Click Create function.

    Create Lambda function

    Save the resulting Function URL. You’ll use the URL later.

    Lambda function URL

  4. Set the FAUNA_SECRET environment variable

    1. On the function’s page, click the Configuration tab, then click Environment variables.

    2. Under Environment variables, click Edit.

    3. Click Add environment variable.

    4. Enter FAUNA_SECRET as the Key. Paste the key secret you copied earlier as the Value.

    5. Click Save.

    Set FAUNA_SECRET environment variable

  5. Initialize a Node.js project

    In your local machine’s terminal:

    • Create and navigate to a project directory named myFaunaLambda.

    • Initialize a Node.js project.

    • Install the Fauna JavaScript driver.

    mkdir myFaunaLambda
    cd myFaunaLambda
    npm init -y
    npm install fauna
  6. Add code for the Lambda function

    In the myFaunaLambda directory, create an index.mjs file and add the following code:

    import { Client, fql, FaunaError } from "fauna";
    
    export const handler = async (event) => {
      // Initialize the client to connect to Fauna
      // using the `FAUNA_SECRET` environment variable.
      const client = new Client();
    
      try {
        // Compose a query using an FQL template string.
        // The query calls the `Product` collection's
        // `sortedByPriceLowToHigh()` index. It projects the `name`,
        // `description`, and `price` fields covered by the index.
        const query = fql`
          Product.sortedByPriceLowToHigh() {
            name,
            description,
            price
          }`;
    
        // Run the query.
        const response = await client.query(query);
    
        return {
          statusCode: 200,
          body: JSON.stringify(response.data)
        };
      } catch (error) {
        console.error('Fauna Query Error:', error);
        return {
          statusCode: 500,
          body: JSON.stringify({
            error: error instanceof FaunaError ? error.message : 'Unknown error'
          })
        };
      } finally {
        client.close();
      }
    };
  7. Package the function

    In the myFaunaLambda directory, run the following command to package the function as a ZIP file:

    zip -r function.zip .
  8. Upload the function

    1. On the Lambda function page, click the Code tab, then click Upload from and select .zip file.

    2. Click Upload.

    3. Select the function.zip file in the myFaunaLambda directory.

    4. Click Save.

    Upload Lambda function

  9. Test the function

    To test the function, make a GET request to the Function URL you saved earlier. Using curl:

    curl <AWS_LAMBDA_FUNCTION_URL> | jq .

    The response contains the results of the query:

    {
      "data": [
        {
          "name": "single lime",
          "description": "Conventional, 1 ct",
          "price": 35
        },
        {
          "name": "cilantro",
          "description": "Organic, 1 bunch",
          "price": 149
        },
        {
          "name": "limes",
          "description": "Conventional, 16 oz bag",
          "price": 299
        },
        {
          "name": "organic limes",
          "description": "Organic, 16 oz bag",
          "price": 349
        },
        {
          "name": "avocados",
          "description": "Conventional Hass, 4ct bag",
          "price": 399
        },
        {
          "name": "pizza",
          "description": "Frozen Cheese",
          "price": 499
        },
        {
          "name": "cups",
          "description": "Translucent 9 Oz, 100 ct",
          "price": 698
        },
        {
          "name": "taco pinata",
          "description": "Giant Taco Pinata",
          "price": 2399
        },
        {
          "name": "donkey pinata",
          "description": "Original Classic Donkey Pinata",
          "price": 2499
        }
      ]
    }

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!