Skip to content

Instantly share code, notes, and snippets.

@d8aninja
Forked from jeshan/cdk-profile-plugin.js
Created April 28, 2022 20:01
Show Gist options
  • Save d8aninja/6b0fd2191027a02bd4772d60705f4035 to your computer and use it in GitHub Desktop.
Save d8aninja/6b0fd2191027a02bd4772d60705f4035 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"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment