Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created May 8, 2019 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertsosinski/54ac1ea346fd47c824bc596e95bfd17b to your computer and use it in GitHub Desktop.
Save robertsosinski/54ac1ea346fd47c824bc596e95bfd17b to your computer and use it in GitHub Desktop.
AWS SDK CLI STS Usage
let fs = require("fs");
let path = require("path");
let AWS = require("aws-sdk");
let cliPath = path.resolve(process.env.HOME, ".aws", "cli");
/*
NOTES: Make sure you alias the SHA1 fingerprinted cache token to the profile name
E.G. ln -s ~/.aws/cli/cache/09a19nczc3zvza58zrftetyfso7yw9d48u6ugsel.json profile
Then run your program with the specified profile alias as an envrionemnt variable
E.G. AWS_SDK_CLI_STS_PROFILE=profile node app.js
*/
module.exports = function() {
var sts;
try {
var cliString = fs.readFileSync(path.resolve(cliPath, process.env.AWS_SDK_CLI_STS_PROFILE), "utf8");
var cliObject = JSON.parse(cliString);
sts = new AWS.STS().credentialsFrom(cliObject);
}
catch (e) {
// use defaults...
}
return sts;
};
var AWS = require("aws-sdk");
var cliSTS = require("./aws-sdk-cli-sts");
AWS.config.credentials = cliSTS();
var s3 = new AWS.S3();
s3.listBuckets({}, function(err, data) {
if (err) console.log(err);
if (data) console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment