AWS Lambda quick start
Use Fauna’s JavaScript driver to query e-commerce demo data using an AWS Lambda function.
-
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 an authentication secret
Fauna supports several types of authentication secrets. Create a key, which is a type of secret, with the built-in
server-readonly
role:-
In the Dashboard’s Explorer page, select your demo database.
-
In the database’s Keys tab, click Create Key.
-
Choose a Role of server (read-only).
-
Click Save.
-
Copy the Key Secret.
-
-
Create the Lambda function
-
Open the Functions page of the Lambda console.
-
Click Create function.
-
Select Author from scratch.
-
In the Basic information pane, for Function name, enter
myFaunaLambda
. -
For Runtime, click Node.js 22.x.
-
Leave architecture set to x86_64.
-
Under Additional Configurations, select Enable function URL. Under Auth type, select NONE.
-
Click Create function.
Save the resulting Function URL. You’ll use the URL later.
-
-
Set the FAUNA_SECRET environment variable
-
On the function’s page, click the Configuration tab, then click Environment variables.
-
Under Environment variables, click Edit.
-
Click Add environment variable.
-
Enter
FAUNA_SECRET
as the Key. Paste the key secret you copied earlier as the Value. -
Click Save.
-
-
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
-
-
Add code for the Lambda function
In the
myFaunaLambda
directory, create anindex.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 pages = await client.paginate(query); // Iterate through the results. const products = []; for await (const product of pages.flatten()) { products.push(product); } return { statusCode: 200, body: JSON.stringify(products) }; } 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(); } };
-
Package the function
In the
myFaunaLambda
directory, run the following command to package the function as a ZIP file:zip -r function.zip .
-
Upload the function
-
On the Lambda function page, click the Code tab, then click Upload from and select .zip file.
-
Click Upload.
-
Select the function.zip file in the myFaunaLambda directory.
-
Click Save.
-
-
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:
[ { "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 } ]