A Zsh plugin for Oh My Zsh that simplifies switching between AWS profiles and managing corresponding Terraform directories.
- 🔄 Switch between AWS profiles with automatic SSO authentication
- 📁 Automatically set Terraform working directory (
TFPATH) based on profile - 🎯 Interactive profile selection with fzf support
- ⚙️ Configurable profile-to-path mappings
-
Clone this repository into your Oh My Zsh custom plugins directory:
git clone https://github.com/jmischler72/tfaws.git $ZSH_CUSTOM/plugins/tfaws -
Add
awsandtfawsto your plugins list in~/.zshrc:plugins=(... aws tfaws)
-
Reload your shell:
source ~/.zshrc
tfaws [COMMAND] [PROFILE]| Command | Description |
|---|---|
ch <profile> |
Switch to the specified AWS profile and set TFPATH |
ch (no profile) |
Clear current AWS profile and TFPATH |
ls, list |
List and interactively select an AWS profile |
sh, show |
Show current AWS profile and Terraform path |
config |
Configure Terraform paths for AWS profiles |
# Switch to 'dev' profile
tfaws ch dev
# Interactive profile selection (uses fzf if available)
tfaws list
# Show current profile and path
tfaws show
# Edit configuration file
tfaws configThe plugin uses configuration files with YAML-like syntax to map AWS profiles to Terraform directories:
~/.tfaws- Global configuration (applies to all directories)./.tfaws- Local configuration (overrides global settings for current directory)
tf_paths:
dev: "$HOME/terraform/dev"
staging: "$HOME/terraform/staging"
prod: "$HOME/terraform/production"Environment variables like $HOME are automatically expanded.
Run tfaws config to create a template configuration file with your current AWS profiles:
tfaws configThis will:
- Create a
~/.tfawsfile if it doesn't exist - Pre-populate it with your available AWS profiles as commented templates
- Open the file in your default editor
The plugin enhances the terraform command to automatically use the configured TFPATH:
# When TFPATH is set, terraform commands run in that directory
terraform plan # Runs: terraform -chdir="$TFPATH" plan
terraform apply # Runs: terraform -chdir="$TFPATH" apply
# Format commands work normally (don't use -chdir)
terraform fmt # Runs: terraform fmtThe plugin provides an asp alias for quick profile switching:
asp dev # Same as: tfaws ch dev- Zsh shell
- Oh My Zsh
- AWS CLI v2 with configured profiles
fzf(optional, for enhanced interactive selection)
The plugin assumes you have AWS profiles configured and uses:
aws sts get-caller-identity- to check authentication statusaws sso login- for SSO authenticationaws_profiles- to list available profiles (assumed to be available)
-
Profile Switching: When you switch profiles, the plugin:
- Uses
asp(change_context) to set the AWS profile - Extracts the SSO session name from
~/.aws/config - Runs
awsidto ensure authentication - Sets
TFPATHenvironment variable from configuration
- Uses
-
Configuration Loading: The plugin reads configuration in this order:
~/.tfaws(global settings)./.tfaws(local overrides)
-
Terraform Integration: The enhanced
terraformcommand:- Uses
TFPATHfor most commands via-chdir - Runs format commands normally (without
-chdir) - Shows current context before execution
- Uses
- Profile not found: Ensure your AWS profile exists in
~/.aws/config - Path not set: Run
tfaws configto configure Terraform paths - Authentication failed: The plugin will automatically run
aws sso loginwhen needed
Use tfaws show to see current configuration:
tfaws show
# Output:
# Current AWS Profile: dev
# Current Terraform Path: /home/user/terraform/devThis project is licensed under the terms specified in the LICENSE file.