aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.96k stars 556 forks source link

TypeError on SQS client QueueUrl in Node 20 Lambda #6150

Open wmhartl opened 1 month ago

wmhartl commented 1 month ago

Describe the issue

The following code (anonymized/simplified a bit) warns every time, whereas it used to work without warning. Has an undocumented change occurred here or is one impending for the SQS Client?

  const sqs = new SQSClient({region: "us-x-#"});
  const QueueUrl = "string_for_queue_name";
  ...
  const command = new SendMessageCommand({QueueUrl, MessageBody});
  const response = await sqs.send(command);
  return response;

The warning (again, anonymized a bit) we're getting is:

2024-05-30T19:46:33.112Z    #aaa###a-##aa-##a#-#aa#-#aaaa####a##    WARN    TypeError: Invalid URL
    at new URL (node:internal/url:796:36)
    at /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:56:28
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-endpoint/dist-cjs/index.js:199:12
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:127:16
    at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
    ... {
  code: 'ERR_INVALID_URL',
  input: 'string_for_queue_name'
}

Links

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-sqs/Class/SQSClient/

RanVaknin commented 1 month ago

Hi @wmhartl ,

I'm a bit confused about the description of the issue. I tested a code snippet with an invalid queue url both in lambda runtime v18 and v20 and the result is similar:

Code:

import { SQSClient, SendMessageBatchCommand } from "@aws-sdk/client-sqs"

const sqsClient = new SQSClient({ region: "us-east-1" });

export const handler = async (event) => {
      const command = new SendMessageBatchCommand({
        QueueUrl: 'test-queue', // this is an invalid queue URL, since it's not a URL
     // QueueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789012/test-queue', // this shouldnt raise this warning
        Entries: [
            { Id: 'foo', MessageBody: JSON.stringify({ valid: 'message' }) },
        ],
    });

    try {
        const response = await sqsClient.send(command);
        console.log(response);
    } catch (error) {
        console.log(error);
    }
};

Output lambda with Node v18:

2024-05-31T20:20:34.656Z    5e571c32-c7d1-4aed-a415-660f8a8d8b4d    WARN    TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:676:13)
    at /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:56:28
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-endpoint/dist-cjs/index.js:199:12
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:158:16
    at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
    at async Runtime.handler (file:///var/task/index.mjs:15:26) {
  input: 'test-queue',
  code: 'ERR_INVALID_URL'
}

Output lambda with Node v20:

2024-05-31T20:18:04.680Z    0d84269f-0ff7-446b-939b-e374931a5b90    WARN    TypeError: Invalid URL
    at new URL (node:internal/url:797:36)
    at /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:56:28
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-endpoint/dist-cjs/index.js:199:12
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:158:16
    at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
    at async Runtime.handler (file:///var/task/index.mjs:15:26) {
  code: 'ERR_INVALID_URL',
  input: 'test-queue'
}

The warning is being raised in both cases and it is being raised because you are providing the queue name instead of the queue URL

Can you please clarify the problem?

Thanks, Ran~

wmhartl commented 1 month ago

Hi Ran, thanks so much for responding. These warnings are new to me, I haven't seen them until recently. We did just recently make a big package version jump a few weeks ago, so perhaps that brought this on, but the queue name has worked for a long time without issuing a warning, and nothing in any docs I have seen state we have to use the URL.

I did see in this semi-recent other issue https://github.com/aws/aws-sdk-js-v3/issues/5938 there is a config option I haven't seen elsewhere, useQueueUrlAsEndpoint: false (I'll admit I could have perhaps not dug around enough to find it). I haven't had a chance to test that, but if I set that, I'd hope the warning will be suppressed and that config option be supported longer term.

Ultimately, I'm mostly concerned here for clarity in the docs as well as if you all plan to stop allowing the use of the queue name here.

Thanks for your support!

RanVaknin commented 3 weeks ago

Hi @wmhartl ,

Thanks for digging this up. It appears this was added by the SDK team for security purposes. I agree that this change in behavior needs to be better documented. As it stands we are waiting on some more guidance from the SQS team wrt how we want to document this. Since the AWS SDKs are all code-generated from the API model of each AWS service, a lot of the SDK behavior is directly derived from the respective service side definitions.

I was able to confirm that use useQueueUrlAsEndpoint: false does work. We are kind of up in the air at the moment with how to document this, but thanks for bringing this to my attention. I'll mark this for review and see if we can re-engage with the SQS team to get this sorted out.

Thanks again, Ran~