[EN] How I Stopped Copy-Pasting AWS EC2 IPs and Started SSHing Smarter
![[EN] How I Stopped Copy-Pasting AWS EC2 IPs and Started SSHing Smarter](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2FDXRP2PKlsFQ%2Fupload%2F8767e91ce57fa7ff90bca9149c142626.jpeg&w=3840&q=75)

Remembering IP in a massive, dynamic environment is not easy. You may have an instance with IP 10.0.0.1 today, but there is no guarantee that the same server will be there tomorrow. If you are still SSH-ing to your server using a traditional method like ssh user@ip_addressyou’ll have a hard time remembering the IP address of that server. Wouldn't it be easier to just run server_a or server_b to get into that particular server? You just have to know the name of the server, which is easier to remember. Also, the command is dynamically updated when a new server is created or deleted.
tl;dr
Allow the bastion host to read EC2 metadata
Create a Python script to get all the instance names and private IPv4 addresses
Add the command alias to your
.bashrcor.zshrcfile.Add cron to run the script automatically and update your alias.
Allow Bastion host to get EC2 instances
The script will get all the EC2 data using the
describe-instancescommand. Create an AWS IAM policy to allow read access to the instances:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ec2:DescribeInstances", "Resource": "*" } ] }Create or modify your existing Bastion IAM role to attach this IAM policy.
Verify the access by running
aws ec2 describe-instancesinside the Bastion host.
Create a script to generate the command alias
The idea is to generate this output that can be fed to the .bashrc:
alias server_a="ssh ubuntu@private_ipv4_address"
...
Here’s the Python code to do that:
Generate the alias and write it to ~/.awsvmaliases:
python3 aliasgen.py > ~/.awsvmaliases
Automatically load the aliases
To automatically load those aliases when you open the terminal, open your .bashrc or .zshrc file and append this line:
...
source ~/.awsvmaliases
Automatically update the list
In my case, it is sufficient to update the script once an hour. So I just use cron to update the aliases:
# Inside the crontab
@hourly /usr/bin/python3 path/to/aliasgen.py > ~/.awsvmaliases
If you need to manually update the list without waiting for cron to run, you can run this command:
python3 path/to/aliasgen.py > ~/.awsvmaliases
# If you use bash shell, tell bash to reload the .bashrc file and read the changes
source ~/.bashrc
![[EN] Set Up Amazon ECR Pull-Through Cache for Docker Hub](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F631dd8693e8d6f3497ad63e7%2F1ca35a5a-6303-4a86-badb-91961cf65694.jpg&w=3840&q=75)
![[EN] Track progress of MySQL Import/Export process using PV](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2Fjf1EomjlQi0%2Fupload%2Fa7ee07f61c1dc2ad71b4cf2bb4523765.jpeg&w=3840&q=75)
![[EN] Lesson learned from using the wrong AWS ElastiCache Redis endpoint](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2FemolMCqnKfg%2Fupload%2Fc7eb8197eb9ef632459ae6612b861cc6.jpeg&w=3840&q=75)
![[EN] My experience taking the KCNA certification](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2FKXwPJtAJLfU%2Fupload%2F3deba4b52e1b8e442179a495944ccb9e.jpeg&w=3840&q=75)