AWS Builder Center

Stop Using SSH Keys? Manage EC2 Instances Securely with AWS Systems Manager Session Manager

Learn how to connect to Amazon EC2 instances without opening inbound SSH ports or managing SSH keys for interactive sessions. Explore AWS Systems Manager Session Manager, IAM permissions, secure access, and practical troubleshooting.

Full Stack Developer | Python | Django | PHP | React | MySQL | REST Api | AWS

1. Introduction

When we launch an Amazon EC2 instance, one of the first things we usually do is connect to it using SSH.
For example:
ssh -i my-key.pem ec2-user@PUBLIC_IP
Although SSH is a common way to access Linux servers, it introduces some operational challenges:
  • Managing and protecting private key files.
  • Configuring inbound SSH rules.
  • Restricting access to trusted IP addresses.
  • Maintaining secure access for multiple users.
  • Managing access when team members change.
Is there another way to connect to EC2 without opening port 22?
Yes! AWS provides a service called AWS Systems Manager Session Manager.
It allows authorized users to connect to managed EC2 instances through the AWS Management Console or AWS CLI without requiring inbound SSH access.
Let's understand how it works.

2. What is AWS Systems Manager?

AWS Systems Manager is an AWS service that helps manage and operate resources across cloud and hybrid environments.
It provides capabilities for:
  • Managing EC2 instances.
  • Running commands remotely.
  • Starting interactive sessions.
  • Automating operational tasks.
  • Managing patches.
  • Maintaining operational visibility.
One of its useful capabilities is Session Manager.

3. What is Session Manager?

Session Manager is a capability of AWS Systems Manager that provides secure, browser-based or CLI-based interactive access to managed instances.
Instead of connecting directly to an EC2 instance using SSH, Session Manager establishes a session through the AWS Systems Manager infrastructure.
The basic architecture looks like this:
Administrator
|
v
AWS Console
|
v
Session Manager
|
v
AWS Systems Manager
|
v
SSM Agent
|
v
EC2 Instance
The administrator starts a session, Systems Manager communicates with the instance through the SSM Agent, and the user receives an interactive terminal.
The important difference: You do not need to expose inbound SSH port 22 just to establish a Session Manager shell session.

4. How Does Session Manager Work?

Several components work together to establish a session.
1. IAM permissions
IAM controls which users or roles are allowed to start sessions and which instances they can access.
2. SSM Agent
The Systems Manager Agent runs on the EC2 instance and communicates with the Systems Manager service.
3. IAM instance profile
The EC2 instance needs an appropriate IAM role, commonly with the  AmazonSSMManagedInstanceCore  policy, so the agent can communicate with Systems Manager.
4. Network connectivity
The instance must be able to reach the required Systems Manager service endpoints. This can be achieved through internet connectivity, NAT, or appropriately configured VPC endpoints.
5. Session Manager
Systems Manager establishes the interactive session between the authorized user and the instance.

5. Hands-on: Connect to EC2 Without SSH

Let's walk through a basic setup.

Step 1: Launch an EC2 Instance

Open the AWS Management Console and navigate to Amazon EC2.
Launch an instance using a supported operating system, such as Amazon Linux 2023.
For this exercise, configure:
  • Instance type: A suitable small instance.
  • AMI: Amazon Linux 2023.
  • Network: Your selected VPC and subnet.
  • Security Group: No inbound SSH rule is required for Session Manager access.
You do not need to expose port 22 to the internet.

Step 2: Configure the IAM Role

Navigate to IAM and create an IAM role for EC2.
Choose:
  • Trusted entity: AWS service.
  • Use case: EC2.
  • Permission policy:  AmazonSSMManagedInstanceCore .
Attach this role to your EC2 instance through the EC2 instance's security settings.
This allows the instance to communicate with Systems Manager.
Security tip: The instance role and the human administrator's permissions serve different purposes. The instance role allows the agent to communicate with AWS; the administrator's IAM permissions determine who can start sessions.

Step 3: Verify SSM Agent

Amazon Linux 2023 includes the SSM Agent by default.
You can check its status after obtaining access through an appropriate method:
sudo systemctl status amazon-ssm-agent
If the agent is not installed or running, install or start it according to the AWS documentation for your operating system.

Step 4: Configure Network Connectivity

The EC2 instance must reach the required Systems Manager endpoints.
For private instances, you can use VPC interface endpoints for the relevant Systems Manager services, commonly:
  •  ssm 
  •  ssmmessages 
  •  ec2messages , where applicable
The required endpoints depend on the Region and current Systems Manager connectivity requirements.
Make sure DNS resolution, endpoint security groups, and outbound network rules are configured correctly.

Step 5: Connect Using Session Manager

Open the AWS Management Console.
Navigate to:
AWS Systems Manager
|
v
Session Manager
|
v
Start Session
|
v
Select EC2 Instance
|
v
Start Session
If the instance is correctly configured and appears as a managed node, select it and start a session.
You should now receive a terminal session without using an SSH private key or opening inbound port 22.

6. Session Manager vs SSH

Both approaches allow administrators to interact with remote instances, but their access mechanisms are different.
Feature
SSH
Session Manager
Inbound port 22
Usually required
Not required
SSH private key
Commonly required
Not required for standard sessions
IAM integration
Additional configuration
Built-in authorization model
Browser-based access
Not normally
Yes
Centralized session logging
Requires additional setup
Supported
Network requirements
SSH connectivity
Systems Manager connectivity
Session Manager is especially useful when you want centralized access control and do not want to expose SSH to the network.
SSH remains useful for many existing workflows, but it is not the only way to administer EC2.

7. Additional Capabilities

Session Manager provides more than interactive shell access.
Port forwarding: You can securely forward a local port to a remote port on an instance or another reachable destination without opening that application port to the internet.
Remote administration: Administrators can execute commands and perform troubleshooting tasks through their managed access workflow.
Session logging: You can configure session data logging to Amazon S3 or Amazon CloudWatch Logs, subject to supported session types and configurations.
Centralized access control: IAM policies can restrict who can access instances and under what conditions.
One important limitation: Session content logging is not supported for sessions using SSH or port forwarding through Session Manager. Plan your auditing requirements accordingly.

8. Common Problems and Troubleshooting

Sometimes an EC2 instance does not appear in Session Manager.
Here are common causes:
Problem 1: Instance is not showing as a managed node
Check whether the SSM Agent is installed and running, and whether the instance has the required IAM instance profile.
Problem 2: Access denied
Check the IAM permissions of the administrator or user starting the session.
Problem 3: SSM Agent cannot connect
Check outbound network connectivity, DNS resolution, VPC endpoints, and endpoint security groups.
Problem 4: Session fails to start
Verify that the instance is online in Systems Manager and that the required agent and service connectivity are working.
A useful troubleshooting approach is to check IAM permissions, agent status, and network connectivity separately.

9. Security Best Practices

When managing EC2 instances with Session Manager, consider these practices:
  • Apply least-privilege IAM permissions.
  • Avoid granting unrestricted session access to every instance.
  • Use IAM policies to control which managed instances users can access.
  • Enable session logging where appropriate.
  • Restrict access to sensitive production environments.
  • Monitor administrative activity using AWS logging and auditing services.
  • Avoid unnecessary inbound security group rules.
The goal is not just to connect to a server, but to manage access securely and consistently.

10. When Should You Use Session Manager?

Session Manager is useful in several situations:
Production environments: Access servers without exposing inbound SSH ports.
Private subnets: Manage instances that do not have public IP addresses, provided they have the required Systems Manager connectivity.
Development environments: Give developers controlled access to EC2 instances.
Security-focused infrastructure: Centralize authorization and session auditing.
Operations and troubleshooting: Provide a consistent way to access managed instances.

Conclusion

AWS Systems Manager Session Manager offers an alternative to traditional SSH-based EC2 access.
By combining IAM permissions, SSM Agent, and the required network connectivity, administrators can establish interactive sessions without opening inbound SSH ports.
For anyone learning AWS, this is an important concept because cloud security is not only about launching infrastructure. It is also about controlling how people access and manage that infrastructure.
Instead of asking only, "How do I connect to my EC2 instance?", start asking:
"How can I provide secure access to my cloud infrastructure without unnecessarily exposing it to the internet?"
That is an important step towards building more secure AWS environments.
Discussion question: Would you prefer SSH with key pairs or AWS Systems Manager Session Manager for managing your EC2 instances? Share your reasons in the comments!

Official AWS documentation

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article