Skip to content

Instantly share code, notes, and snippets.

@aalbertson
Created September 11, 2025 15:25
Show Gist options
  • Save aalbertson/4592632fd89881fa289662e35986e9af to your computer and use it in GitHub Desktop.
Save aalbertson/4592632fd89881fa289662e35986e9af to your computer and use it in GitHub Desktop.
ssm like ssh with query of ec2 instances

What is

  • Aliases to handle finding and connecting to ec2 instances over SSM so you're not using ssh and you don't have to remember the syntax

How use

  • Add to your aliases (e.g. ~/.zshrc.local, ~/.bashrc, ~/.bash_profile, or in your full aliases section), source (new terminal session should do the trick!)

Must haz

  • Ok, here goes.
  1. ec2 - prompt that will allow you to enter a name or part of a name (ignores case) and return all hosts in the set of regions listed so you can grab the Instance ID and use it for whatever (including the later ssm command) 1.1. includes some basic info about the hosts, IP, ID, region, State, Size/type. 1.2. defaults to returning all hosts if none entered
ec2() {
    US_REGIONS=("us-east-1" "us-east-2" "us-west-1" "us-west-2")

    for REGION in "${US_REGIONS[@]}"; do
        echo "Region: $REGION"
        if [ -z "$1" ]; then
            aws --region "$REGION" ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name`].Value | [0], InstanceType, PublicIpAddress, PrivateIpAddress, State.Name]' --output table
        else
            aws --region "$REGION" ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name`].Value | [0], InstanceType, PublicIpAddress, PrivateIpAddress, State.Name]' --output table | grep -i "$1"
        fi
        echo
    done
}
  1. ssm - prompt that you can take the instance ID and directly connect (note: default behavior with no input is similar to ec2 above
ssm () {
    export AWS_PAGER=""

    if [ -z "$1" ]; then
        echo "No instance specified. Here are the available instances:"
        echo "----------------------------------------------------"
        ec2
        echo "----------------------------------------------------"
        echo "Usage: ssm <instance-id or name> [region]"
        echo "Example: ssm i-1234567890abcdef0"
        echo "         ssm webserver"
        echo "         ssm webserver us-west-2"
        return 1
    fi

    local instance_id=$1
    local region=${2:-us-east-2}
    
    # If the first argument doesn't start with "i-", treat it as an instance name
    if [[ ! "$instance_id" =~ ^i- ]]; then
        # Get the instance ID of the most recent instance matching the name (case insensitive)
        instance_id=$(aws --region "$region" ec2 describe-instances \
            --filters "Name=tag:Name,Values=*${instance_id}*" \
            --query 'sort_by(Reservations[].Instances[], &LaunchTime)[-1].InstanceId' \
            --output text)
        
        if [[ "$instance_id" == "None" || -z "$instance_id" ]]; then
            echo "No matching instance found with name containing: $1"
            return 1
        fi
        echo "Connecting to instance: $instance_id"
    fi
    
    aws --region "$region" ssm start-session --target "$instance_id"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment