Cloud providers have a bewildering array of CLI commands, and remembering them is a losing battle. Zsh aliases, combined with a touch of scripting, can transform this chaos into a manageable, efficient workflow.
Imagine you’re juggling multiple cloud accounts and services daily. You need to quickly spin up a VM, check its status, or list S3 buckets. Without aliases, this involves extensive typing, searching documentation, and a high chance of typos. This setup aims to drastically reduce that friction.
Let’s see it in action. We’ll set up a few common aliases for AWS, Azure, and GCP.
First, ensure you have the respective CLIs installed and configured for your accounts. For this example, we’ll assume you’re using aws configure, az login, and gcloud auth login.
Here’s a basic Zsh alias setup in your ~/.zshrc:
# AWS Aliases
alias aws-vm-list='aws ec2 describe-instances --query "Reservations[*].Instances[*].{ID:InstanceId,State:State.Name,Type:InstanceType,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,Name:Tags[?Key==`Name`].Value|[0]}" --output table'
alias aws-s3-ls='aws s3 ls'
alias aws-s3-sync='aws s3 sync'
# Azure Aliases
alias az-vm-list='az vm list --output table'
alias az-disk-list='az disk list --output table'
alias az-webapp-list='az webapp list --output table'
# GCP Aliases
alias gcp-vm-list='gcloud compute instances list --format="table(name,zone,status,internalIp,externalIp)"'
alias gcp-gcs-ls='gcloud storage ls'
alias gcp-gke-clusters='gcloud container clusters list --format="table(name,zone,status)"'
After adding these to your ~/.zshrc, reload your shell with source ~/.zshrc.
Now, instead of typing aws ec2 describe-instances --query "Reservations[*].Instances[*].{ID:InstanceId,State:State.Name,Type:InstanceType,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,Name:Tags[?Key==\Name`].Value|[0]}" --output table, you simply type aws-vm-list. Similarly, az vm list --output tablebecomesaz-vm-list, and gcloud compute instances list --format="table(name,zone,status,internalIp,externalIp)"becomesgcp-vm-list`.
The core problem these aliases solve is cognitive load and typing efficiency. Each cloud provider has distinct command structures and options. By creating short, memorable aliases, you offload the need to recall these specifics, allowing you to focus on the task at hand. This is particularly powerful when working with multiple cloud environments concurrently, as it provides a consistent mental model for common operations.
The aws-vm-list alias demonstrates a more complex use case. It doesn’t just wrap a single command; it includes a JMESPath query (--query) to extract and format specific instance details into a readable table. This transforms a verbose, data-heavy output into precisely what you need to see at a glance. The single quotes around the entire command and the backticks within the JMESPath query are crucial for proper shell interpretation.
Beyond simple command shortcuts, you can build more sophisticated aliases that accept arguments or chain commands. For instance, an alias to quickly SSH into a specific AWS instance might look like this:
alias aws-ssh='f() {
INSTANCE_ID=$1
INSTANCE_NAME=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[*].Instances[*].Tags[?Key==`Name`].Value|[0]" --output text)
PUBLIC_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[*].Instances[*].PublicIpAddress" --output text)
if [ -z "$PUBLIC_IP" ]; then
echo "Instance $INSTANCE_ID ($INSTANCE_NAME) has no public IP."
return 1
fi
echo "Connecting to $INSTANCE_NAME ($INSTANCE_ID) at $PUBLIC_IP..."
ssh -i ~/.ssh/your-aws-key.pem ec2-user@$PUBLIC_IP
}; f'
This alias, aws-ssh, takes an instance ID as an argument ($1), fetches its public IP and name using separate aws ec2 describe-instances calls, and then uses ssh to connect. The f() { ... }; f syntax is a common Zsh idiom for defining a function-like alias that can accept arguments. The ~/.ssh/your-aws-key.pem and ec2-user would need to be adjusted for your specific setup.
The real power here lies in the ability to customize and extend. You can create aliases to deploy specific application stacks, check resource quotas across regions, or even trigger CI/CD pipelines. The key is to identify repetitive tasks and encapsulate them into simple, memorable commands.
Many developers overlook the ability to define complex functions within their Zsh aliases. Instead of just aliasing a single command, you can define a small shell script that takes arguments, performs multiple operations, and even includes conditional logic. This allows for highly dynamic and context-aware shortcuts that go far beyond simple text substitution. It’s a way to embed operational intelligence directly into your command line.
The next logical step is exploring tools like fzf to make these aliases contextually searchable, allowing you to fuzzy-find the exact command or resource you need without typing anything.