DEV Community

neetu-mallan
neetu-mallan

Posted on

2

Automating the selection of named profiles from the .aws/credentials file

While working continuously on the AWS CLI, I found it tedious to switch between users by manually exporting the AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY as these credentials need to be set in every session of the shell. So I have created a bash script to automate this selection in which you can provide a named profile & based on the access key ID & secret access key the above environment variables would be set automatically.

To understand more on named profiles, please go through the AWS documentation link:

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

The .aws/credentials file created follows a format shown below:
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

In case I do not specify any named profile, the default profile would be applied as below

aws ec2 describe-instances

else if I want a specific profile to be applied to my command I can :
aws ec2 describe-instances --profile user1

This will list the ec2 instances for the user profile of user1.

You can automate this so that every time you open a shell you just provide the named profile to the script & it will set the access credentials for you that will be valid for the entire session.

While working on this automation I came across Ben Kehoe's article regarding the same & this worked as an inspiration to write my own script:

https://ben11kehoe.medium.com/aws-configuration-files-explained-9a7ea7a5b42e

I have placed the bash script my GitHub repository:

https://github.com/neetu-mallan/AutomateCLINamedProfileSelection

Check the README.md file to understand what the script does:

https://github.com/neetu-mallan/AutomateCLINamedProfileSelection/blob/main/README.md

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay