Thursday, 8 August 2013

Loading credentials JSON with AWS SDK Results in Error

Loading credentials JSON with AWS SDK Results in Error

I'm trying to load credentials for AWS with loadFromPath and getting an
unexpected error. Hardcoding the same credentials with AWS.config.update
works fine. To make sure the path and format of credentials file is
correct I loaded the same with fs.readFile and it loads correctly, so
there don't seem to be any path / permissions issues. This seems super
basic but I've been pulling my hair out trying to resolve. Thanks for your
help.
The error / output:
Here: /home/ec2-user/.ec2/credentials.json
Got this through readFile: { access_id: 'XXXXXXX',
private_key: 'XXXXXXX',
keypair: 'praneethkey',
'key-pair-file': '/home/ec2-user/.ec2/praneethkey.pem',
region: 'us-west-2' }
/home/ec2-user/node_modules/aws-sdk/lib/config.js:221
if (err) throw err;
^
SyntaxError: Unexpected token <
at Object.parse (native)
at /home/ec2-user/node_modules/aws-sdk/lib/metadata_service.js:100:38
at IncomingMessage.<anonymous>
(/home/ec2-user/node_modules/aws-sdk/lib/metadata_service.js:75:43)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
The code:
'use strict';
var AWS = require('aws-sdk');
var fs = require('fs');
var pathv = process.env.HOME + '/.ec2/credentials.json';
AWS.config.loadFromPath(pathv);
console.log('Here: ' + pathv);
fs.readFile(pathv, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
console.log("Got this through readFile:",data);

No comments:

Post a Comment