Admiral-Piett / goaws

AWS (SQS/SNS) Clone for Development testing
MIT License
767 stars 144 forks source link

Error when testing with node using aws-sdk v2 #268

Closed ztrange closed 10 months ago

ztrange commented 10 months ago

Using the default config. I only changed the Host value in line 3 to localhost

The error thrown by aws node sdk v2:

UnknownEndpoint: Inaccessible host: `us-east-1.localhost' at port `undefined'. This service may not be available in the `us-east-1' region.

This is the code that I used to test:

const AWS = require('aws-sdk');

const config = {
  region: 'us-east-1',
  accessKeyId: 'x',
  secretAccessKey: 'x',
  endpoint: new AWS.Endpoint('http://localhost:4100'),
  sslEnabled: false,
};

AWS.config.update(config);

const sns = new AWS.SNS();

function publish(msg) {
  const publishParams = {
    TopicArn: 'arn:aws:sns:us-east-1:100010001000:local-topic1',
    Message: `Hello World, ${msg}}`,
  };

  sns.publish(publishParams, function after(err, data) {
    if (err) {
      console.log(err.stack);
      return;
    }
    console.log(data);
  });
}

const sqs = new AWS.SQS();

function listen() {
  const listenParams = {
    QueueUrl: 'http://us-east-1.localhost:4100/100010001000/local-queue3',
    AttributeNames: ['All'],
    MaxNumberOfMessages: 10,
    MessageAttributeNames: ['All'],
    WaitTimeSeconds: 5,
  };

  sqs.receiveMessage(listenParams, function after(err, data) {
    if (err) {
      console.log(err.stack);
      return;
    }
    console.log(data);
  });
}

for (let i = 0; i < 2; i += 1) {
  publish(`message: ${i}`);
}

listen();
ztrange commented 10 months ago

I figured out the issue is caused by the sdk v2 and a workaround is using the node sdk v3:

const { SNSClient, PublishCommand } = require('@aws-sdk/client-sns');
const { SQSClient, ReceiveMessageCommand } = require('@aws-sdk/client-sqs');

async function publish(msg) {
  const sns = new SNSClient({
    region: 'us-east-1',
    accessKeyId: 'x',
    secretAccessKey: 'x',
    endpoint: 'http://localhost:4200',
  });

  await sns.send(
    new PublishCommand({
      TopicArn: 'arn:aws:sns:us-east-1:100010001000:local-topic1',
      Message: `Hello World, ${msg}}`,
    }),
  );
}

async function listen() {
  const sqs = new SQSClient({
    region: 'us-east-1',
    accessKeyId: 'x',
    secretAccessKey: 'x',
    endpoint: 'http://localhost:4200',
  });

  const response = await sqs.send(
    new ReceiveMessageCommand({
      QueueUrl: 'http://localhost:4200/queue/local-queue3',
      AttributeNames: ['All'],
      MaxNumberOfMessages: 10,
      MessageAttributeNames: ['All'],
      WaitTimeSeconds: 5,
    }),
  );

  return response;
}

async function run() {
  await publish('test');

  const result = await listen();

  console.log(result);
}

run().then(() => console.log('done'));
ztrange commented 10 months ago

I'm closing this, since it was only created FYI.