0

This is what my code looks like:

'use strict'; process.env.AWS_PROFILE // Load the AWS SDK for Node.js const AWS = require('aws-sdk'); // Create EC2 service object var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); // Load credentials and set region from JSON file AWS.config.loadFromPath('/Users/testuser/.aws/credentials'); // Load in security group parameters const securityParams = require('./securityParams.json'); module.exports = { //Exports creation of Security Groups CreateSecurityGroup: (req, res) => { ec2.createSecurityGroup(securityParams, function(err, data) { if (err) { return (console.log("Error", err)); } // Pass the Json as a parameter in this function ec2.authorizeSecurityGroupIngress(securityParams, function(err, data) { if (err) { res.serverError(err, err.stack); } else { res.ok(data); console.log('Ingress Security Rules Created'); } }) // Pass the Json as a parameter in this function ec2.authorizeSecurityGroupEgress(securityParams, function(err, data) { if (err) { res.serverError(err, err.stack); } else { res.ok(data); console.log('Egress Security Rules Created'); } }) }) } } 

I'm trying to have the script load configurations from two files; one aws credentials file, and one json. However its throwing errors on the credentials file which looks like this:

[default] aws_access_key_id=************** aws_secret_access_key************** 

I'm not sure what I'm missing to get it to read the properties in correctly.

Here is the error I'm seeing:

undefined:1 [default] ^ SyntaxError: Unexpected token d in JSON at position 1 at JSON.parse (<anonymous>) 
4
  • "However its throwing errors on the credentials file" Two things: 1. What errors? 2. That's not JSON, but your title is "NodeJS Failing to load in JSON" (presumably securityParams.json contains JSON -- which you're treating as though it were JavaScript code via require -- but not the credentials). Commented Apr 10, 2017 at 17:08
  • Updated my question, so it sounds like I can't require the file the way I am trying to? Commented Apr 10, 2017 at 17:14
  • 1
    Looks like loadFromPath is trying to parse a JSON file, however the credentials file is not JSON. Here are two resources about setting credentials when using the AWS SDK. I personally prefer using environment variables. docs.aws.amazon.com/cli/latest/userguide/… docs.aws.amazon.com/sdk-for-java/v1/developer-guide/… Commented Apr 10, 2017 at 17:16
  • One more question, in order to actually read in the JSON do I have to add in a JSON parse into my code? If so, where? Commented Apr 11, 2017 at 0:47

1 Answer 1

1

credentials is a plain Ascii file, it's not json file

// Load credentials and set region from JSON file AWS.config.loadFromPath('/Users/testuser/.aws/credentials'); 

You can check file type with command file /Users/testuser/.aws/credentials

sample snippet to read props file and set AWS config

var PropertiesReader = require('properties-reader'); var AWS = require('aws-sdk') var properties = PropertiesReader('/Users/username/.aws/credentials'); AWS.config.update({ accessKeyId : properties.get('aws_access_key_id'), secretAccessKey : properties.get('aws_secret_access_key'), region : 'us-west-2' }) console.log(AWS.config) 

Ref:https://www.npmjs.com/package/properties-reader

Sign up to request clarification or add additional context in comments.

9 Comments

Yes I recognize that now. Any idea how I can read those parameters into the script I'm writing, without changing the file as it is.
Yes, you have to use properties-reader package and update config accordingly.
Got it thats helpful. One more thing. Right now none of my functions in module exports are executing, basically nothing is being returned. Is there anything I need to do to get them to run?
No errors at all. Have tried two things, AWS_PROFILE=aws-admin node AWS_Security_Group.js
Yes it exports, but req,res objects are related to api calls so you have to call this function in that api. So if you want run this file independently, just remove references to all req,res and call module.exports.CreateSecurityGroup()
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.