Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
/ awsenv Public archive

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.

License

Notifications You must be signed in to change notification settings

aproint/awsenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project is not maintained. We recommend using https://direnv.net/ which gives you broader capabilities.

.awsenv

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.

Motivation

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:

.awsenv
# You can use comments
AWS_PROFILE=myprofile

We support the variables defined in the official AWS documentation Environment Variables To Configure the AWS CLI.

Installation

.awsenv is almost POSIX-compliant and it should work in the most of the major contemporary shells. It was tested in zsh and bash.

Oh My ZSH

  1. Clone this repository into ~/.oh-my-zsh/custom/plugins directory:

    git clone https://github.com/aproint/awsenv.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/awsenv
  2. Activate the plugin in ~/.zshrc:

    plugins=( [plugins...] awsenv)
  3. Reopen your terminal

    Note
    This installation enables the hook automatically

Other Shells

  1. Clone this repository into ~/.awsenv directory.

    git clone https://github.com/aproint/awsenv.git ${HOME}/.awsenv
  2. 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.sh script 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.sh script in the shell profile manually:

      source ${HOME}/.awsenv/zsh-awsenv.zsh
  3. Reopen your terminal

Usage

The script defines two commands awsenv and awsenvclr.

  1. awsenv - execute in a directory with .awsenv file to load its AWS environment variables

  2. awsenvclr - unsets all AWS environment variables; this is a convenience command when you feel your env variables got mixed up.

Auto-load when changing directory

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.

How does awsenv command work?

  1. It searches for a .awsenv file 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).

  2. If awsenv is not active, it stores the current AWS environment variables into AWSENV_PREV_ equivalents (aka history).

  3. It sources the .awsenv file into your current shell session.

    Important
    awsenv filters out anything but AWS_ prefixed environment variables.
  4. When you change to a directory

    • without .awsenv file in its parent hierarchy, it loads your previous AWS environment variables from AWSENV_PREV_ and clears the history.

    • with another .awsenv file, it loads the new AWS environment variables, but it doesn’t overwrite the history.

Development

  • PRs are welcomed.

  • Tests written with bats.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages