AWS CLI to list AWS EC2 Instances

Connect to EC2 instance using System Manager and AWS CLI.

Oct 16, 2022

Connect to EC2 instance using System Manager and AWS CLI.

Prerequisites

AWS CLI command to list EC2 instances

# filename: list-ec2instances

aws ec2 describe-instances \
    --query 'Reservations[*].Instances[*].{
        Instance:InstanceId,
        State:State.Name,
        Name:Tags[?Key==`Name`]|[0].Value,
        Application:Tags[?Key==`Application`]|[0].Value,
        Customer:Tags[?Key==`Customer`]|[0].Value} | sort_by(@, &[0].Instance)' \
    --output table

Starting System Manager session using fzf

echo "Using the follwing profile: AWS_PROFILE=${AWS_PROFILE}"
aws ssm start-session --target $(list-ec2instances | fzf | grep -oP '\si\-[0-9a-f]*')

See also