From 5fa8925daa082c02d65b082f540f9d2e4c60a260 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Mon, 28 Jul 2025 23:38:40 +0100 Subject: [PATCH] [shell-common] Update instances command --- .../.config/shell-common/functions.sh | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/shell-common/.config/shell-common/functions.sh b/shell-common/.config/shell-common/functions.sh index 159c088..3c05b38 100644 --- a/shell-common/.config/shell-common/functions.sh +++ b/shell-common/.config/shell-common/functions.sh @@ -130,5 +130,39 @@ function awsssh() { echo "No public IP found. Falling back to AWS Session Manager..." aws ssm start-session --target "$instance_id" "${aws_opts[@]}" fi - +} + +function instances() { + local profile="" + local region="" + + # Parse optional arguments + while [[ $# -gt 0 ]]; do + case "$1" in + --profile) + profile="$2" + shift 2 + ;; + --region) + region="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + echo "Usage: list_ec2_instances [--profile prof] [--region region]" + return 1 + ;; + esac + done + + # Build AWS CLI options + local aws_opts=() + [[ -n "$profile" ]] && aws_opts+=(--profile "$profile") + [[ -n "$region" ]] && aws_opts+=(--region "$region") + + # Query EC2 for names and instance IDs + aws ec2 describe-instances \ + --query 'Reservations[].Instances[].{Name: Tags[?Key==`Name`].Value | [0], InstanceId: InstanceId}' \ + --output table \ + "${aws_opts[@]}" }