Generate SSH config file for your AWS instances

you have plenty of application instances running in a region and you quickly want to connect to one of them.

so, you log in to console, select the region, click on instance get the IP address (or right-click and get copy connect string), come back to your terminal and type (or paste)

ssh -i `whereverPAthtoMyFIleIs` ubuntu/ec2-user@someip 

what if you messed up writing the wrong path. well at least I had some problems before while following this naive approach, so I read about something called host config for ssh that you pre-write the path to file and all the details needed to connect to the instance

something like

and store it in by default in ~/.ssh/config file .. well now you can just do

well I was greedy, I wanted to connect all the instances whenever I want to, without having to write them one time or go back to console and get the string for that matter

so I give you :P ( the dirty code )

this BOTO3 code generate the host config file for all your amzon-linux, ubuntu machine running in a region ( yeah I hardcoded the region, change it to your wish)

Assumption:

Each of your instances must be tagged with unique 'Name' else, how will u know which one to connect to? :)

PREREQUISITES:

you have aws_cli setup with proper keys and access to ec2 for instances

python3* is installed

boto3 package is installed

EXECUTION:

  1. copy the code to a file, name it anything u want ( i will name it hostconfig.py)

  2. create an empty file named config.txt ( not necessary )

  3. execute command python hostconfig.py

voila, config.txt now has the host config ready

you can move it to ~/.ssh/config and execute the command

ssh my_unique_instance_Name #yeah, just a random name, you got the idea :)

NOTE: change the pem path to your absolute path in system where u store your private keys

Published By

DevOps AWS ☁️(CDA,CSA) | Python | Terraform | Packer | Docker | Jenkins | Ansible | ELK | Rancher | Kubernetes | Bash

Followarrow-up-right

Last updated