Search code examples
amazon-web-servicesqueuenestjsamazon-sqs

Nestjs credential issue with Amazon SQS queue


I am trying to publish a message from my nestjs application to an amazon sqs queue.

Here is a snippet of a simple post request, and i am using the package from aws-sdk:

import * as aws from 'aws-sdk';

--
--
--
--
--

@Post('dummySqs')
  async sendMessage(@Body() message: string) {
    const config = {
      apiVersion: '2022-02-21',
      accessKeyId: 'myaccess',
      accessSecretKey: 'mysecret',
      region: 'us-east-1',
      output: 'json',
    };
    aws.config.update(config);
    const sqs = new aws.SQS();
    const params = {
      MessageBody: 'Something about Daniel',
      QueueUrl: 'myurl',
    };
    sqs.sendMessage(params, (err, data) => {
      if (err) {
        console.log('ERRROR', err);
      } else {
        console.log('Success', data.MessageId);
      }
    });
  }

After hitting this post request, i am getting the error below:

ERRROR Error [CredentialsError]: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at Timeout.connectTimeout [as _onTimeout] (/Users/danielongzh/Documents/poc/schedule-distributed-lock-service/node_modules/aws-sdk/lib/http/node.js:69:15)
    at listOnTimeout (internal/timers.js:555:17)
    at processTimers (internal/timers.js:498:7) {
  code: 'CredentialsError',
  time: 2022-02-21T13:31:40.708Z,
  retryable: true,
  originalError: {
    message: 'Could not load credentials from any providers',
    code: 'CredentialsError',
    time: 2022-02-21T13:31:40.707Z,
    retryable: true,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      code: 'TimeoutError',
      time: 2022-02-21T13:31:40.707Z,
      retryable: true,
      originalError: [Object]
    }
  }
}

Any ideas on how to solve it?


Solution

  • Try this:

    import * as aws from 'aws-sdk';
    import config from '../aws.json' 
    as.config.update(config);
    

    or

    this.athena = new Athena({
        region: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_REGION,
        accessKeyId: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_ACCESS_KEY,
        secretAccessKey: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_SECRET_KEY,
    });