-
-
Save d8aninja/6b0fd2191027a02bd4772d60705f4035 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"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment