Home AWSAWS Session Manager: SSH to EC2 Without Open Ports

AWS Session Manager: SSH to EC2 Without Open Ports

by Knowledge Base Linux
AWS Session Manager secure tunnel connecting a laptop to cloud servers through a firewall, orange and blue tones

For decades, the standard way to reach a Linux server in the cloud has been SSH over port 22, usually through a bastion host in a public subnet. That model works, but it has real costs: you manage SSH key pairs, you expose port 22 to the internet (or at least to a jump box), and you maintain an extra instance just to act as a door. AWS Session Manager removes all of that. It gives you an interactive shell on your EC2 instances through the AWS Systems Manager service, with zero open inbound ports, no SSH keys, and every session authenticated by IAM and optionally logged for audit.

Browser-based AWS Session Manager shell sessions connecting to EC2 virtual machines through a managed cloud service

This guide walks you through setting up AWS Session Manager from scratch: the IAM instance profile your instances need, checking the SSM Agent, connecting from the AWS console and the AWS CLI, forwarding ports over a session, logging session activity to S3 and CloudWatch Logs, and fixing the most common connectivity problems.

Prerequisites

Before you start, make sure you have:

  • An AWS account with permission to create IAM roles and EC2 instances.
  • The AWS CLI installed and configured. If you have not set it up yet, see our guide on how to install and configure the AWS CLI on Linux.
  • An EC2 instance running Amazon Linux, Ubuntu, Debian, or RHEL. The SSM Agent is preinstalled on Amazon Linux, Ubuntu, Debian, and recent RHEL AMIs.
  • For CLI sessions, the Session Manager plugin installed on your local machine (details below).

How AWS Session Manager Works

AWS Session Manager does not connect to your instance directly. Instead, the SSM Agent installed on the instance opens an outbound HTTPS connection (port 443) to the Systems Manager endpoints in your region. When you start a session, AWS brokers a secure channel between your terminal or browser and that existing outbound connection.

Because the connection is initiated from inside the instance outward, your security group needs no inbound rules at all. Not even port 22. No public IP is required either — the instance can live in a fully private subnet as long as it can reach the SSM endpoints, either through a NAT gateway, an internet gateway (for public subnets), or VPC interface endpoints (ssm, ssmmessages, and ec2messages).

Step 1: Create the IAM Role with AmazonSSMManagedInstanceCore

The SSM Agent authenticates to AWS using an IAM instance profile. Create a role that trusts EC2 and attach the AWS managed policy AmazonSSMManagedInstanceCore, which grants the permissions the agent needs to communicate with Systems Manager:

# Create the role with an EC2 trust policy
aws iam create-role --role-name EC2SSMSessionRole \
  --assume-role-policy-document '{
    "Version": "2012-10-17",
    "Statement": [{
      "Effect": "Allow",
      "Principal": { "Service": "ec2.amazonaws.com" },
      "Action": "sts:AssumeRole"
    }]
  }'

# Attach the managed SSM policy
aws iam attach-role-policy --role-name EC2SSMSessionRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore

# Create the instance profile and add the role to it
aws iam create-instance-profile --instance-profile-name EC2SSMSessionProfile
aws iam add-role-to-instance-profile \
  --instance-profile-name EC2SSMSessionProfile \
  --role-name EC2SSMSessionRole

To use the console instead: open IAM, create a role for the EC2 use case, search for and attach AmazonSSMManagedInstanceCore, and name the role. On an already-running instance, go to EC2, select the instance, choose Actions > Security > Modify IAM role, and attach the new role. On a new instance, select the instance profile in the launch wizard under Advanced details.

Step 2: Verify the SSM Agent Is Running

On Amazon Linux, Ubuntu, and Debian, the SSM Agent ships with the AMI and starts automatically. Verify it:

sudo systemctl status amazon-ssm-agent

If the agent is missing (for example on a minimal custom image), install it. On Amazon Linux:

sudo yum install -y amazon-ssm-agent
sudo systemctl enable --now amazon-ssm-agent

On Ubuntu, recent versions also ship the agent via snap or deb; enable it the same way:

sudo snap install amazon-ssm-agent --classic
sudo systemctl enable --now snap.amazon-ssm-agent.amazon-ssm-agent

Give the instance a few minutes after attaching the IAM role, then check Fleet Manager in the AWS console (Systems Manager > Fleet Manager) — your instance should appear as a managed node.

Step 3: Start an AWS Session Manager Session from the Console

The console is the fastest way to connect:

  • Open the EC2 console, select your instance.
  • Click Connect, then choose the Session Manager tab.
  • Click Connect again to open a browser-based shell.

You land in a shell as the ssm-user account. From there you can run any command the machine allows. If you need root, sudo -i works the same as over SSH. Once you confirm sessions work, you can safely remove port 22 from the security group — a concrete step toward the hardening checklist in our EC2 hardening guide.

Step 4: Connect from the AWS CLI with the Session Manager Plugin

For daily work you will want the CLI. First install the Session Manager plugin on your workstation. On Linux, download the RPM or DEB from AWS and install it:

# Ubuntu/Debian (adjust the URL to your region and the current version)
curl -o session-manager-plugin.deb \
  "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb"
sudo dpkg -i session-manager-plugin.deb

# Verify the installation
session-manager-plugin

Then start a session by targeting the instance ID:

aws ssm start-session --target i-0abcd1234efgh5678

The plugin establishes an encrypted WebSocket channel through the SSM service to your instance and drops you into a local interactive shell. Your own IAM user needs the ssm:StartSession permission to initiate sessions — follow least privilege and scope it to the instances you manage.

Step 5: Port Forwarding over an AWS Session Manager Session

A common reason people keep SSH around is tunneling — reaching a database or internal web service through the instance. AWS Session Manager supports port forwarding natively, so SSH tunnels become unnecessary:

# Forward a local port to a port on the instance itself
aws ssm start-session \
  --target i-0abcd1234efgh5678 \
  --document-name AWS-StartPortForwardingSession \
  --parameters '{"portNumber":["3306"],"localPortNumber":["3306"]}'

Now localhost:3306 on your workstation reaches the MySQL service on the instance, tunneled through the SSM session. For a remote host reachable from the instance (for example an RDS endpoint in the same VPC), use the AWS-StartPortForwardingSessionToRemoteHost document:

aws ssm start-session \
  --target i-0abcd1234efgh5678 \
  --document-name AWS-StartPortForwardingSessionToRemoteHost \
  --parameters '{"portNumber":["5432"],"localPortNumber":["5432"],"host":["mydb.example.internal"]}'

Step 6: Log Sessions to S3 and CloudWatch Logs

One of the strongest arguments for AWS Session Manager over plain SSH is auditing. You can record full session output to Amazon S3, to CloudWatch Logs, or both. In the AWS console go to Systems Manager > Session Manager > Preferences and:

  • Check Enable session logging.
  • Choose Amazon S3 and pick a bucket, or CloudWatch Logs and pick a log group. Both are supported simultaneously.
  • Optionally enable KMS key encryption so the log stream is encrypted with your own key.
  • Save the preferences. Every session started afterwards is recorded automatically.

The IAM role on the instance needs permission to write to your chosen destination — AmazonSSMManagedInstanceCore does not include S3 write by default, so add a policy allowing s3:PutObject on your session-logging bucket or logs:CreateLogStream and logs:PutLogEvents for CloudWatch. These logs are invaluable during incident response: you can replay exactly what an operator typed during a maintenance window.

Troubleshooting AWS Session Manager Connectivity

The instance does not appear in Fleet Manager or as a session target

Work through this checklist in order:

  • IAM role missing or wrong policy: confirm an instance profile is attached and that the role includes AmazonSSMManagedInstanceCore. IAM changes can take a few minutes to propagate.
  • Agent not running: check sudo systemctl status amazon-ssm-agent. Restart it and inspect the log at /var/log/amazon/ssm/amazon-ssm-agent.log.
  • No outbound HTTPS: the instance must reach ssm.<region>.amazonaws.com, ssmmessages.<region>.amazonaws.com, and ec2messages.<region>.amazonaws.com on port 443. In a private subnet without NAT, create VPC interface endpoints for these three services (plus S3 if you log to S3, plus CloudWatch Logs if you log there).
  • Stale instance ID or region: verify you are querying the right region; aws ssm start-session uses your configured default region.

CLI fails with “SessionManagerPlugin is not found”

The plugin is a separate install from the AWS CLI. Reinstall it from the official download page and confirm session-manager-plugin runs without arguments. On Linux, also check that /usr/local/bin (or wherever it installed) is on your PATH.

Sessions connect but drop quickly

Look at CloudWatch metrics for the instance — an overloaded or out-of-memory box can kill the agent. Also check that no aggressive egress firewall (OS-level iptables or a VPC NACL) blocks the outbound WebSocket traffic after the initial handshake.

For the full list of requirements and supported operating systems, see the official Session Manager documentation.

Conclusion

AWS Session Manager replaces the entire SSH bastion architecture with a managed, IAM-authenticated, fully auditable channel. The recipe is short: attach an instance profile with AmazonSSMManagedInstanceCore, make sure the SSM Agent is running, give the instance outbound HTTPS to the SSM endpoints, and you can connect from the console or with aws ssm start-session. Add port forwarding when you need tunnels, turn on S3 or CloudWatch logging for compliance, and close port 22 for good. Your attack surface shrinks, key management disappears, and every shell session becomes an audit trail — which is exactly how production access should work.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.