Is there a way to export an AWS CLI Profile to Environment Variables?

后端 未结 7 981
半阙折子戏
半阙折子戏 2020-12-29 23:41

When working with certain third-party tools like Terraform, it\'s not easily possible to specify an AWS CLI profile, and I like working with the environment variables better

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 00:12

    I like Kay's ideas of a script that exports the desired profile so I wrote one too:

    PROFILES=$(awk -F"\\\]|\\\[" '/^\[/{print $2}' ~/.aws/credentials)
    
    select PROFILE in $PROFILES; do
      export AWS_ACCESS_KEY_ID="$(aws configure get aws_access_key_id --profile $PROFILE)"
      export AWS_SECRET_ACCESS_KEY="$(aws configure get aws_secret_access_key --profile $PROFILE)"
      export AWS_DEFAULT_REGION="$(aws configure get region --profile $PROFILE)"
      break
    done
    
    echo AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    echo AWS_SECRET_ACCESS_KEY=$(echo $AWS_SECRET_ACCESS_KEY|tr '[:print:]' '*')
    echo AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
    

    Just put it in a file and then source (.) it from your shell.

提交回复
热议问题