SDK configuration data
It's not an issue but I've been trying to get the information out by going through code files but I believe I need a little help.
In the following code configuration data is passed from application to the SDK and then SDK shares this data with the rest of the modules like S3 etc.
var AWS = require("aws-sdk");
AWS.config.credentials = new AWS.Credentials(accessKeyId, secretAccessKey);
I believe s3 is accessing the same configuration data under the hood while performing it's operations.
var s3 = new AWS.S3();
s3.listBuckets(function(error, data){
});
Question is how S3 is able to read the accessKeyId and secretAccessKey or let's say any other module who needs this info, is able to read it?
To get the accessKeyId and secretAccessKey, you must go IAM Management Console > User > Create A New User. Once you create the user you will get these info.
After you would need to give to this new user some permissions, for example give access to S3 or dynamoDB. To do so click on the user then go to Permissions Tab then Attach Policies. Then you should be fine. @casamm
sorry I think my question wasn't clear, I'm actually going through the SDK code and trying to understand the working behind it.
How the AWS is storing the configuration data that even actors like S3 which are not instantiated yet are able to access those values later after the instantiation.
Can some engineer/developer of the SDK help me out on this?
Hi @casamm
When an S3 (or any AWS service) client is instantiated, it sets the property credentials on its own config object to reference AWS.config.credentials. Note that its a reference, so if you modify the existing AWS.config.credentials, then all service clients referencing that object will receive the same modifications, but if you set AWS.config.credentials to a new object, only new service clients instantiated from that point forward will receive those changes. So for example:
AWS.config.credentials = {accessKeyId: 'someId1'};
s3 = new AWS.S3();
console.log(s3.config.credentials.accessKeyId); // 'someId1'
AWS.config.credentials.accessKeyId = 'someId2';
console.log(s3.config.credentials.accessKeyId); // 'someId2'
AWS.config.credentials = {accessKeyId: 'someId3'};
console.log(s3.config.credentials.accessKeyId); // 'someId2'
Let me know if that answers your question, or feel free to provide more clarification if it doesn't.
@LiuJoyceC we are getting closer, let me narrow AWS files really down as per my understanding and it may not match the actual code but I need help with broader structure to figure out how the S3 and other actors are accessing configuration data.
app.js
var AWS = require("./aws");
var aws = new AWS("key", "secret");
var s3 = new AWS.S3();
s3.listBuckets();
aws.js
var AWS = function(accessKeyId, secretAccessKey){
AWS.config.credentials.accessKeyId = accessKeyId;
AWS.config.credentials.secretAccessKey = secretAccessKey;
};
AWS.config = {
credentials: {
accessKeyId: null,
secretAccessKey: null
}
};
AWS.S3 = require("./s3");
module.exports = AWS;
s3.js
var S3 = function(){};
S3.prototype.listBuckets = function(){};
module.exports = S3;
//console.log(AWS.config); //how S3 is reaching out to configuration data
To follow your example of the scoped-down version of AWS, it's more similar to:
app.js
var AWS = require("./aws");
require("./s3");
AWS("key", "secret");
var s3 = new AWS.S3();
s3.listBuckets();
aws.js
var AWS = function(accessKeyId, secretAccessKey){
AWS.config.credentials.accessKeyId = accessKeyId;
AWS.config.credentials.secretAccessKey = secretAccessKey;
};
AWS.config = {
credentials: {
accessKeyId: null,
secretAccessKey: null
}
};
module.exports = AWS;
s3.js
var AWS = require("./aws");
var S3 = function(){
this.config = {};
this.config.credentials = AWS.config.credentials;
};
S3.prototype.listBuckets = function(){
var credentials = this.config.credentials;
// make request using these credentials
};
// S3 will now be available on the AWS object in any file that requires it
// as long as this s3 file is required before AWS.S3 is called
AWS.S3 = S3;
The actual code doesn't look like this, but this is essentially what it's doing in this scoped-down example. To see the actual code that's implementing this, take a look at lib/services.js (at the constructor at and at initialize) and lib/config.js (at the constructor and extractCredentials).
Slightly modified since didn't want to have require("./s3"); in the app.js. Please suggest in case you see something wrong with it. Thanks for your awesome help.
app.js
var AWS = require("./aws");
AWS("key", "secret");
var s3 = new AWS.S3();
s3.listBuckets();
aws.js
var AWS = function(accessKeyId, secretAccessKey){
AWS.config.credentials.accessKeyId = accessKeyId;
AWS.config.credentials.secretAccessKey = secretAccessKey;
};
AWS.config = {
credentials: {accessKeyId: null, secretAccessKey: null}
};
module.exports = AWS;
require("./s3"); //moved from app.js
s3.js
var AWS = require("./aws");
var S3 = function(){
this.config = {};
this.config.credentials = AWS.config.credentials;
};
S3.prototype.listBuckets = function(){
var credentials = this.config.credentials;
// make request using these credentials
};
AWS.S3 = S3;
For the purpose of explaining how the S3 client has access to AWS.config.credentials, either of these work. The real implementation is more complicated and doesn't actually require the service until the client is instantiated for the first time.
Let me know if this answers your question. Thanks!
Thanks LiuJoyceC. This clarifies, I'll ask you again in case. Great.
Hi LiuJoyceC,
Wondering how multi-tenancy will work independently of two instances of require("./aws");
It seems to me that I've to update credentials each time I need to switch to another environment under a different account as follows.
AWS("key", "secret"); // account A
s3.putObject();
// wait until pubObject completes
AWS("key2", "secret2"); // account B
s3.putObject();
Is there a way to have two distinct objects in an efficient way, and use both of them concurrently to upload to two different accounts. Even If custom changes are required to SDK under the light of our minimalistic example above then please advise.
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.