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

Naftuli Kay picture Naftuli Kay · Nov 28, 2016 · Viewed 10k times · Source

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 than the profiles.

Is there a way for me to have the AWS CLI simply export the current profile as AWS_ACCESS_KEY_ID and AWS_SECRET_KEY environment variables to my session?

Answer

Frederic Henri picture Frederic Henri · Nov 28, 2016

you could use the following command to set your environment variable

aws configure get default.aws_access_key_id
aws configure get default.aws_secret_access_key

if you have another profile you can change, another way to write is

aws configure get aws_access_key_id --profile <new_profile>
aws configure get aws_secret_access_key --profile <new_profile>

so for example it would be

export TF_VAR_access_key=`aws configure get default.aws_access_key_id`