ssut / nestjs-sqs

A project to make SQS easier to use within NestJS, with decorator-based handling and seamless NestJS-way integration.
MIT License
204 stars 54 forks source link

SQS receive message failed: Could not load credentials from any providers #69

Closed KozielGPC closed 11 months ago

KozielGPC commented 11 months ago

I'm trying to setup a sqs consumer in nestjs using this package but I'm having some troubles. When I start my application I get the error below:

image

My setup:

"dependencies": {
    "@apollo/server": "^4.9.3",
    "@graphql-tools/utils": "^10.0.6",
    "@nestjs/apollo": "^12.0.9",
    "@nestjs/common": "^10.2.6",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.2.6",
    "@nestjs/graphql": "^12.0.9",
    "@nestjs/platform-express": "^8.0.0",
    "@prisma/client": "^5.3.1",
    "@ssut/nestjs-sqs": "^2.2.0",
    "aws-sdk": "^2.1473.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "graphql": "^16.8.1",
    "graphql-tools": "^9.0.0",
    "prisma": "^5.3.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
yudikubota commented 11 months ago

You need to set your AWS credentials in environment variables or pass a SQSClient instance to the consumer config.

export AWS_SECRET_ACCESS_KEY=...
export AWS_ACCESS_KEY_ID=...

Example:

import { SQSClient } from '@aws-sdk/client-sqs';

const endpoint = ...
const accountId = ...
const queueName = ...

const config = {
    consumers: [
        {
            name: queueName,
            queueUrl: `${endpoint}/${accountId}/${queueName}`,
            sqs: new SQSClient({
                region: 'AWS_REGION',
                credentials: {
                    accessKeyId: 'AWS_ACCESS_KEY_ID',
                    secretAccessKey: 'AWS_SECRET_ACCESS_KEY',
                },
                endpoint,
            }),
        }
    ],
}

Check the sqs-consumer docs: https://github.com/bbc/sqs-consumer#credentials

neeraj-banknovo commented 11 months ago

You can try passing Sqs client in consumers as well it works.


// your sqs client
const sqsClient = new SQSClient({
  region: config.aws.region,
  credentials: {
    accessKeyId: config.aws.accessKey,
    secretAccessKey: config.aws.secret,
  },
});

// your consumers
consumers: [
        {
          name: 'queue-name',
          queueUrl: 'queue-url',
          sqs: sqsClient,
       }
 ],