Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"]
}
@jeshan

This comment has been minimized.

Copy link
Owner Author

@jeshan jeshan commented Sep 6, 2019

@Chewbee

This comment has been minimized.

Copy link

@Chewbee 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

This comment has been minimized.

Copy link
Owner Author

@jeshan 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

This comment has been minimized.

Copy link

@Chewbee 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
You can’t perform that action at this time.