When working with the AWS CLI or Infrastructure-as-Code (IaC) tools like Terraform, many developers store long-term Access Keys and Secret Access Keys in plaintext within the ~/.aws/credentials file.
However, storing credentials in plaintext creates serious security vulnerabilities: malware on a local machine or an accidental commit of the .aws directory to a public repository like GitHub can expose your AWS environment to unauthorized access.
In this article, we’ll explore how to use aws-vault , an open-source tool designed to keep IAM credentials safely encrypted in your OS keystore while leveraging short-lived Temporary Credentials for your daily AWS operations.
What is aws-vault?
aws-vault is a CLI tool designed to securely store and access AWS credentials in development environments.
- Encrypted Keystore Integration: It leverages native OS credential managers (macOS Keychain, Windows Credential Manager, Secret Service for Linux) to store long-term keys.
- STS-Based Short-Lived Tokens: Instead of exposing raw keys in disk files, it calls AWS Security Token Service (
AssumeRole or GetSessionToken ) to issue temporary tokens that are injected into sub-process environment variables.
- Seamless MFA & Multi-Account Switching: It handles MFA token prompts smoothly and simplifies role assumption across multiple AWS accounts.
Installation and Initial Setup
1. Installation
You can install aws-vault using package managers across macOS, Windows, or Linux.
brew install --cask aws-vault
- Windows (Scoop / Chocolatey):
scoop install aws-vault
or
choco install aws-vault
2. Adding Credentials ( aws-vault add )
Use aws-vault add <profile-name> to store your access keys securely in the OS keychain.
$ aws-vault add my-dev-profile
Enter Access Key Id: AKIAXXXXXXXXXXXXXXXX
Enter Secret Access Key: ****************************************
Added credentials to profile "my-dev-profile" in vault
Once added, you no longer need long-term secrets written in ~/.aws/credentials .
Running AWS CLI and IaC Tools
1. Executing Single Commands ( aws-vault exec )
Wrap your commands with aws-vault exec . Temporary tokens ( AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY , and AWS_SESSION_TOKEN ) are injected into the sub-shell environment only for the duration of that command.
# List S3 buckets
aws-vault exec my-dev-profile -- aws s3 ls
# Run Terraform plan/apply
aws-vault exec my-dev-profile -- terraform plan
2. Adjusting Session Duration
To customize session validity, use the --duration flag:
aws-vault exec my-dev-profile --duration=8h -- aws s3 ls
3. AWS Console Quick Login ( aws-vault login )
In addition to CLI operations, you can securely launch an AWS Management Console session in your default browser:
aws-vault login my-dev-profile
Integrating with AWS Config (Assume Role & MFA)
Configure IAM Roles and MFA devices in ~/.aws/config , and aws-vault will automatically handle role assumptions.
Example ~/.aws/config :
[profile my-base-user]
region = us-east-1
mfa_serial = arn:aws:iam::123456789012:mfa/my-user
[profile dev-admin]
source_profile = my-base-user
role_arn = arn:aws:iam::987654321098:role/DeveloperAdminRole
Running the command below will prompt for your MFA token and assume the targeted role seamlessly:
aws-vault exec dev-admin -- aws sts get-caller-identity
Key Takeaways
- Clear
~/.aws/credentials : Remove long-term plaintext access keys.
- Store in OS Keychain: Secure credentials using
aws-vault add .
- Use Isolated Execution Environments: Run commands safely using
aws-vault exec .
By following this approach, you eliminate the risk of credential leaks while keeping your day-to-day workflow efficient and secure!
Loading article