Jessi Ann George
Jessi Ann George

Reputation: 339

Cant set AWS credentials in nodejs

I'm working on a cloud project using NodeJS. I have to run EC2 instances so have done a npm install aws-sdk.

I believe we have to add our credentials now before we run the application?

I could not aws folder so I have created a folder and added the credentials in the credentials.txt file.

C:\Users\jessig\aws

I keep getting this error:

{ [TimeoutError: Missing credentials in config]
  message: 'Missing credentials in config',
  code: 'CredentialsError',

I tried setting the Access key and secret key in environment variables but still get the same error..

Not sure why I cant find the \.aws\credentials (Windows) folder..

Can anyone please help?

Upvotes: 24

Views: 40125

Answers (5)

Abraham
Abraham

Reputation: 9885

Adding accessKeyId and secretAccessKey in the config for AWS is deprecated as of today. As the AWS Docs for SDK for Node.js states:

The SDK automatically detects AWS credentials set as variables in your environment and uses them for SDK requests. This eliminates the need to manage credentials in your application. The environment variables that you set to provide your credentials are:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_SESSION_TOKEN (Optional)

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


You may want to use dotenv package to load those environment variables.

Upvotes: 5

Agilan I
Agilan I

Reputation: 232

The AWS credentials can be set as ENVIRONMENT VAR in the running container. You would either add the following two ENVIRONMENT VAR directly:

AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

or set these ENVIRONMENT VAR programmatically within NODE as

var AWS = require('aws-sdk')
AWS.config = new AWS.Config();
process.env.AWS_ACCESS_KEY_ID = "AKIA************L55A"
process.env.AWS_SECRET_ACCESS_KEY = "Ef*******+C5LrtOroSj**********yNE"
AWS.config.region = "us-east-2"

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

Upvotes: 1

BigMan73
BigMan73

Reputation: 1584

I used the following programmatic way, combined with the popular npm config module (which allows different config files for development vs production, etc.):

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

const accessKeyId = config.get('AWS.accessKeyId');
const secretAccessKey = config.get('AWS.secretAccessKey');
const region = config.get('AWS.region');
AWS.config.update(
    {
        accessKeyId,
        secretAccessKey,
        region
    }
);

And the json config file, e.g. development.json, would look like:

   {
       "AWS": {
           "accessKeyId": "TODO",
           "secretAccessKey": "TODO",
           "region": "TODO"
       }
   }

Upvotes: 19

Jim P.
Jim P.

Reputation: 1127

As Frederick mentioned hardcoding is not an AWS recommended standard, and this is not something you would want to do in a production environment. However, for testing purpose, and learning purposes, it can be the simplest way.

Since your request was specific to AWS EC2, here is a small example that should get you started.

To get a list of all the methods available to you for Node.js reference this AWS documentation.

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

AWS.config = new AWS.Config();
AWS.config.accessKeyId = "accessKey";
AWS.config.secretAccessKey = "secretKey";
AWS.config.region = "us-east-1";

var ec2 = new AWS.EC2();

var params = {
  InstanceIds: [ /* required */
    'i-4387dgkms3',
    /* more items */
  ],
  Force: true
};
ec2.stopInstances(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

Upvotes: 25

Frederic Henri
Frederic Henri

Reputation: 53813

There are multiple ways to configure the sdk to work with node js

There are a few ways to load credentials. Here they are, in order of recommendation:

  • Loaded from IAM roles for Amazon EC2 (if running on EC2),
  • Loaded from the shared credentials file (~/.aws/credentials),
  • Loaded from environment variables,
  • Loaded from a JSON file on disk,
  • Hardcoded in your application

Although the hardcoded one is not recommended.

If you want to use a shared credentials files, on windows it would be

C:\Users\jessig\.aws\credentials

(note the . before aws). Your file should be something like

[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

Upvotes: 6

Related Questions