How To Connect To An AWS EC2 Instance Without Key Pair
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to add an EC2 instance to AWS System manager.
https://cloudaffaire.com/how-to-add-an-ec2-instance-to-aws-system-manager-ssm/
In this blog post, we will discuss how to connect to an AWS EC2 instance without key pair using both AWS console from web browser and AWS CLI from command prompt through AWS SSM Session Manager.
What Is AWS SSM Session Manager:
Session Manager is a fully managed AWS Systems Manager capability that lets you manage your Amazon Elastic Compute Cloud (Amazon EC2) instances, on-premises instances, and virtual machines (VMs) through an interactive one-click browser-based shell or through the AWS Command Line Interface (AWS CLI). Session Manager provides secure and auditable instance management without the need to open inbound ports, maintain bastion hosts, or manage SSH keys.
AWS SSM Session Manager Requirements:
There are multiple ways you can connect to your EC2 instance through session manager, for example, you can connect through AWS EC2 console or AWS SSM console or through AWS CLI or using SSH. In this demo, we have covered connections through AWS console and AWS CLI.
- One EC2 instance managed by AWS System Manager (SSM Managed Instance). You can follow the below blog posts to configure an AWS EC2 SSM Managed instance.
https://cloudaffaire.com/how-to-install-aws-system-manager-ssm-agent/
https://cloudaffaire.com/how-to-add-an-ec2-instance-to-aws-system-manager-ssm/
- AWS CLI installed and configured in your local system (Optional, If you want to connect using AWS CLI). You can follow the blog posts to install and configure AWS CLI.
https://cloudaffaire.com/how-to-install-aws-cli/
https://cloudaffaire.com/how-to-configure-aws-cli/
How To Connect To An AWS EC2 Instance Without Key Pair:
Using AWS Console Through Web Browser:
Step 1: Open your favorite web browser and connect to AWS console.
Step 2: Navigate to AWS EC2 and select the instance you want to connect and click ‘Connect’.
Step 3: Select ‘Session Manager’ and click ‘Connect’.
You will get connected to the EC2 instance with root privileges on the web browser. Type exit or click ‘Terminate’ to end your session. Click ‘Close’ when prompted.
Note: You can also connect from AWS SSM Session Manager.
Using AWS CLI Through Command Prompt Or Shell:
Step 1: Install SSM Session Manager Plugin for AWS CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
############################################################ ## How To Connect To An AWS EC2 Instance Without Key Pair ## ############################################################ ## Install SSM Session Manager Plugin # ---------- # Windows OS # ---------- # Download the installer for windows using below link https://s3.amazonaws.com/session-manager-downloads/plugin/latest/windows/SessionManagerPluginSetup.exe # Double click on the installer and complete the installation # ------ # MAC OS # ------ # Download the bundled installer curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip" # Unzip the package unzip sessionmanager-bundle.zip # Run the install command sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin # ---------------- # RHEL | CentOS OS # ---------------- # Download the Session Manager plugin RPM package (64 bit version). curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/linux_64bit/session-manager-plugin.rpm" -o "session-manager-plugin.rpm" # Install the package sudo yum install -y session-manager-plugin.rpm # ------------------ # Ubuntu | Debian OS # ------------------ # Download the Session Manager plugin deb package (64 bit version). curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb" # Install the package sudo dpkg -i session-manager-plugin.deb ## Verify the Session Manager plugin installation session-manager-plugin |
Step 2: Start a new session.
1 2 |
## Start a session aws ssm start-session --target instance-id " |
You will get connected to the EC2 instance with root privilege on your local terminal where you have executed the above command.
Step 3: Get SSM Session Manager session details.
1 2 3 4 5 6 |
## Open a new command prompt | shell window in your local system ## Get all session history aws ssm describe-sessions --state "History" ## Get current active session aws ssm describe-sessions --state "Active" |
If you intend to terminate the session using AWS CLI, note down the current active session id
Step 4: Terminate the session.
1 2 3 4 5 6 7 |
## Terminate the session ## type exit on the terminal where you have established your session exit # or in the other window execute below command aws ssm terminate-session --session-id " |
Hope you have enjoyed this blog post, to get more details on AWS SSM, please refer below AWS documentation
https://docs.aws.amazon.com/systems-manager/index.html
It should be aws ssm start-session –target
and not …
aws ssm start-session –target instance-id “”