aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.14k stars 581 forks source link

[Error] TypeError [ERR_INVALID_ARG_TYPE]" argument must be of type string. #6134

Closed thorwebdev closed 6 months ago

thorwebdev commented 6 months ago

Checkboxes for prior research

Describe the bug

When using @aws-sdk/client-bedrock-runtime within the Supabase Edge Runtime I get the following error because this runtime doesn't have access to the file system.

SDK version number

@aws-sdk/client-bedrock-runtime@3.583.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

deno 1.43.3 (release, aarch64-apple-darwin); v8 12.4.254.13; typescript 5.4.5

Reproduction Steps

  1. Install the Supabase CLI
  2. Run supabase init
  3. Run supabase functions new bedrock
  4. Paste the following code into the newly created supabase/functions/bedrock/index.ts file:
import {
  BedrockRuntimeClient,
  InvokeModelCommand,
} from "npm:@aws-sdk/client-bedrock-runtime";
import { createClient } from "npm:@supabase/supabase-js";
import { decode } from "npm:base64-arraybuffer";

console.log("Hello from Amazon Bedrock!");

Deno.serve(async (req) => {
  const client = new BedrockRuntimeClient({
    region: "us-west-2",
    credentials: {
      accessKeyId: Deno.env.get("AWS_ACCESS_KEY_ID") ?? "",
      secretAccessKey: Deno.env.get("AWS_SECRET_ACCESS_KEY") ?? "",
      sessionToken: Deno.env.get("AWS_SESSION_TOKEN") ?? "",
    },
  });

  const { prompt, seed } = await req.json();
  console.log(prompt);
  const input = {
    contentType: "application/json",
    accept: "*/*",
    modelId: "amazon.titan-image-generator-v1",
    body: JSON.stringify({
      "taskType": "TEXT_IMAGE",
      "textToImageParams": { "text": prompt },
      "imageGenerationConfig": {
        "numberOfImages": 1,
        "quality": "standard",
        "cfgScale": 8.0,
        "height": 512,
        "width": 512,
        "seed": seed ?? 0,
      },
    }),
  };

  const command = new InvokeModelCommand(input);
  const response = await client.send(command);
  console.log(response);

  if (response.$metadata.httpStatusCode === 200) {
    const { body, $metadata } = response;

    const textDecoder = new TextDecoder("utf-8");
    const jsonString = textDecoder.decode(body.buffer);
    const parsedData = JSON.parse(jsonString);
    console.log(parsedData);
    const image = parsedData.images[0];

    const supabaseClient = createClient(
      // Supabase API URL - env var exported by default.
      Deno.env.get("SUPABASE_URL")!,
      // Supabase API ANON KEY - env var exported by default.
      Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
    );

    const { data: upload, error: uploadError } = await supabaseClient.storage
      .from("images")
      .upload(`${$metadata.requestId ?? ""}.png`, decode(image), {
        contentType: "image/png",
        cacheControl: "3600",
        upsert: false,
      });
    if (!upload) {
      return Response.json(uploadError);
    }
    const { data } = supabaseClient
      .storage
      .from("images")
      .getPublicUrl(upload.path!);
    return Response.json(data);
  }

  return Response.json(response);
});
  1. Create a supabase/.env file with your AWS credentials
    AWS_DEFAULT_REGION="us-west-2"
    AWS_ACCESS_KEY_ID=""
    AWS_SECRET_ACCESS_KEY="" 
    AWS_SESSION_TOKEN=""
  2. Run supabase functions serve --env-file supabase/.env
  3. Trigger the function:
    curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/bedrock' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
    --header 'Content-Type: application/json' \
    --data '{"prompt":"A beautiful picture of a bird"}'

Observed Behavior

[Error] TypeError [ERR_INVALID_ARG_TYPE]" argument must be of type string. Received null
    at assertPath (ext:deno_node/path/_util.ts:10:11)
    at join (ext:deno_node/path/_posix.ts:77:5)
    at getCredentialsFilepath (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/shared-ini-file-loader/3.0.0/dist-cjs/index.js:75:117)
    at loadSharedConfigFiles (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/shared-ini-file-loader/3.0.0/dist-cjs/index.js:132:22)
    at file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/node-config-provider/3.0.0/dist-cjs/index.js:51:105
    at file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:79:33
    at eventLoopTick (ext:core/01_core.js:64:7)
    at async coalesceProvider (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:106:18)
    at async file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:117:20 {
  code: "ERR_INVALID_ARG_TYPE",
  name: "TypeError",
  toString: [Function (anonymous)]
}

runtime has escaped from the event loop unexpectedly: event loop error: TypeError [ERR_INVALID_ARG_TYPE]" argument must be of type string. Received null
    at assertPath (ext:deno_node/path/_util.ts:10:11)
    at join (ext:deno_node/path/_posix.ts:77:5)
    at getCredentialsFilepath (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/shared-ini-file-loader/3.0.0/dist-cjs/index.js:75:117)
    at loadSharedConfigFiles (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/shared-ini-file-loader/3.0.0/dist-cjs/index.js:132:22)
    at file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/node-config-provider/3.0.0/dist-cjs/index.js:51:105
    at file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:79:33
    at eventLoopTick (ext:core/01_core.js:64:7)
    at async coalesceProvider (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:106:18)
    at async file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@smithy/property-provider/3.0.0/dist-cjs/index.js:117:20
failed to send request to user worker: request has been cancelled by supervisor
user worker failed to respond: request has been cancelled by supervisor
WorkerRequestCancelled: request has been cancelled by supervisor
    at async Promise.allSettled (index 1)
    at async UserWorker.fetch (ext:sb_user_workers/user_workers.js:77:21)
    at async Object.handler (file:///home/deno/main/index.ts:165:14)
    at async respond (ext:sb_core_main_js/js/http.js:162:14) {
  name: "WorkerRequestCancelled"
}

Expected Behavior

Since I'm passing the required configuration BedrockRuntimeClientConfig within the code, so in my scenario the client shouldn't need to access the file system.

Possible Solution

Would you be able to wrap the file system access in a way that it doesn't throw an error? For context, if I run my code in a local environment that exposes the file system, it works fine.

Additional Information/Context

No response

thorwebdev commented 6 months ago

Do note that this is not a Deno specific issue. It will happen in any environment that doesn't have filesystem. e.g. vercel edge functions, cloudflare workers etc.

thorwebdev commented 6 months ago

Okay, I was able to work around this by mocking the file system for the AWS config files:

In my .env file I do

AWS_SHARED_CREDENTIALS_FILE="./aws/credentials"
AWS_CONFIG_FILE="./aws/config"

Then in my function I do

import {
  prepareVirtualFile,
} from "https://deno.land/x/mock_file@v1.1.2/mod.ts";

prepareVirtualFile("./aws/config");
prepareVirtualFile("./aws/credentials");

This makes it work in Supabase Edge Functions. So feel free to close this issue, but I'm still not sure if the file system should be needed at all in this scenario, so you might want to fix this nonetheless. Thanks.

RanVaknin commented 6 months ago

Hi @thorwebdev ,

The AWS SDK only provides support for AWS resources and not 3rd party tools. So instead of Supabase functions, we offer support for Lambda.

If you need a certain AWS product to work with supabase you'll need to reach out to their support for further assistance.

Thanks, Ran~

github-actions[bot] commented 5 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.