Last active
June 17, 2021 22:24
-
-
Save uhop/e89b2ce0ceda1f1592861b22f344a5be to your computer and use it in GitHub Desktop.
Script to set up AWS environment according to its profile.
This file contains hidden or 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
'use strict'; | |
const AWS = require('aws-sdk'); | |
let profile = process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV ? '' : 'default'; | |
const profileIndex = process.argv.indexOf('--profile'); | |
if (profileIndex > 0 && profileIndex + 1 < process.argv.length) { | |
profile = process.argv[profileIndex + 1]; | |
} | |
if (profile) { | |
const iniLoader = new AWS.IniLoader(); | |
try { | |
const config = iniLoader.loadFrom({isConfig: true}); | |
if (config[profile]) { | |
AWS.config.update(config[profile]); | |
} else if (config.default && profile !== 'default') { | |
console.log(`Warning: there is no configuration corresponding to profile "${profile}" --- trying the default...`); | |
AWS.config.update(config.default); | |
} else { | |
console.log('Warning: there is no default configuration --- ignoring...'); | |
} | |
} catch (e) { | |
console.log('Warning: there is no default config file --- ignoring...'); | |
} | |
let tryDefault = profile === 'default'; | |
if (!tryDefault) { | |
try { | |
AWS.config.credentials = new AWS.SharedIniFileCredentials({profile}); | |
} catch (e) { | |
console.log(`Warning: there are no credentials corresponding to profile "${profile}" --- trying the default...`); | |
tryDefault = true; | |
} | |
} | |
if (tryDefault) { | |
try { | |
AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: 'default'}); | |
} catch (e) { | |
console.log('Warning: there are no default credentials --- ignoring...'); | |
tryDefault = true; | |
} | |
} | |
} | |
module.exports = AWS; |
This file contains hidden or 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
'use strict'; | |
const getOptions = async (AWS, {profile, warn} = {}) => { | |
profile = profile || process.env.AWS_PROFILE; | |
if (!profile) return {}; | |
let region = '', | |
credentials; | |
const iniLoader = new AWS.IniLoader(); | |
try { | |
const config = iniLoader.loadFrom({isConfig: true}); | |
if (config[profile]) { | |
region = config[profile].region; | |
} else if (config.default && profile !== 'default') { | |
warn && console.log(`Warning: there is no configuration corresponding to profile "${profile}" --- trying the default...`); | |
region = config.default.region; | |
} else { | |
warn && console.log('Warning: there is no default configuration --- ignoring...'); | |
} | |
} catch (e) { | |
warn && console.log('Warning: there is no default config file --- ignoring...'); | |
} | |
const providers = [ | |
() => new AWS.EnvironmentCredentials('AWS'), | |
() => new AWS.EnvironmentCredentials('AMAZON'), | |
() => new AWS.SharedIniFileCredentials({profile}) | |
]; | |
if (profile !== 'default') providers.push(() => new AWS.SharedIniFileCredentials({profile: 'default'})); | |
if (AWS.ECSCredentials) providers.push(() => new AWS.ECSCredentials()); | |
providers.push([ | |
() => new AWS.RemoteCredentials(), | |
() => new AWS.ProcessCredentials(), | |
() => new AWS.TokenFileWebIdentityCredentials(), | |
() => new AWS.EC2MetadataCredentials() | |
]); | |
const chain = new AWS.CredentialProviderChain(providers); | |
try { | |
credentials = await chain.resolvePromise(); | |
} catch (e) { | |
warn && console.log(`Warning: no credentials corresponding to profile "${profile}" were retrieved.`); | |
} | |
const options = {}; | |
if (region) options.region = region; | |
if (credentials) options.credentials = credentials; | |
return options; | |
}; | |
module.exports = getOptions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script looks for
--profile XXX
on the command line (optionally) and sets up the AWS object accordingly. In case of errors, it tries to load thedefault
profile.This file is meant to be used with command-line utilities and uses standard AWS configuration files:
credentials
andconfig
.