Skip to content

Instantly share code, notes, and snippets.

@jeshan
Last active April 28, 2022 20:01
Show Gist options
  • Save jeshan/178dfa811df0f652b30d3cc61058512d to your computer and use it in GitHub Desktop.
Save jeshan/178dfa811df0f652b30d3cc61058512d to your computer and use it in GitHub Desktop.
How to select AWS profiles per account in AWS CDK
const { CredentialProviderChain } = require('aws-sdk');
const AWS = require('aws-sdk');
const accountProvider = require('./account-provider');
let getEnv = function(accountId) {
// TODO: insert logic to get your desired profile name
return profileName;
};
let getProvider = async (accountId, mode) => {
let { profile } = getEnv(accountId);
let chain = new CredentialProviderChain([
new AWS.SharedIniFileCredentials({
profile,
}),
]);
let credentials = await chain.resolvePromise();
return Promise.resolve({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken,
});
};
module.exports = {
version: '1',
init: host => {
console.log('Init loading cdk profile plugin', host);
host.registerCredentialProviderSource({
name: 'cdk-profile-plugin',
canProvideCredentials(accountId) {
canProvide = true; // TODO: your logic to determine whether should use the code in this file or not (optional)
return Promise.resolve(canProvide);
},
getProvider,
isAvailable() {
return Promise.resolve(true);
},
});
},
};
{
"app": "node bin/app.js",
"plugin": ["/full/path/to/cdk-profile-plugin.js"]
}
@Chewbee
Copy link

Chewbee commented Mar 31, 2020

Is it still working ?
To me it fails on the first line trying to do the import, i remember using it in an other project at christmas and it was working

@jeshan
Copy link
Author

jeshan commented Mar 31, 2020

I don't remember what happended in the meantime but I have updated the gist (minus my business logic).
Thanks for letting me know.

@Chewbee
Copy link

Chewbee commented Apr 5, 2020

I don't remember what happended in the meantime but I have updated the gist (minus my business logic).
Thanks for letting me know.

You are welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment