Connection refused to AWS running on localstack

Hello, i'm just trying to list my s3 buckets on node red, and I wrote a simple node js application:

var AWS = global.get('aws-sdk');
var ep = new AWS.Endpoint('http://localhost:4566');
var s3 = new AWS.S3({endpoint:ep});

s3.listBuckets(function(err,data){
    if(err) console.log(err,err.stack);
    else console.log(data.Buckets);
});

It works when i run node app.js siply from bash, but If i try to run it on a function node in node-red, I get this error:

Error: connect ECONNREFUSED 169.254.169.254:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
  message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
  errno: -111,
  code: 'CredentialsError',
  syscall: 'connect',
  address: '169.254.169.254',
  port: 80,
  time: 2022-07-11T11:52:37.421Z,
  originalError: {
    message: 'Could not load credentials from any providers',
    errno: -111,
    code: 'CredentialsError',
    syscall: 'connect',
    address: '169.254.169.254',
    port: 80,
    time: 2022-07-11T11:52:37.421Z,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      errno: -111,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '169.254.169.254',
      port: 80,
      time: 2022-07-11T11:52:37.420Z,
      originalError: [Object]
    }
  }
} Error: connect ECONNREFUSED 169.254.169.254:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)

I tried debugging it and the endpoint looks correctly configured. I should mention I'm running both node-red and localstack through docker, and they are connected to the same docker network.

Solved the problem: basically I tried to refer to localhost from within the container so obviously it tried to access the wrong resource. All I had to do was change from
http://localhost:4566
to
http://localstackContainerName:4566

Looks like many of us face this type of connection problems with localstack. I faced issues while connecting to SQS using localstack. I needed to change the docker-compose setup of localhost container to fix it. Detail steps here How to Solve LocalStack Always Connecting To LocalHost – W3 Spot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.