sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.19k stars 149 forks source link

Error because D1 is not linked on initial deploy of a worker #285

Closed lukeshay closed 3 months ago

lukeshay commented 3 months ago

The following error is happening on the initial creation of the stack. If I remove line 5 of src/index2.ts, it works on the initial deploy.

image

src/index2.ts:

import { Hono } from "hono";
import { Resource } from "sst";

const app = new Hono();

Resource.RestAPIDB;

app
  .put("/*", async (c) => {
    const key = crypto.randomUUID();
    await Resource.ImagesBucket.put(key, c.req.raw.body, {
      httpMetadata: {
        contentType: c.req.header("content-type"),
      },
    });
    return new Response(`Object created with key: ${key}`);
  })
  .get("/", async (c) => {
    const first = await Resource.ImagesBucket.list().then(
      (res) =>
        res.objects.sort(
          (a, b) => a.uploaded.getTime() - b.uploaded.getTime(),
        )[0],
    );
    const result = await Resource.ImagesBucket.get(first.key);
    c.header("content-type", result.httpMetadata.contentType);
    return c.body(result.body);
  });

export default app;

sst.config.ts:

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

export default $config({
  app(input) {
    return {
      name: "api",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "cloudflare",
    };
  },
  async run() {
    const restApiDb = new sst.cloudflare.D1("RestAPIDB");

    const imagesBucket = new sst.cloudflare.Bucket("ImagesBucket");
    const restApi = new sst.cloudflare.Worker("RestAPI", {
      url: true,
      link: [imagesBucket, restApiDb],
      handler: "src/index2.ts",
    });

    return {
      api: restApi.url,
      imagesBucket: imagesBucket.name,
      restApiDb: restApiDb.nodes.database.name,
    };
  },
});

Ion Verbose Logs:

time=2024-04-20T14:54:24.674-05:00 level=INFO msg="checking for pulumi" path="/Users/luke/Library/Application Support/sst/bin/pulumi"
time=2024-04-20T14:54:24.898-05:00 level=INFO msg="checking for bun" path="/Users/luke/Library/Application Support/sst/bin/bun"
time=2024-04-20T14:54:24.908-05:00 level=INFO msg=args args=[] length=0
time=2024-04-20T14:54:24.908-05:00 level=INFO msg="no existing server found, starting new one"
time=2024-04-20T14:54:24.910-05:00 level=INFO msg="waiting for server to start"
time=2024-04-20T14:54:24.940-05:00 level=INFO msg="checking for pulumi" path="/Users/luke/Library/Application Support/sst/bin/pulumi"
time=2024-04-20T14:54:25.197-05:00 level=INFO msg="checking for bun" path="/Users/luke/Library/Application Support/sst/bin/bun"
time=2024-04-20T14:54:25.206-05:00 level=INFO msg="initializing project" version=0.0.296
time=2024-04-20T14:54:25.207-05:00 level=INFO msg="esbuild building"
time=2024-04-20T14:54:25.210-05:00 level=INFO msg="esbuild built" outfile=/Users/luke/FriendFetcher/api-v3/.sst/eval/eval-1713642865206.mjs
time=2024-04-20T14:54:25.210-05:00 level=INFO msg="evaluating config"
time=2024-04-20T14:54:25.272-05:00 level=INFO msg="config evaluated"
time=2024-04-20T14:54:25.273-05:00 level=INFO msg="checking platform"
time=2024-04-20T14:54:25.550-05:00 level=INFO msg="cloudflare account selected" account=0761603e9b3423fc858e07e5392e51e1
time=2024-04-20T14:54:26.082-05:00 level=INFO msg="found existing bucket" bucket=sst-state
time=2024-04-20T14:54:26.082-05:00 level=INFO msg="loaded config" app=api stage=luke
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=server addr=0.0.0.0:13557
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*project.StackEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*watcher.FileChangedEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*aws.FunctionInvokedEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*aws.FunctionResponseEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*aws.FunctionErrorEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*aws.FunctionLogEvent
time=2024-04-20T14:54:26.083-05:00 level=INFO msg=subscribed type=*project.StackEvent
time=2024-04-20T14:54:26.084-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3
time=2024-04-20T14:54:26.085-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src
time=2024-04-20T14:54:26.086-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/dao
time=2024-04-20T14:54:26.086-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/errors
time=2024-04-20T14:54:26.086-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/mappers
time=2024-04-20T14:54:26.086-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/routes
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/routes/api
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/routes/api/v1
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/schemas
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/schemas/v1
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=watching path=/Users/luke/FriendFetcher/api-v3/src/services
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=subscribed type=*watcher.FileChangedEvent
time=2024-04-20T14:54:26.087-05:00 level=INFO msg=subscribed type=*server.DeployRequestedEvent
time=2024-04-20T14:54:26.088-05:00 level=INFO msg=subscribed type=*project.StackEvent
time=2024-04-20T14:54:26.088-05:00 level=INFO msg="running stack command" cmd=up
time=2024-04-20T14:54:26.088-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:26.088-05:00 level=INFO msg="INFO locking app=api stage=luke"
time=2024-04-20T14:54:26.088-05:00 level=INFO msg="INFO getting data key=lock app=api stage=luke"
time=2024-04-20T14:54:26.121-05:00 level=INFO msg="connecting to server" addr=0.0.0.0:13557
time=2024-04-20T14:54:26.123-05:00 level=INFO msg=subscribed addr=127.0.0.1:50014
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*aws.FunctionInvokedEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*aws.FunctionResponseEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*aws.FunctionErrorEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*aws.FunctionLogEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*project.StackEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*aws.FunctionBuildEvent
time=2024-04-20T14:54:26.124-05:00 level=INFO msg="got server stream"
time=2024-04-20T14:54:26.124-05:00 level=INFO msg=subscribed type=*cloudflare.WorkerBuildEvent
time=2024-04-20T14:54:26.125-05:00 level=INFO msg=subscribed type=*cloudflare.WorkerUpdatedEvent
time=2024-04-20T14:54:26.125-05:00 level=INFO msg=subscribed type=*cloudflare.WorkerInvokedEvent
SST ❍ ion 0.0.296  ready!

➜  App:        api
   Stage:      luke
   Console:    https://console.sst.dev/local/api/luke

time=2024-04-20T14:54:26.692-05:00 level=INFO msg="INFO putting data key=lock app=api stage=luke"
time=2024-04-20T14:54:27.412-05:00 level=INFO msg="INFO pulling state app=api stage=luke out=/Users/luke/FriendFetcher/api-v3/.sst/.pulumi/stacks/api/luke.json"
time=2024-04-20T14:54:28.022-05:00 level=INFO msg="INFO getting passphrase app=api stage=luke"
time=2024-04-20T14:54:28.737-05:00 level=INFO msg="INFO getting data key=secret app=api stage=luke"
time=2024-04-20T14:54:29.178-05:00 level=INFO msg="esbuild building"
time=2024-04-20T14:54:29.213-05:00 level=INFO msg="esbuild built" outfile=/Users/luke/FriendFetcher/api-v3/.sst/platform/eval/eval-1713642869178.mjs
time=2024-04-20T14:54:29.215-05:00 level=INFO msg="files changed" files=98
time=2024-04-20T14:54:29.215-05:00 level=INFO msg="tracked files"
time=2024-04-20T14:54:29.443-05:00 level=INFO msg="built workspace"
time=2024-04-20T14:54:29.673-05:00 level=INFO msg="built stack"
time=2024-04-20T14:54:29.898-05:00 level=INFO msg="built config"
time=2024-04-20T14:54:29.899-05:00 level=INFO msg="running stack command" cmd=up
time=2024-04-20T14:54:30.902-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.587-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.594-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.600-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.603-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.606-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:31.620-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.821-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.822-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.822-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.822-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Creating    RestAPIDB sst:cloudflare:D1
|  Creating    ImagesBucket sst:cloudflare:Bucket
|  Creating    RestAPI sst:cloudflare:Worker
time=2024-04-20T14:54:33.827-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.829-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.831-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.836-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Created     RestAPIDB sst:cloudflare:D1
time=2024-04-20T14:54:33.851-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.853-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.854-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.855-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.856-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.860-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.861-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.862-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.863-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.865-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.866-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.867-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.868-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.869-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.870-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.872-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.873-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.874-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.875-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.877-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.878-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.880-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.899-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Created     ImagesBucket sst:cloudflare:Bucket
time=2024-04-20T14:54:33.904-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.907-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:33.909-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Creating    ImagesBucket sst:cloudflare:Bucket → ImagesBucketBucket cloudflare:index:R2Bucket
time=2024-04-20T14:54:33.912-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Creating    RestAPIDB sst:cloudflare:D1 → RestAPIDBDatabase cloudflare:index:D1Database
time=2024-04-20T14:54:34.989-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Created     ImagesBucket sst:cloudflare:Bucket → ImagesBucketBucket cloudflare:index:R2Bucket (1.1s)
time=2024-04-20T14:54:34.994-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:35.907-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Created     RestAPIDB sst:cloudflare:D1 → RestAPIDBDatabase cloudflare:index:D1Database (2.0s)
time=2024-04-20T14:54:35.912-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:35.934-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:35.935-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:35.938-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Creating    RestAPI sst:cloudflare:Worker → RestAPIScript cloudflare:index:WorkerScript
time=2024-04-20T14:54:42.665-05:00 level=INFO msg=publishing type=*project.StackEvent
|  Error       RestAPI sst:cloudflare:Worker → RestAPIScript cloudflare:index:WorkerScript Error: "RestAPIDB" is not linked
|  at worker.mjs:1585:11 in get
|  at worker.mjs:1591:10
|  (10021)
time=2024-04-20T14:54:42.667-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:42.669-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:42.685-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:42.732-05:00 level=INFO msg="done running stack command"
time=2024-04-20T14:54:42.732-05:00 level=INFO msg="stack command complete"
time=2024-04-20T14:54:43.299-05:00 level=INFO msg=publishing type=*project.StackEvent
time=2024-04-20T14:54:43.299-05:00 level=INFO msg="INFO pushing state app=api stage=luke from=/Users/luke/FriendFetcher/api-v3/.sst/.pulumi/stacks/api/luke.json"

×  Failed
   RestAPI sst:cloudflare:Worker → RestAPIScript cloudflare:index:WorkerScript
   Error: "RestAPIDB" is not linked
   at worker.mjs:1585:11 in get
   at worker.mjs:1591:10
   (10021)

time=2024-04-20T14:54:44.205-05:00 level=INFO msg="INFO unlocking app=api stage=luke"
time=2024-04-20T14:54:44.672-05:00 level=INFO msg="waiting for file changes"
lukeshay commented 3 months ago

I figured out the error is that bindings only work once the handler is invoked.

mkysft commented 2 days ago

@lukeshay Can you expand more on the fix here? I might be running into something similar where the DB is not linked to the Worker.