easy-easier-easiest-lambdas

Quickstarts for AWS Lambda (“serverless”) functions in React, with and without AWS tooling. Functions connect to a serverless Apache Cassandra database.

View the Project on GitHub januff/easy-easier-easiest-lambdas

AWS Lambdas: Vercel With Next.js

Project Init

npx create-next-app vercel-with-next-js && cd $_

📖 Official Next.js Docs: Create Next App

1. Log Hello World

npm run dev

📖 Official Next.js Docs: API Routes

⚠️ zsh: command not found: next
Use npm run dev instead of next dev

2. Deploy Hello World

📖 Official Vercel Docs: Deployment

3. Pass URL Params

vercel-with-next-js > pages > api > hello.js

export default (req, res) => {
    const name = req.query.name ?? "World"
    res.status(200).json({ body: `Hello ${name}` })
  }

vercel-with-next-js > pages > api > hello > [name].js

export default function handler(req, res) {
    const { name } = req.query
    res.end(`Hello ${name} (v2)`)
  }

📖 Official Next.js Docs: Dynamic API Routes

📖 Official Vercel Docs: Path Segments

4. Install NPM Packages

npm i @astrajs/collections

5. Set Env Values

// pages/api/hello.js

export default async (req, res) => {
  const region = process.env.ASTRA_DB_REGION
  const name = req.query.name ?? "World" 

  res.status(200).json({ 
    body: `Hello ${name}. Region is ${region}.` 
  })
}

📖 Official Next.js Docs: Environmental Variables

📖 Official Vercel Docs: Environmental Variables

6. Test Token

const { createClient } = require("@astrajs/collections");

export default async (req, res) => {
  const region = process.env.ASTRA_DB_REGION
  const name = req.query.name ?? "World"
  
  // create an Astra client  
  const astraClient = await createClient({
    astraDatabaseId: process.env.ASTRA_DB_ID,
    astraDatabaseRegion: process.env.ASTRA_DB_REGION,
    applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
  });
  
  res.status(200).json({ 
    body: `Hello ${name}. 
           Region: ${region}.
           Token: ${astraClient.restClient.applicationToken}.` 
          })
}

7. Deploy Authenticated