Direnv
If you’re bouncing between multiple CLI credentials like AWS for example, this tool might be a nice quality of life improvement.
Basically, direnv can load and unload environment variables depending on the current directory you’re in. In this post I’ll show how to use it to switch between AWS credentials.
Let’s start with a real world example right of the bat:
~/playground master* ❯ ls -l
total 20
drwxr-xr-x 2 dbei dbei 4096 Apr 21 09:37 project1
drwxr-xr-x 2 dbei dbei 4096 Apr 21 09:37 project2
~/playground master*
- Project1 will use AWS Account #1
- Project2 will use AWS Account #2
Let’s start with Project1:
-
Setup direnv.
-
cd into your desired directory. In my case it would be
project1
. -
Create .envrc:
vi .envrc
-
Add your AWS credentials which you can generate in IAM:
-
cd
in and out of that directory. -
You should see an error:
-
Type
direnv allow
and you should be good: -
You can test that it works with any AWS CLI command. For example:
Repeat the same process for Project2 and use different credentials. After that, you should be set. From here you can move between folders, which will automatically move between your AWS credentials and accounts!
Tip
Don’t forget to add .envrc
to your .gitignore
!