Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 491
Jessi Ann George Avatar asked May 08 '16 05:05

Jessi Ann George


People also ask

How do I add AWS credentials?

Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users. Choose the name of the user whose access keys you want to create, and then choose the Security credentials tab. In the Access keys section, choose Create access key.


2 Answers

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
});
like image 137
Jim P. Avatar answered Oct 13 '22 01:10

Jim P.


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"
       }
   }
like image 35
BigMan73 Avatar answered Oct 13 '22 01:10

BigMan73