34

im setting up a dynamodb locally to test with my Node app. To set it up i just plain out copied the code from here and adjusted it for my needs.
This is the code:

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

var config = ({
  "apiVersion": "2012-08-10",
  "accessKeyId": "abcde",
  "secretAccessKey": "abcde",
  "region": "us-west-2",
  "endpoint": "http://localhost:8001",
});

var dynamodb = new AWS.DynamoDB(config);

var params = {
    TableName : "Movies",
    KeySchema: [       
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
        { AttributeName: "title", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

This throws an error though and i have no idea why:

Unable to create table. Error JSON: {
  "message": "Missing credentials in config",
  "code": "CredentialsError",
  "time": "2017-04-10T11:45:26.748Z",
  "retryable": true,
  "originalError": {
    "message": "Could not load credentials from any providers",
    "code": "CredentialsError",
    "time": "2017-04-10T11:45:26.747Z",
    "retryable": true,
    "originalError": {
      "message": "Connection timed out after 1000ms",
      "code": "TimeoutError",
      "time": "2017-04-10T11:45:26.747Z",
      "retryable": true
    }
  }
}

Would be thankful for any help!

Torben
  • 558
  • 2
  • 7
  • 16

9 Answers9

58

I had the same error. In my case the issue was that it worked fine locally running from the command line, but not in docker.

Basically how to pass credentials to AWS npm S3 client between aws-sdk (version 2) and @aws-sdk/s3-client (version 3), has changed. In v2 case, the structure is like that:

const credentials = {
    region: configuration.aws.region,
    accessKeyId: configuration.aws.accessKeyId,
    secretAccessKey: configuration.aws.secretAccessKey
};

In v3:

const credentials = {
  region: configuration.aws.region,
  credentials: {
    accessKeyId: configuration.aws.accessKeyId,
    secretAccessKey: configuration.aws.secretAccessKey
  }
};

It was working locally fine because I was already authenticated to AWS through CLI and it was ignoring those v2 style credentials that I passed to v3 client.

Conclusion: use typescript for everything boys & girls!

Madis Pukkonen
  • 1,005
  • 11
  • 12
15

Change to the following code, have dummy accessKeyId and secretAccessKey, then run it

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

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

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Movies",
    KeySchema: [
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
        { AttributeName: "title", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});
Jess Telford
  • 12,880
  • 8
  • 42
  • 51
Zhu Xiaohu
  • 589
  • 6
  • 8
9

As per error message your credentials is not set in config.

I am giving my view to set the credentials and then use the services.

 const aws = require('aws-sdk');
 aws.config = new aws.Config();
 aws.config.accessKeyId = "xxxxxxxxxxxxxx";
 aws.config.secretAccessKey = "xxxxxxxxxx";
 aws.config.region = "region";

Now then use the Dynamodb

var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

UPDATE

you ca also set credential with file

aws.config.loadFromPath('./AwsConfig.json'); 

Hope it works!!

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • You are using windows machine and have you installed aws-cli? – abdulbarik Apr 10 '17 at 12:18
  • any how it's setting null values that's why it's coming? is there any env variable is set for aws? I am updating my answer with add credentials with file. – abdulbarik Apr 10 '17 at 12:57
  • i've moved the credentials outside of the script and into a json file. This still gives me the error and i really don't know why. – Torben Apr 10 '17 at 13:20
  • You need to dig yourself, some how it set null, it's not using the configuration which you have given in code – abdulbarik Apr 10 '17 at 13:22
  • i dont create a dynamodb object anywhere else in the code. As i stated above this is literally the code amazon provides as a tutorial which is not working. Java runtime is up to date, the credentials json is in the same folder and i dont change the values anyhwere. I really can't figure out this problem. – Torben Apr 10 '17 at 13:26
  • what is the structure of the json file? Aws is honestly the worst documentation of all times – Felipe May 26 '17 at 03:44
  • You can refer this for example https://aws.amazon.com/sdk-for-node-js/ and https://stackoverflow.com/questions/18137780/loading-credentials-json-with-aws-sdk-results-in-error – abdulbarik May 26 '17 at 05:21
6

Apparently i figured out the problem. Using a json file to set the credentials still led to the error. Using only the config object without the flag -inMemory caused an error too. The combination of hard coding the credentials in the script and using the -inMemory flag while starting the dynamodb locally seemed to solve this. I really don't understand why though.

Torben
  • 558
  • 2
  • 7
  • 16
5

This worked for me

const sqsClient = new SQSClient({ region: REGION ,
   credentials: {
    accessKeyId: 'XXXXXXXXXXXXXXX',
    secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXX'
  }
});
Sandeep P
  • 4,291
  • 2
  • 26
  • 45
1

You probably are confusing between web service DynamoDB and local version. You can find the main differences between both in AWS documentation.

If you want to use a local version of DynamoDB, you will find the latest informations to install and run it in AWS documentation.

Once you're done, make sure to run the local DynamoDB instance by running:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -inMemory

Your error will probably be avoided.

Thomas L.
  • 1,294
  • 9
  • 13
  • i was running dynamodb locally the whole time, which is the reason i set port 8001. i hoped the inMemory flag you provided might not lead to this error but it does. Sadly your solution does not help. – Torben Apr 10 '17 at 13:14
1

DynamoDB local works fine for me on port 8000 with dummy credentials.

Note:

The port number is 8000. I use the below dummy credentials.

var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000",
    credentials: creds
});

Start command:-

java -Djava.library.path=DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

OS:-

Windows

notionquest
  • 37,595
  • 6
  • 111
  • 105
1

In node.js, you can easily load from .env value by setting these values

AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

AWS_SESSION_TOKEN (Optional)

https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-node-credentials-environment.html

Aravin
  • 6,605
  • 5
  • 42
  • 58
1

Another option/solution to this problem would be creating a credentials file at your user's home.

Something like:

~/.aws/credentials

And the content will be:

[default]
aws_access_key_id = *************************
aws_secret_access_key = *********************************************
aws_session_token =**************************************************
jonathanlima
  • 159
  • 2
  • 9