Git Product home page Git Product logo

awsprofile's Introduction

AWS PROFILE

INTRODUCTION

Managing multiple AWS accounts from aws-cli tends to be a little tedious. When you need to switch between multiple AWS accounts, it seems to be a hassle keeping a track of the current account, all the available account names.

Even setting the variable and using it to switch between accounts doesn't seem like the right deal.

export AWS_PROFILE=user2

Aliases can be used to manage AWS profiles in a better way.

With aliases we can do the following:

  • List available profiles
  • View current profile configuration
  • Switch to another profile

Creating Aliases

First we will create functions that would be invoked by our Alias

Create a file as below:

 vim ~/.awsAliases

Next add the below lines in the file:

#!/bin/bash
function _awsListAll() {

    credentialFileLocation=${AWS_SHARED_CREDENTIALS_FILE};
    if [ -z $credentialFileLocation ]; then
        credentialFileLocation=~/.aws/credentials
    fi
 
    while read line; do
        if [[ $line == "["* ]]; then echo "$line"; fi;
    done < $credentialFileLocation;
};
 
function _awsSwitchProfile() {
   if [ -z $1 ]; then  echo "Usage: awsp profilename"; return; fi
   
   exists="$(aws configure get aws_access_key_id --profile $1)"
   if [[ -n $exists ]]; then
       export AWS_DEFAULT_PROFILE=$1;
       export AWS_PROFILE=$1;
       export AWS_REGION=$(aws configure get region --profile $1);
       echo "Switched to AWS Profile: $1";
       aws configure list
   fi
};

Now if you are using bash, open bash_profile as below:

vim ~/.bash_profile

And add the following files:

. ~/.awsAliases
alias awsall="_awsListAll"
alias awsp="_awsSwitchProfile"
alias awswho="aws configure list"

If you are using zshrc, then add the lines in ~/.zshrc

Now, reload your bash profile or zshrc as below

source ~/.bash_profile # For zshrc source ~/.zshrc

Running Aliases

To list all the profiles use:

awsall

To switch between profiles use:

awsp <profileName>

To show current profile details:

awswho

Credits

Code based on article written by Carl Nordenflet

awsprofile's People

Contributors

vaulstein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

arunsridher

awsprofile's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.