Cant set AWS credentials in nodejs

27,709

Solution 1

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
});

Solution 2

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"
       }
   }

Solution 3

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
Share:
27,709
Jessi Ann George
Author by

Jessi Ann George

Updated on July 09, 2022

Comments

  • Jessi Ann George
    Jessi Ann George almost 2 years

    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?