This project is not maintained. We recommend using https://direnv.net/ which gives you broader capabilities.
Zdenek Farana <zdenek.farana@aproint.com> :toc: macro
Trouble remembering the right AWS profile or region in specific projects?
.awsenv comes to the rescue.
Every time you switch to a directory with .awsenv file, its AWS environment variables load automatically.
Profit.
When you work with multiple AWS accounts, you’ll find yourself juggling with various AWS profiles.
You’d use AWS_PROFILE environment variable to switch the profile or you’d use --profile argument in AWS CLI commands.
It’s super easy to forget to switch the profile.
The common case is you’re using a single profile in particular directories. Having a file which defines the profile and other AWS environment variables together with a mechanism how to auto-load them gives you a better control over the access to AWS.
The file itself is pretty simple:
# You can use comments
AWS_PROFILE=myprofileWe support the variables defined in the official AWS documentation Environment Variables To Configure the AWS CLI.
.awsenv is almost POSIX-compliant and it should work in the most of the major contemporary shells.
It was tested in zsh and bash.
-
Clone this repository into
~/.oh-my-zsh/custom/pluginsdirectory:git clone https://github.com/aproint/awsenv.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/awsenv -
Activate the plugin in
~/.zshrc:plugins=( [plugins...] awsenv) -
Reopen your terminal
NoteThis installation enables the hook automatically
-
Clone this repository into
~/.awsenvdirectory.git clone https://github.com/aproint/awsenv.git ${HOME}/.awsenv -
Source the script in your shell profile, i.e.
~/.bash_profile,~/.zshrc,~/.profile, or~/.bashrc:source ${HOME}/.awsenv/awsenv.sh-
Bash change directory hook
Source the hook script after the
awsenv.shscript in the shell profile:source ${HOME}/.awsenv/bash-awsenv.sh -
ZSH change directory hook
If you’re not using Oh My ZSH, you can source the hook script after the
awsenv.shscript in the shell profile manually:source ${HOME}/.awsenv/zsh-awsenv.zsh
-
-
Reopen your terminal
The script defines two commands awsenv and awsenvclr.
-
awsenv- execute in a directory with.awsenvfile to load its AWS environment variables -
awsenvclr- unsets all AWS environment variables; this is a convenience command when you feel your env variables got mixed up.
Thanks to the deeper shell integration with a hook script, it’s possible to execute awsenv on every directory change automatically and thus switch your AWS environment variables seamlessly.
See the installation details.
|
Important
|
The hooks work only in interactive shell sessions. |
-
It searches for a
.awsenvfile in the current directory; if it can’t find the file, it looks a level up in the directory hierarchy recursively until it’s found (or not). -
If
awsenvis not active, it stores the current AWS environment variables intoAWSENV_PREV_equivalents (aka history). -
It sources the
.awsenvfile into your current shell session.Importantawsenvfilters out anything butAWS_prefixed environment variables. -
When you change to a directory
-
without
.awsenvfile in its parent hierarchy, it loads your previous AWS environment variables fromAWSENV_PREV_and clears the history. -
with another
.awsenvfile, it loads the new AWS environment variables, but it doesn’t overwrite the history.
-
-
PRs are welcomed.
-
Tests written with bats.