5
votes

My colleague successfully perform the following code showing the table without setting the credentials in local environment, but when i conduct it it shows such error on my local machine. Please help.

Please find my codes below:

Controller:

    AWS.config.update({
        region: 'localhost',
        endpoint: new AWS.Endpoint('http://localhost:8008'),
    })
    var params = {
        TableName: 'history',
        KeySchema: [{
            AttributeName: 'b_id',
            KeyType: 'HASH'
        },{
            AttributeName: 'e_id',
            KeyType: 'RANGE'
        }],
        AttributeDefinitions: [{
            AttributeName: 'b_id',
            AttributeType: 'S',
        },{
            AttributeName: 'e_id',
            AttributeType: 'N',
        }],
        ProvisionedThroughput: { // required provisioned throughput for the table
            ReadCapacityUnits: 1, 
            WriteCapacityUnits: 1, 
        },
    }
    response.implicitEnd = false
    AWS.query('history',params, function(err, data){
        if(err) console.log(err)
        else  response.json(data)
    })

Error Message:

{ Error: connect EHOSTUNREACH 169.254.169.254:80 - Local () at Object._errnoException (util.js:1022:11) at _exceptionWithHostPort (util.js:1044:20) at internalConnect (net.js:971:16) at net.js:1065:9 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickDomainCallback (internal/process/next_tick.js:218:9) message: 'Missing credentials in config', code: 'CredentialsError', errno: 'EHOSTUNREACH', syscall: 'connect', address: '169.254.169.254', port: 80, time: 2018-04-30T04:34:53.218Z, originalError: { message: 'Could not load credentials from any providers', code: 'CredentialsError', errno: 'EHOSTUNREACH', syscall: 'connect', address: '169.254.169.254', port: 80, time: 2018-04-30T04:34:53.218Z, originalError: { code: 'EHOSTUNREACH', errno: 'EHOSTUNREACH', syscall: 'connect', address: '169.254.169.254', port: 80, message: 'connect EHOSTUNREACH 169.254.169.254:80 - Local ()' } } }

> which python 
/usr/bin/python

> pip install --upgrade pip
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages (10.0.1)
metplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.

> pip --version
pip 10.0.1 from /Library/Python/2.7/site-packages/pip (python 2.7)

> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

> python --version
Python 2.7.10
3
Error: connect EHOSTUNREACH 169.254.169.254:80 is a failure to connect to the EC2 instance metadata service, which is expected since you are not running this code inside EC2. The most likely explanation is that your colleague does indeed have credentials configured in their local environment.Michael - sqlbot
Thanks for your input @Michael-sqlbot, however how do i configure my local environment? is it like what Siu's answer and comments stated below? I failed to execute the last step because I couldn't find my .bash_profile, even after i touch it it still doesnt work (sorry for my lack of bash knowledge)warmjaijai
it may also be just .profile. If you have sudo access, you can sudo pip install awscli and you don't have to redefine the path.Michael - sqlbot
ya i think i installed it but it doesn't work still, error message remains: awc: command not foundwarmjaijai
Also there isn't any .profile eitherwarmjaijai

3 Answers

16
votes

This is what's working for me

AWS.config.update({
  region: 'us-east-1',
  accessKeyId: 'xxxx',
  secretAccessKey: 'xxxx',
  endpoint: 'http://localhost:8000'
});

const dynamodb = new AWS.DynamoDB()
const dynamoDbClient = new AWS.DynamoDB.DocumentClient();

The accessKeyId and secretAccessKey does not need to be real values.

3
votes

Once configured AWS credentials are stored in a local configuration file. For windows the file can be found at

%UserProfile%\.aws\credentials

For linux the file can be found at

~/.aws/credentials

You need to have this file available. Install AWS CLI. Then to configure this file open a command line on your local machine and enter

aws configure

Details on the CLI setup are here. Details of the credentials file are here.

1
votes

Though Stu informs about the configuration, just to hope people encounter the same as what I did trying to put it to work on localhost. after typing awscli aws configure, as it asked you for the access ID and secret access ID, you have to at least type in something to make it work (cannot leave it blank) so that it will create config files at ~/.aws directory, where in my case I simply just typed in a for both entry, then region:localhost and output:json though i think region is really just up to you. Please help to refine my answer, at least this works out for me allowing me to continue using DynamoDB on local machine