Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

node.jsAmazon Web-ServicesAws Config

node.js Problem Overview


When I am trying to load AWS credentials in my project it gives back an error.

When using credentials in plain text everything works good but when I am trying to use environment variables it's not working.

Error message. :

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Here is my tried code :

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


const SESConfig = {
    apiVersion: "2010-12-01",
    accessKeyId: process.env.AWS_SECRET_KEY,
    accessSecretKey: process.env.AWS_SECRET_KEY,
    region: "us-east-1"
}
AWS.config.update(SESConfig);
var sns = new AWS.SNS()
var sns = new AWS.SNS();

function sendSMS(to_number, message, cb) {

    sns.publish({
        Message: message,
        Subject: 'Admin',
        PhoneNumber:to_number
    }, cb);
  
  }
  
  // Example
  const PhoneNumberArray = ['any mobile number']
  PhoneNumberArray.forEach(number => {
    sendSMS(number, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", (err, result)=>{
        console.log("RESULTS: ",err,result)
      })
  })
 

node.js Solutions


Solution 1 - node.js

By default, the SDK detects AWS credentials set in your environment and uses them to sign requests to AWS. That way you don’t need to manage credentials in your applications.

Unix:

$ export AWS_ACCESS_KEY_ID="your_key_id"
$ export AWS_SECRET_ACCESS_KEY="your_secret_key"

Windows:

> set AWS_ACCESS_KEY_ID="your_key_id"
> set AWS_SECRET_ACCESS_KEY="your_secret_key"

you can also add $ export AWS_SESSION_TOKEN='your_token' (optional)

See aws-sdk for more details.

or create a ~/.aws/credentials file and add:

[default]
aws_access_key_id = <YOUR_ACCESS_KEY_ID>
aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>

See aws for more details.

Solution 2 - node.js

I noticed that you are setting your accessKeyId and secretAccessKey to the same environment variable.

const SESConfig = {
    apiVersion: "2010-12-01",
    accessKeyId: process.env.AWS_SECRET_KEY,      // should be:  process.env.AWS_ACCESS_ID
    secretAccessKey: process.env.AWS_SECRET_KEY,  
    region: "us-east-1"
}

These are supplied as separate values by aws and should be represented by two separate environment variables. Maybe this is your issue?

Solution 3 - node.js

You can try create an AWS_PROFILE with the credentials if you have the AWS CLI installed.

$ aws configure --profile testuser
  AWS Access Key ID [None]: 1234
  AWS Secret Access Key [None]: 1234
  Default region name [None]: us-east-1
  Default output format [None]: text

After that you can set the AWS_PROFILE as environment variable.

Linux / Mac

export AWS_PROFILE=testuser

Windows

setx AWS_PROFILE testuser

After that you should be able to run your program and AWS will get the credentials from your profile. This way you don't have to save your credentials in .ENV. If you do, remember to add it in .gitignore.

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

Solution 4 - node.js

Install dotenv

npm install dotenv --save

Create a .env file and add your Variables

AWS_ACCESS_KEY=1234567890
AWS_SECRET_KEY=XXXXXXXXXXXXXXXXXXX

Load dotenv in your project

require('dotenv').config();

Complete code

require('dotenv').config();
const AWS = require('aws-sdk');
const SESConfig = {
    apiVersion: "2010-12-01",
    accessKeyId: process.env.AWS_ACCESS_KEY,
    accessSecretKey: process.env.AWS_SECRET_KEY,
    region: "us-east-1"
}
AWS.config.update(SESConfig);
var sns = new AWS.SNS();

function sendSMS(to_number, message, cb) {

    sns.publish({
        Message: message,
        Subject: 'Admin',
        PhoneNumber:to_number
    }, cb);

  }
  
  const PhoneNumberArray = ['any mobile number']
  PhoneNumberArray.forEach(number => {
    sendSMS(number, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", (err, result)=>{
        console.log("RESULTS: ",err,result)
      })
  })

Solution 5 - node.js

worked after i followed the exact names from aws guide for the env vars

https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-node-credentials-environment.html

AWS_ACCESS_KEY_ID  
AWS_SECRET_ACCESS_KEY  
AWS_SESSION_TOKEN (Optional)  

Solution 6 - node.js

I was able to fix this problem by specifying an apiVersion

AWS.config.update({
  region: 'MY_REGION',
  apiVersion: 'latest',
  credentials: {
    accessKeyId: 'MY_ACCESS_KEY',
    secretAccessKey: 'MY_SECRET_KEY'
  }
})

Solution 7 - node.js

Note that the variable names in ~/.aws/credentials are case sensitive. That was what caused my problem

Solution 8 - node.js

You can simply load the credentials through a dedicated config.json file.

{
    "accessKeyId": "<YOUR_ACCESS_KEY_ID>", 
    "secretAccessKey": "<YOUR_SECRET_ACCESS_KEY>", 
    "region": "eu-west-3"
}

Then use the AWS load command

AWS.config.loadFromPath('./config.json');

In this case you wouldn't need to update the AWS config AWS.config.update(...); as it is done right from the gecko.

Note that: > Loading credentials from a JSON document is not supported in browser scripts.

Solution 9 - node.js

I came across a similar problem, so I watched a few videos and read a bunch of documentation, In dotenv file try creating the IAM user that you wish to give permission to access the accountAWS_PROFILE="exampleProfile" this should be the same user that you got your Access key and secret from, then require so it should look something like this.

const SESConfig = {
   apiVersion: "2010-12-01",
   profile:process.env.AWS_PROFILE,
   accessKeyId: process.env.AWS_ACCESS_KEY,
   accessSecretKey: process.env.AWS_SECRET_KEY,
   region: "us-east-1"
}

Solution 10 - node.js

I have stored all the credentials in my config file itself. For windows, I got it solved by adding a Environment Variable to my nodejs application in .env.local

AWS_SDK_LOAD_CONFIG=1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsingh singhView Question on Stackoverflow
Solution 1 - node.jsGwenMView Answer on Stackoverflow
Solution 2 - node.jsGatesKennedyView Answer on Stackoverflow
Solution 3 - node.jsJohn GhergaView Answer on Stackoverflow
Solution 4 - node.jsdiegofcornejoView Answer on Stackoverflow
Solution 5 - node.jswly185View Answer on Stackoverflow
Solution 6 - node.jsjeninjaView Answer on Stackoverflow
Solution 7 - node.jsNeal HeneghanView Answer on Stackoverflow
Solution 8 - node.jsamarinediaryView Answer on Stackoverflow
Solution 9 - node.jskidMpukaneView Answer on Stackoverflow
Solution 10 - node.jsAbinashView Answer on Stackoverflow