0

I am using aws-sdk with node.js and I am not able to establish a connection to aws ec2 to obtain instance details using the sdk.

This is the error:

Error Error: connect ENETUNREACH 
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)

And this is the piece of code I have written: I have removed the access keys for security purpose.

var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'localhost',
  user: '',
  password: '',
  database: ''
});

var app = express();
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({
  region: 'us-east-1'
});

var AWS_ACCESS_KEY_ID = '';
var AWS_SECRET_ACCESS_KEY = '';

var params = {
  DryRun: false
};

ec2 = new AWS.EC2({
  apiVersion: '2016-11-15'
});
ec2.describeInstances(params, function(err, data) {
  if (err) {
    console.log("Error", err.stack);
  } else {
    console.log("Success", JSON.stringify(data));
    res.send({
      message: data
    });
  }
});

app.listen(443);
console.log("The node server is running at port 443");

Is there a way to fix this? I am using aws-sdk for the first time. Thanks in advance.

1
  • Where are you passing the API keys? Commented Feb 12, 2018 at 19:14

4 Answers 4

1
  • Probably you're not passing the API keys.
var AWS_ACCESS_KEY_ID = '';
var AWS_SECRET_ACCESS_KEY = '';

var params = {
  DryRun: false
};

ec2 = new AWS.EC2({
  accessKeyId: AWS_ACCESS_KEY_ID,
  secretAccessKey: AWS_SECRET_ACCESS_KEY,
  apiVersion: '2016-11-15'
});

Recommendation:

  • Put your API keys in separated locations.
  • If this code will be hosted within an EC2, use Service role permissions for EC2.
  • Use profiles.
Sign up to request clarification or add additional context in comments.

3 Comments

I made those changes. But, no luck. I am getting a different error. . Error MultipleValidationErrors: There were 2 validation errors:** * UnexpectedParameter: Unexpected key 'AWS_ACCESS_KEY_ID' found in params * UnexpectedParameter: Unexpected key 'AWS_SECRET_ACCESS_KEY' found in params at ParamValidator.validate
@abhishekbv I think you didn't declare those variables. Further, can you test removing that MySQL connection?
I am able to establish a connection now. Thank you very much. If I face any other issue I shall send you an email.
0

The credentials seems to be the issue here. Please double check the credentials you are providing for connecting to EC2. The best option would be to generate a new key_id and secret_access_key and use that. I would personally recommend to download the credentials file and pass that in your code rather than putting key_id and secret_access_key here.

1 Comment

I am using valid credentials. Also, to cross verify your suggestion. I generated new keys. Even then, I am getting the same error.
0

Is Your Computer or Node.js connect to internet? You control this. first open cmd an use ping, if the ping return packet proper. Control internet of node.js. For control use requestify module for this. use this code:

const requi=require('requestify');
requi.get("https://google.com")
    .then(function(data){
        console.log(data);
    })
    .catch(function (err) {
       console.log(err);
    });

1 Comment

I am able to interact with the outer world using my Node.js server. It is an issue with AWS keys.
0

The easiest way to ensure you're doing this properly is to follow the suggested credential management described in AWS SDK For NodeJs.

npm install aws-sdk

Create a credentials file at ~/.aws/credentials on Mac/Linux or C:\Users\USERNAME.aws\credentials on Windows

[default] 
aws_access_key_id = your_access_key   
aws_secret_access_key = your_secret_key

And then have the AWS object manage the credentials for you:

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

It looks like you might be almost there; instead of storing your keys in your code, store them in the specified credentials file above, and try again.

3 Comments

I am going to implement as you have suggested. This makes the code more readable.
Is that a typo for the Windows location -- USERNAME.aws? USERNAME\.aws makes sense. It's testable, I suppose. My assumption that it's a typo sure isn't working yet; but it sure seems strange (and obnoxious) to create a folder beneath C:\Users not directly related to a Windows login.
I forgot this page gives the directory in the way I'd expect: aws.amazon.com/developers/getting-started/nodejs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.