aws / aws-sdk-js

AWS SDK for JavaScript in the browser and Node.js
https://aws.amazon.com/developer/language/javascript/
Apache License 2.0
7.59k stars 1.55k forks source link

Initiate dynamodb error #1295

Closed howardya closed 7 years ago

howardya commented 7 years ago

Hello, I just started on DynamoDB and I do the following

import { DynamoDB, Config } from 'aws-sdk';

Config.region = 'my region';
Config.credentials = new CognitoIdentityCredentials({
  IdentityPoolId: appConfig.IdentityPoolId
});
const dynamoDB = new DynamoDB(Config);

I got Error: Missing credentials in config

How do I pass in my credentials? I have already successfully log into Amazon Cognito and has cognitoUser object.

howardya commented 7 years ago

After referring to this I have modified it to be the following

let Logins = {};
Logins[`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`] = 'xxx';
const myCredentials = new CognitoIdentityCredentials({
  IdentityPoolId: appConfig.IdentityPoolId,
  region: appConfig.region,
  Logins: Logins
});

const dynamodb = new DynamoDB({
  IdentityPoolId: appConfig.IdentityPoolId,
  region: appConfig.region,
  credentials: myCredentials
});

I still have Error: Missing credentials in config

Note: I only import DynamoDB and not the whole AWS using webpack so as to slim down my js file.

Any advice and help? Thank you 👍

chrisradek commented 7 years ago

@howardya Sorry for the late response.

The error object will include an originalError field that could give more details. Did your error happen to contain an originalError that eventually ended with Missing region in config?

First, to workaround the error you're seeing, you can do something like this before instantiating your CognitoIdentityCredentials:

var AWS = require('aws-sdk/global');
AWS.config.update({
  region: <REGION>
});

Behind the scenes, CognitoIdentityCredentials calls the CognitoIdentity service. That service client needs to know what region to hit. By setting the AWS.config.region to the region your IdentityUserPoolId is in, CognitoIdentityCredentials should work.

This is not the best workflow, so what we should do is also allow a way to pass a user-provided region to the CognitoIdentityCredentials provider as well.

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.