aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

"endpoint" configuration parameter is ignored by DynamoDBClient

Open IevgenRagulin opened this issue 3 years ago • 8 comments

Describe the bug

Trying to migrate to aws-sdk 3 from aws-sdk 2. We are using endpoint configuration parameter to have the Dynamo client talk to a local instance of Dynamo for unit tests, but the SDK ignores it, and keeps trying to reach the remote Dynamo, resulting in an error ExpiredToken: The security token included in the request is expired

Your environment

Node 12.20.1, MacOS Catalina

SDK version number

"@aws-sdk/client-dynamodb": "3.16.0"

Is the issue in the browser/Node.js/ReactNative?

Node.js

Details of the browser/Node.js/ReactNative version

Node 12.20.1

Steps to reproduce

const {DynamoDBClient, BatchWriteItemCommand} = require('@aws-sdk/client-dynamodb');
const {marshall} = require('@aws-sdk/util-dynamodb');

(async () => {
  const dynamoClient = new DynamoDBClient({endpoint: "http://localhost:2348"});
  const ids = ['1232'];
  const params = {
    RequestItems: {
      'mytable': ids.map((id) => {
        return {
          DeleteRequest: {
            Key: marshall({id}),
          },
        };
      }),
    },
  };
const command = new BatchWriteItemCommand(params);
await dynamoClient.send(command);
})().catch(e => {
  console.log(e, 'error');
})

Observed behavior

AWS SDK client tries to send requests to real Dynamo AWS.

Expected behavior

AWS SDK client should be sending requests to local Dynamo instance.

IevgenRagulin avatar May 21 '21 23:05 IevgenRagulin

Hi @IevgenRagulin thanks for opening this issue, is it still an issue with the latest version of the SDK? I was not able to reproduce it.

ajredniwja avatar Nov 22 '21 17:11 ajredniwja

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

github-actions[bot] avatar Nov 30 '21 00:11 github-actions[bot]

I'm facing a similar issue, only with SNS connected to localstack. I'm setting the right endpoint URL, but looks like it still tries to connect to AWS because of this error: The security token included in the request is invalid

gornostal avatar Dec 04 '21 14:12 gornostal

Hi @gornostal, i´m getting the same error on SQS, have you fixed it?

frannav avatar Jan 17 '22 11:01 frannav

@frannav yes, I fixed it. I think by passing AWS_REGION env variable to my app container. I also have this in my config:

const snsConfig: SNSClientConfig = {
  region: process.env.AWS_REGION,
  endpoint: process.env.NODE_ENV == 'dev' ? 'http://localstack:4566' : null,
};

I forgot what exactly fixed the issue. It was a month ago.

gornostal avatar Jan 24 '22 19:01 gornostal

@frannav ah, I remember -- it was an issue related to nestjs dependency injection. I've misconfigured it and it would create an SNSClient instance without passing the config object in there.

gornostal avatar Jan 24 '22 19:01 gornostal

Thanks!

frannav avatar Feb 16 '22 20:02 frannav

@ajredniwja , I confirm it's still an issue when using the latest sdk (3.85.0). Passing AWS_REGION as suggested by @gornostal doesn't help in my case.

IevgenRagulin avatar May 06 '22 20:05 IevgenRagulin

Revisiting during review meeting. The action item would be to check if endpoint which contains localhost is overridden in any middleware.

trivikr avatar Nov 11 '22 22:11 trivikr

@gornostal Our endpoint resolution has changed since this issue was reported in May'21.

Is this issue still reproducible for you?

trivikr avatar Feb 14 '23 17:02 trivikr

The HttpRequest is created in serializer.

I logged the contents in buildHttpRpcRequest before HttpRequest is created, for testing listTables

// node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js
const buildHttpRpcRequest = async (context, headers, path, resolvedHostname, body) => {
    // ...
    console.log({ contents });
    return new protocol_http_1.HttpRequest(contents);
};

And ran the following script:

import { DynamoDB } from "@aws-sdk/client-dynamodb";
const client = new DynamoDB({ endpoint: "http://localhost:2348" });

try {
  await client.listTables({});
} catch (err) {
  console.log(err);
}

I verified that request attempt is made for localhost, as required by the configuration. The error is thrown on my laptop, as it's not running local DynamoDB instance.

{
  contents: {
    protocol: 'http:',
    hostname: 'localhost',
    port: 2348,
    method: 'POST',
    path: '/',
    headers: {
      'content-type': 'application/x-amz-json-1.0',
      'x-amz-target': 'DynamoDB_20120810.ListTables'
    },
    body: '{}'
  }
}
Error: connect ECONNREFUSED 127.0.0.1:2348
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 2348,
  '$metadata': { attempts: 1, totalRetryDelay: 0 }
}

@gornostal Is this issue reproducible for you?

My environment:

trivikr avatar Feb 14 '23 18:02 trivikr

@trivikr sorry, I can't confirm. I'm not working on that project anymore.

gornostal avatar Feb 14 '23 20:02 gornostal

Closing this issue, as it's not reproducible as per testing in https://github.com/aws/aws-sdk-js-v3/issues/2416#issuecomment-1430156717, and original author is not working on the project.

If this issue is reproducible for you, please create a new bug report (recommended), or comment on this issue with a minimal repro.

trivikr avatar Feb 14 '23 21:02 trivikr

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.

github-actions[bot] avatar Mar 02 '23 00:03 github-actions[bot]