Release v0.1.1 (What’s new?).!--None.svg?style=social!--None.svg?style=social

Welcome to ssh2awsec2 Documentation#

ssh2awsec2 is a interactive Python CLI tool to help you SSH to your EC2 instances smoothly.

+----- ⏱ 🟢 Start 'SSH to EC2 instance' ----------------------------------------+
| Which EC2 you want to SSH to?
| ⬆ ⬇ Move your cursor up and down and press Enter to select.
[?] Current selection: id = i-0123456789aaaaaaa, name = 'jump-host', public ip =
 > id = i-0123456789aaaaaaa, name = 'jump-host', public ip =
   id = i-0123456789bbbbbbb, name = 'dev-box', public ip =
   id = i-0123456789ccccccc, name = 'web-app', public ip = 333.333.333.333

| ✅ selected: id = i-0123456789aaaaaaa, name = 'jump-host', public ip =
| try to locate pem file ...
| ✅ found pem file at: /Users/myname/ec2-pem/111122223333/us-east-1/dev.pem
| Try to find OS username based on the AMI id = ami-0123456789abcdefg, name = ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server
| cannot automatically detect OS username
| Choose OS username of your EC2, if you don't know, see this document:
| ⬆ ⬇ Move your cursor up and down and press Enter to select.
[?] Current selection: ubuntu
 > ubuntu

| Run ssh command: ssh -i /Users/myname/ec2-pem/111122223333/us-east-1/dev.pem ubuntu@
| Precess Ctrl + D to exit SSH session
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.0-1031-aws x86_64)

 * Documentation:
 * Management:
 * Support:

  System information as of Wed Jun 14 23:28:41 UTC 2023

  System load:  0.0                Processes:             104
  Usage of /:   52.0% of 19.20GB   Users logged in:       0
  Memory usage: 7%                 IPv4 address for ens5:
  Swap usage:   0%

 * Ubuntu Pro delivers the most comprehensive open source security and
   compliance features.

 * Introducing Expanded Security Maintenance for Applications.
   Receive updates to over 25,000 software packages with your
   Ubuntu Pro subscription. Free for personal use.

Expanded Security Maintenance for Applications is not enabled.

19 updates can be applied immediately.
To see these additional updates run: apt list --upgradable

Enable ESM Apps to receive additional future security updates.
See or run: sudo pro status

The list of available updates is more than a week old.
To check for new updates run: sudo apt update
New release '22.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

2 updates could not be installed automatically. For more details,
see /var/log/unattended-upgrades/unattended-upgrades.log

Last login: Wed Jun 14 23:23:58 2023 from
ubuntu@ip-10-1-1-1:~$ logout
Connection to closed.
+----- ⏰ 🟢 End 'SSH to EC2 instance', elapsed = 16.35 sec ---------------------+

Usage Example

# show CLI help info

# or use the short name

For first usage, you need to configure ssh2awsec2 to locate your AWS CLI credential.

# show config related information
sshec2 info

# show config subcommand help info
sshec2 config

# show current config
sshec2 config show

# set the AWS profile
sshec2 config set-profile --profile <your_aws_profile>

# set to use the default AWS profile
sshec2 config set-profile --profile default

# set cache expire time
sshec2 config set-cache-expire 3600

# clear cache
sshec2 clear-cache

Then you can use sshec2 ssh command to ssh to your AWS EC2 instance. Just follow the interactive prompt:

# show help info
sshec2 ssh --help

# pick an EC2 and ssh into it
sshec2 ssh

# you can use -n to filter EC2 by name tag
sshec2 ssh -n <name_tag_here>
# example
sshec2 ssh -n jump_host

# you can use -i to filter EC2 by instance id
sshec2 ssh -i <full_instance_id_or_chunk_here>
# example
sshec2 ssh -i 1a2b

# you can use -k to filter EC2 by instance id (quote the query string)
sshec2 ssh -k "<name_tag_key_value_pair_here>"
# example
sshec2 ssh -k "env=prod,owner=alice"

It prompt you to select EC2 instance and probably ask for the OS username. The selection will be cached for 24 hours, so you will see it on top of the list next time.


ssh2awsec2 is released on PyPI, so all you need is:

$ pip install ssh2awsec2

To upgrade to latest version:

$ pip install --upgrade ssh2awsec2

Table of Content#

About the Author#

(\ (\
( -.-)o

Sanhe Hu is a seasoned software engineer with a deep passion for Python development since 2010. As an author and maintainer of 20+ open-source projects, I bring a wealth of experience to the table. As a Senior Solution Architect and Subject Matter Expert in Amazon Web Services, Cloud Engineering, DevOps, Big Data, and Machine Learning, I thrive on helping clients with platform design, enterprise architecture, and strategic roadmaps.

API Document#