Last active
April 28, 2022 20:01
-
-
Save jeshan/178dfa811df0f652b30d3cc61058512d to your computer and use it in GitHub Desktop.
How to select AWS profiles per account in AWS CDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, | |
}); | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"app": "node bin/app.js", | |
"plugin": ["/full/path/to/cdk-profile-plugin.js"] | |
} |
I don't remember what happended in the meantime but I have updated the gist (minus my business logic).
Thanks for letting me know.
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
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