5

I am running a NodeJS application that uses the aws-sdk library. I have the following environment variables exported:

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXX

I can verify they are set correctly by running the env command as well as by running console.log(process.env) in my Node code.

When running my application I get the following error:

Error: ENOENT: no such file or directory, open '/root/.aws/credentials'
    at Object.fs.openSync (fs.js:577:3)
    at Object.fs.readFileSync (fs.js:483:33)
    at Object.readFileSync (/app/node_modules/aws-sdk/lib/util.js:97:26)
    at SharedIniFile.loadFile [as ensureFileLoaded] (/app/node_modules/aws-sdk/lib/shared_ini.js:19:18)
    at SharedIniFile.loadProfile [as getProfile] (/app/node_modules/aws-sdk/lib/shared_ini.js:52:10)
    at Config.region (/app/node_modules/aws-sdk/lib/node_loader.js:88:34)
    at Config.set (/app/node_modules/aws-sdk/lib/config.js:448:39)
    at Config.<anonymous> (/app/node_modules/aws-sdk/lib/config.js:283:12)
    at Config.each (/app/node_modules/aws-sdk/lib/util.js:485:32)
    at new Config (/app/node_modules/aws-sdk/lib/config.js:282:19)
    at Object.<anonymous> (/app/node_modules/aws-sdk/lib/node_loader.js:99:14)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1

Obviously the exact problem is obvious I do not have a /root/.aws/credentials file. However from reading, specifically here it seems that the sdk should automatically detect my environment variables and not need to have a credentials file.

My question is how can I get the aws-sdk to use the credentials in my environment variables without erroring about not having an credentials file?

Jack Gore
  • 3,874
  • 1
  • 23
  • 32
  • Looks like whatever is looking up the file isn't handling the 'no-such-file' exception. What happens if you place an empty file at '/root/.aws/credentials'? – Himal Jul 09 '18 at 21:18
  • What does the code like that interacts with the credentials? – stdunbar Jul 09 '18 at 22:08
  • Did you manage to get to the bottom of this. I've got exactly the same issue. Running locally, it throws this error even though env's are set and confirmed with console.log(process.env). Deploying witch docker works fine. Ubuntu 18, node v8.0. – cbarlow123 Nov 02 '18 at 11:47
  • Did you run aws service in docker container? – Aleksandrs Jun 09 '21 at 06:50

7 Answers7

4

I believe you get this issue if you access a feature that requires a region (which is most).

You need to export AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION. You are missing AWS_REGION.

Note: if that doesn't work can you provide a stack trace with the AWS SDK version.

Luke
  • 2,851
  • 1
  • 19
  • 17
2

Potential cause: ensure AWS_SDK_LOAD_CONFIG is not set

From a previous project, I had AWS_SDK_LOAD_CONFIG=1 hanging out in my environment. This kept forcing the AWS SDK to look for a credentials file. I removed this from my bash_profile and the SDK was finally able use the env variables for my key/secret.

Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • This solved my problem, thanks. For some reason, setting `AWS_SDK_LOAD_CONFIG=0` didn't work. I had to conditionally set it inside the `if` statement. – Pawel May 05 '22 at 12:03
1

I had the same problem. It cost me quite some head ache because I had this running in AWS Fargate and debugging is not that easy there.

The error means the Javascript SDK can not find the AWS credentials. Here you can see in what order the SDK tries to load the credentials from: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html

My error was quite embarrassing, I just had a typo in my environment variables. My variable was AWS_ACCESSS_KEY_ID instead of AWS_ACCESS_KEY_ID. (Quite hard to see the difference, right?)

So probably double check the names of your environment variables (or if you use a config file, all the lines in the config file)

Anton
  • 936
  • 1
  • 8
  • 27
0

AWS-SDK automatically get's the .aws/credential file's credentials.

  • in Windows it is inside C:\Users\username\.aws\credentials
  • in Linux most probably it will be in the ~/.aws/credentials location. Plus you should annotate it as below,

[default] AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXX

SamPiy93
  • 490
  • 3
  • 13
0

Did you export the env variables in your shell profile file? If you are able to print process.env.AWS_ACCESS_KEY_ID in your node code, then your node is able to read env file. The order of precedence for aws sdk to get creds is env variables first and then credentials file. So, it is very likely that your env variables aren't rightly exported (or readable for your node code). Note that you have to restart the terminal console for env var changes to take into effect. Hope this helps.

juve stig
  • 21
  • 3
0

currently aws provides its own method to dynamically load aws credentials from external file, no need to use any library like 'dotenv'. just create creds.json file in your project folder, inside this file you can add any aws credentials like, fyi region is hardcoded

creds.json

{
 "region": "us-east-1", // region also you can change
 "accessKeyId": "YOUR_ACCESS_KEY",
 "secretAccessKey": "YOUR_SECRET_KEY"
}

load this file in your index.js or app.js using following code snipet

index.js

var AWS = require("aws-sdk");
AWS.config.loadFromPath('./creds.json');

var ec2 = new AWS.EC2(); //just create whatever instance you like from aws-sdk

FYI my project folder is like

demo/
   index.js
   config.json
   package.json
   ...

>> node index.js

works like charm.. if you face file not found error /.aws/credentials just create expected empty file in correct path

Vivek Chaudhari
  • 1,930
  • 1
  • 14
  • 20
0

One possible way to create .env file in the root folder of the project:

AWS_ACCESS_KEY_ID=aws_access_key
AWS_SECRET_ACCESS_KEY=aws_secret_key

Then simply add this line of code require("dotenv").config(); to the node.js file. Full example:

require("dotenv").config(); // this line will use environment variables from local .env file of node.js app
import AWS from "aws-sdk";
import { AWSRegions } from "./types/aws";

AWS.config.update({ region: AWSRegions.EU_NORTH_1 });

const { DynamoDB } = AWS;
const dynamodb = new DynamoDB();
Alex
  • 1