
Stop Opening Port 22: Secure AWS EC2 Access Using Systems Manager Session Manager
Discover how AWS Systems Manager Session Manager enables secure access to EC2 instances without exposing inbound SSH ports or managing bastion hosts. Learn the architecture, IAM configuration, networking requirements, hands-on setup, and security best practices.
Introduction
Imagine deploying an Amazon EC2 instance for your application. You need to connect to it regularly to install packages, troubleshoot errors, inspect logs, and manage configurations.
Traditionally, administrators use SSH (Secure Shell) to connect to Linux instances through TCP port 22.
But this introduces a security consideration: opening inbound SSH access, particularly to
0.0.0.0/0 , exposes the SSH service to connection attempts from anywhere on the internet.What if you could manage your EC2 instances without exposing SSH ports or maintaining a bastion host?
That's where AWS Systems Manager Session Manager comes in.
In this article, we'll explore how Session Manager works, understand its architecture, configure it step by step, and learn how to use it in real-world cloud environments.
1. Understanding the Traditional SSH Approach
To connect to a Linux EC2 instance using traditional SSH, you generally need:
- A reachable network path to the instance.
- An SSH client.
- A valid private key.
- An inbound security group rule allowing TCP port 22.
Example:
ssh -i my-key.pem ec2-user@<EC2-PUBLIC-IP>Although this approach works, it introduces several operational challenges:
- SSH private keys must be securely managed.
- Network access must be configured and maintained.
- Publicly reachable SSH services increase the attack surface.
- Managing access across multiple instances becomes complicated.
- Organizations may need bastion hosts or jump servers.
For example, allowing SSH access from
0.0.0.0/0 is generally not recommended.Can we manage EC2 without exposing inbound SSH access?
Yes. AWS Systems Manager Session Manager provides an alternative.
2. What Is AWS Systems Manager Session Manager?
AWS Systems Manager Session Manager is a capability of AWS Systems Manager that allows administrators to establish interactive sessions with managed instances.
It provides:
- Browser-based shell access.
- AWS CLI-based session access.
- IAM-based access control.
- Support for Linux and Windows instances.
- Session logging integrations with Amazon S3 and Amazon CloudWatch Logs.
- Connectivity to instances without requiring inbound SSH or RDP ports.
The important difference is that Session Manager does not require administrators to connect directly to the instance through an inbound SSH connection.
Instead, the instance communicates with AWS Systems Manager through the Systems Manager Agent (SSM Agent).
3. How Does Session Manager Work?
Session Manager uses a secure communication channel between the administrator and the managed instance.
The process works as follows:
- An administrator authenticates to AWS using an authorized identity.
- IAM policies determine whether the administrator can start a session.
- AWS Systems Manager communicates with the SSM Agent.
- The agent establishes a bidirectional communication channel.
- The administrator receives an interactive terminal.
Session traffic is encrypted using TLS 1.2, and session initiation requests are signed using AWS Signature Version 4.
Architecture Overview
The following architecture illustrates the communication flow.
flowchart TD A["Administrator"] --> B["AWS Console / AWS CLI"] B --> C["IAM Authentication & Authorization"] C --> D["AWS Systems Manager<br/>Session Manager"] D <-->|"Secure communication"| E["SSM Agent"] E --> F["EC2 Instance"] F --> G["Private Subnet / VPC"] D --> H["Amazon CloudWatch Logs"] D --> I["Amazon S3"] B --> J["AWS CloudTrail"]Key observation: The EC2 instance initiates outbound communication with Systems Manager. You do not need to allow inbound SSH traffic from the administrator's computer.
This also means that an EC2 instance can be managed without a public IP address, provided it has the required Systems Manager connectivity.
4. AWS Services Involved
AWS Service / Component | Purpose |
|---|---|
Amazon EC2 | Hosts the application or server |
AWS Systems Manager | Provides centralized instance management |
Session Manager | Establishes interactive sessions |
SSM Agent | Communicates with Systems Manager |
AWS IAM | Controls user and instance permissions |
Amazon CloudWatch Logs | Stores session data |
Amazon S3 | Provides session log storage |
AWS CloudTrail | Records relevant AWS API activity |
Two IAM permission layers are especially important.
Instance permissions: The EC2 instance needs permissions to communicate with Systems Manager. The
AmazonSSMManagedInstanceCore managed policy is commonly attached through an EC2 IAM instance profile.User permissions: Administrators need appropriate IAM permissions to start sessions and access the intended instances.
Remember: Instance permissions and administrator permissions serve different purposes.
5. Hands-On: Configure Session Manager for EC2
Let's configure Session Manager using the AWS Management Console.
Step 1: Launch an EC2 Instance
- Open the AWS Management Console.
- Navigate to Amazon EC2.
- Select Launch Instance.
- Choose an Amazon Linux AMI.
- Select an appropriate instance type.
- Configure your VPC and subnet.
- Create or select a security group.
For this demonstration, use an instance in a private subnet if possible.
Security group configuration:
- Do not add an inbound SSH rule for Session Manager.
- Do not add an inbound RDP rule for Session Manager.
- Ensure the instance has the required outbound connectivity.
Step 2: Configure IAM Permissions
The EC2 instance needs an IAM role to communicate with AWS Systems Manager.
- Open the IAM console.
- Navigate to Roles.
- Select Create role.
- Choose AWS Service as the trusted entity.
- Select EC2 as the use case.
- Attach the AWS managed policy:
AmazonSSMManagedInstanceCore - Name the role:
EC2-SessionManager-Role - Create the role.
Now attach this role to your EC2 instance.
Navigate to:
EC2 → Instances → Select Instance → Actions → Security → Modify IAM Role.
Select the newly created role and save the changes.
Step 3: Verify SSM Agent
SSM Agent is responsible for communicating with AWS Systems Manager.
Many AWS-provided AMIs already include the agent.
For Amazon Linux, check its status:
sudo systemctl status amazon-ssm-agentIf it is installed but inactive, start it:
sudo systemctl start amazon-ssm-agentEnable automatic startup:
sudo systemctl enable amazon-ssm-agentThe service name and installation procedure may differ depending on the operating system.
Step 4: Configure Network Connectivity
This is one of the most important steps.
Although Session Manager does not require inbound SSH access, SSM Agent must communicate with the required AWS Systems Manager endpoints.
For private instances, there are two common approaches.
Option A: NAT Gateway
A NAT Gateway provides an outbound network path for instances in private subnets to reach required AWS service endpoints.
Option B: VPC Interface Endpoints
AWS PrivateLink allows instances to communicate with supported AWS services through private network connectivity.
Common Systems Manager endpoints include:
-
com.amazonaws.<region>.ssm
-
com.amazonaws.<region>.ssmmessages
-
com.amazonaws.<region>.ec2messageswhere required by the Region and agent configuration.
Ensure that DNS resolution, endpoint security groups, and outbound HTTPS connectivity on TCP port 443 are configured correctly.
Step 5: Connect Through the AWS Console
Now let's establish our first session.
- Open the Amazon EC2 console.
- Navigate to Instances.
- Select your EC2 instance.
- Click Connect.
- Select Session Manager.
- Click Connect.
If the configuration is correct, an interactive terminal opens in your browser.
Try these commands:
whoamihostnameuptimedf -hYou can now manage your instance without opening inbound SSH port 22.
Step 6: Connect Using AWS CLI
You can also start a session using AWS CLI.
First, configure your AWS CLI credentials and install the Session Manager plugin on your local machine.
Then execute:
aws ssm start-session \ --target i-xxxxxxxxxxxxxxxxx \ --region ap-south-1Replace the instance ID with your actual EC2 instance ID.
This starts an interactive session with the selected managed instance.
6. Session Manager vs Traditional SSH
Feature | Traditional SSH | Session Manager |
|---|---|---|
Connection | Direct SSH | Systems Manager channel |
Inbound port 22 | Usually required for direct SSH | Not required |
Public IP | Not always required | Not required |
Bastion host | May be required | Not required |
Access control | SSH authentication and network controls | IAM policies |
Session logging | Requires additional configuration | Integrated logging options |
AWS CLI support | SSH client | AWS CLI and plugin |
Session Manager simplifies administrative access, especially when managing multiple instances across different environments.
7. Security Best Practices
Session Manager improves the way administrators access infrastructure, but it does not automatically make an environment completely secure.
Follow these practices:
1. Apply least-privilege IAM permissions
Allow users to access only the instances they need. Use IAM policies and resource tags to control access.
2. Remove unnecessary inbound SSH rules
If your environment uses Session Manager exclusively for administration, remove unnecessary inbound SSH access.
3. Enable session logging
Configure session logging to Amazon S3 or CloudWatch Logs when command-level auditing is required.
Important: Session Manager logging is not available for SSH or port-forwarding sessions because Session Manager acts as a tunnel for that encrypted traffic.
4. Use encryption
Configure AWS KMS encryption for session data and stored logs where appropriate.
5. Keep SSM Agent updated
Automate SSM Agent updates to maintain compatibility and security.
6. Use VPC endpoints for private workloads
Consider AWS PrivateLink endpoints to avoid requiring public internet connectivity for Systems Manager communication.
7. Monitor administrative activity
Use CloudTrail to monitor relevant API activity. EventBridge and SNS can also be configured to notify administrators about session start and stop events.
8. Common Problems and Troubleshooting
Problem | Possible solution |
|---|---|
Instance does not appear as managed | Verify SSM Agent and IAM instance profile |
SSM Agent is offline | Check outbound HTTPS and endpoint connectivity |
Access denied | Review administrator IAM permissions |
Session cannot start | Check instance registration and SSM connectivity |
Private instance cannot connect | Verify VPC endpoints, DNS, and endpoint security groups |
One important point: Attaching an IAM role alone is not sufficient. The instance must also have the required network connectivity to Systems Manager.
9. Real-World Use Case
Imagine an organization managing 50 EC2 instances across development, testing, and production environments.
With traditional SSH access, administrators may need to manage private keys, configure security groups, and maintain bastion hosts.
As the infrastructure grows, these responsibilities become more difficult.
By adopting Session Manager, the organization can:
- Centralize administrative access through IAM.
- Avoid exposing inbound SSH ports.
- Access private EC2 instances.
- Configure centralized session logging.
- Reduce the operational overhead of managing bastion infrastructure.
Organizations can also use IAM conditions and resource tags to restrict which instances each team can access.
10. Conclusion
Cloud security is not only about protecting applications from external attacks. It is also about controlling how administrators access and manage infrastructure.
AWS Systems Manager Session Manager provides a practical alternative to traditional SSH-based server management.
By combining IAM, SSM Agent, secure communication channels, and centralized logging, organizations can simplify EC2 administration while reducing the need for inbound management ports.
The key takeaway is simple: You do not always need to expose a server to manage it. Sometimes, the better approach is to bring secure management to the server.
If you are learning AWS, try this hands-on experiment: launch an EC2 instance, configure Session Manager, remove the inbound SSH rule, and connect through the AWS console.
You will gain practical experience with IAM, networking, EC2, and AWS Systems Manager through one exercise.
References and Further Learning
Author: Meghaa Manickam
Topics: AWS | Cloud Security | Amazon EC2 | Systems Manager | DevOps
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article