Scripts by udondan

AWS Select profile

This is a helper to switch AWS profiles, which then can be used by other scripts.

The script parses the ~/.aws/config file and let's the user select one of the configured profiles. The current active profile is marked with a star.

Preview

The selected value then will be stored as AWS_PROFILE in the env file.

To use the profile name in another script, you can get the value via:

const region = await env('AWS_PROFILE');
// Menu: AWS Select Profile
// Description: Select AWS profile from you AWS config
// Author: Daniel Schroeder
// Twitter: @udondan
import { readFileSync } from 'fs';
const ini = await npm('ini');
const awsConf = `${process.env.HOME}/.aws/config`;
const config = ini.parse(readFileSync(awsConf, 'utf-8'));
const currentMarker = ' ★';
const currentProfile = env['AWS_PROFILE'];
const sections = Object.keys(config).reduce(function (filtered, section) {
if (section.startsWith('profile ')) {
let profileName = section.replace('profile ', '');
if (profileName == currentProfile) {
profileName += currentMarker;
}
filtered.push(profileName);
}
return filtered;
}, []);
const profile = await arg(
{
placeholder: 'Select profile',
hint: `parsed from ${awsConf}`,
},
sections.sort()
);
await cli('set-env-var', 'AWS_PROFILE', profile.replace(currentMarker, ''));
Discuss Post