DEV Community

Alex Spinov
Alex Spinov

Posted on

SST v3 Has a Free API That Deploys Full-Stack Apps to AWS With Zero Config

SST (Serverless Stack) v3 is the framework for building full-stack apps on AWS. Define your infrastructure in TypeScript — Next.js, Remix, Astro, or any framework.

sst.config.ts: Your Infrastructure

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: "scraping-app",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "aws",
    };
  },
  async run() {
    // Database
    const table = new sst.aws.Dynamo("Products", {
      fields: { pk: "string", sk: "string" },
      primaryIndex: { hashKey: "pk", rangeKey: "sk" },
    });

    // Storage
    const bucket = new sst.aws.Bucket("Exports");

    // API
    const api = new sst.aws.Function("Api", {
      handler: "packages/functions/src/api.handler",
      link: [table, bucket],
      url: true,
    });

    // Cron job
    new sst.aws.Cron("DailyScrape", {
      schedule: "rate(1 hour)",
      job: {
        handler: "packages/functions/src/scrape.handler",
        link: [table],
        timeout: "5 minutes",
      },
    });

    // Next.js frontend
    const site = new sst.aws.Nextjs("Web", {
      link: [api, table],
    });

    return { api: api.url, site: site.url };
  },
});
Enter fullscreen mode Exit fullscreen mode

Resource Linking: Type-Safe Access

// packages/functions/src/api.ts
import { Resource } from "sst";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { PutCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";

const db = new DynamoDBClient({});

export async function handler(event) {
  // Resource.Products.name is auto-typed from sst.config.ts!
  const result = await db.send(new QueryCommand({
    TableName: Resource.Products.name,
    KeyConditionExpression: "pk = :pk",
    ExpressionAttributeValues: { ":pk": "products" },
  }));

  return { statusCode: 200, body: JSON.stringify(result.Items) };
}
Enter fullscreen mode Exit fullscreen mode

CLI: Live Lambda Dev

# Start dev mode — changes deploy in milliseconds
sst dev

# Deploy to production
sst deploy --stage production

# Remove
sst remove

# Open console
sst console
Enter fullscreen mode Exit fullscreen mode

Secrets

# Set secret
sst secret set DatabaseUrl "postgres://..."
sst secret set ApiKey "sk-..."

# Use in code
const url = Resource.DatabaseUrl.value;
Enter fullscreen mode Exit fullscreen mode

Supported Frameworks

// Next.js
new sst.aws.Nextjs("Web");

// Remix
new sst.aws.Remix("Web");

// Astro
new sst.aws.Astro("Web");

// SvelteKit
new sst.aws.SvelteKit("Web");

// Static site
new sst.aws.StaticSite("Docs", { build: { command: "npm run build", output: "dist" } });
Enter fullscreen mode Exit fullscreen mode

Deploy scraping apps to AWS? My Apify tools + SST = production-grade infrastructure.

Custom AWS deployment? Email spinov001@gmail.com

Top comments (0)