luktom.net
  • blog
  • contact
  • polish





Work comfortable with AWS CLI profiles in Bash

On 15 Mar, 2018
AWS
No Comments
Views : 5744

As I work with multiple AWS accounts every day I need to switch between them very often. It’s a key issue for me to always be sure on which account I work and execute AWS CLI command.

So I wrote a quick bash function that I put in my .bashrc. The function is used in my PS1 prompt and shows current AWS CLI profile (from env variable AWS_PROFILE):

aws_profile() {
    if [[ -n $AWS_PROFILE ]]; then
      echo -n "[$AWS_PROFILE] "
    else
      echo -n ""
    fi
}

PS1="\$(aws_profile)\[\033[01;34m\]\w\[\033[00m\]\$ "

I also have two handy aliases for easy and reliable setting of AWS_PROFILE env variable, without the need to call export AWS_PROFILE=profile_name each time:

setprofile alias checks if the provided profile name is present in ~/.aws/config so you don’t have to worry about any typos :)

clearprofile alias – as the name suggests – clears profile name in AWS_PROFILE env variable, so you now use default profile.

The code for these aliases:

alias clearprofile='export AWS_PROFILE='

setprofile() {
  if [ $(cat ~/.aws/config | grep "\[profile $1\]" | wc -l) == 0 ]; then
    echo "Invalid profile name"
  else
    export AWS_PROFILE=$1
  fi
}

I hope these ticks will make your work easier :)



Tags :   awsaws clibash

Related Posts

  • AWS Lambda and MySQL IAM Authentication in Go

  • “Immutable infrastructure: automate VM template creation using Packer and Ansible” – an invitation to my speech

  • Configure AWS CLI profiles – quickly and efficiently

  • Leave a Comment

    Click here to cancel reply

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>





    Łukasz Tomaszkiewicz

    Łukasz Tomaszkiewicz

    Łukasz Tomaszkiewicz is a highly skilled and passionate cloud expert who loves to automate repeatable things and secure them.

    His broad experience in the areas of software development, database design, containerization and cloud infrastructure management gives him a holistic view of a modern technology stack.

    In his spare time he enjoys photography, blogging and speaking on local IT-related communities.

    Vim-believer :)

    Categories

    • Ansible
    • AWS
    • C#
    • Go
    • Google Cloud
    • Kubernetes
    • Prometheus
    • Speeches
    • Virtualization
    • Windows

    Tags

    alert alerting alertmanager ansible ansible operator argocd aws aws cli aws ug bash c# centos cloudwatch databases esxi flux gcp gitops google cloud k8s kubernetes linux mysql open source operator operator-sdk policies powershell prelekcje prometheus recovery restore rhel rpo rto scp speeches terraform virtualization vmware vsan vsphere weaveworks wifi windows

    Copyright © 2006-2018 by Łukasz Tomaszkiewicz. Wszelkie prawa zastrzeżone