dimanche 4 mai 2014

Linux - couleur d'invite de commandes de l'utilisateur de changement AWS "--profil"-Stack Overflow


The new unified AWS CLI gives us the ability to pass in a "--profile" to switch between accounts. Very cool feature, but I see this getting us in a lot of trouble (if, for example a user forgets they are in PROD rather than DEV and run commands that whack stuff out).


I don't see any way to hook the "--profile" into something like a ".bashrc" script where we could do things like changing the prompt color (to like red) & adding "PRODUCTION" to the prompt.


The only way I could imagine doing this is writing a wrapper bash script named something like "gotoprod.sh" which changes prompt & sets up the "--profile". Anything more elegant in the aws cli tools themselves?




The --profile option is passed to each individual command, so technically you are not "IN" a profile until the command is running. For that reason, I don't think it's possible to do exactly what you're trying to do using just the --profile switch.


However, if you use the AWS_DEFAULT_PROFILE environment variable, it is possible.


Here's what I do in .bashrc:


function startaws {
export AWS_DEFAULT_PROFILE=$1
}

(You can name the function whatever you like, of course).
This allows you to do something like startaws dev, which both loads your "dev" profile, AND gives you the $AWS_DEFAULT_PROFILE environment variable, based on which you can now change your bash prompt.



The new unified AWS CLI gives us the ability to pass in a "--profile" to switch between accounts. Very cool feature, but I see this getting us in a lot of trouble (if, for example a user forgets they are in PROD rather than DEV and run commands that whack stuff out).


I don't see any way to hook the "--profile" into something like a ".bashrc" script where we could do things like changing the prompt color (to like red) & adding "PRODUCTION" to the prompt.


The only way I could imagine doing this is writing a wrapper bash script named something like "gotoprod.sh" which changes prompt & sets up the "--profile". Anything more elegant in the aws cli tools themselves?



The --profile option is passed to each individual command, so technically you are not "IN" a profile until the command is running. For that reason, I don't think it's possible to do exactly what you're trying to do using just the --profile switch.


However, if you use the AWS_DEFAULT_PROFILE environment variable, it is possible.


Here's what I do in .bashrc:


function startaws {
export AWS_DEFAULT_PROFILE=$1
}

(You can name the function whatever you like, of course).
This allows you to do something like startaws dev, which both loads your "dev" profile, AND gives you the $AWS_DEFAULT_PROFILE environment variable, based on which you can now change your bash prompt.


0 commentaires:

Enregistrer un commentaire