Sub topic 12
Running SSH through Session Manager
This chapter assumes you can start a basic Session Manager shell, as covered in the previous chapters. You also need the Session Manager plugin for the AWS CLI and an OpenSSH client. The instance must be online in Systems Manager and must use a key pair that you hold. SSH over Session Manager (SSM) gives you the SSH protocol without opening port 22 to the network.
Why tunnel SSH through Session Manager
A plain Session Manager shell is enough for most administration. SSH is still useful when you need tools that speak SSH, such as scp, sftp, rsync, or SSH port forwarding. Tunneling lets you keep those tools and remove the inbound rule for port 22 from the security group.
The tunnel works because the SSM document AWS-StartSSHSession connects a Session Manager stream to the SSH daemon on the instance. The daemon listens on the instance's own port 22. The instance does not need that port to be reachable from the network.
Configure the SSH client
Add a Host entry to ~/.ssh/config that matches instance IDs. SSH runs the ProxyCommand instead of opening a TCP connection itself.
Host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"SSH replaces %h with the host name you typed (the instance ID) and %p with the port, which is 22 by default. The mi-* pattern covers managed on-premises nodes.
Connect
Use the instance ID in place of an IP address. The key pair and login user are the same as for a normal SSH connection.
ssh -i ~/.ssh/my-key.pem ec2-user@i-0123456789abcdef0File transfer works through the same entry:
scp -i ~/.ssh/my-key.pem report.txt ec2-user@i-0123456789abcdef0:/tmp/What changes in permissions
Two layers of access control now apply, and both must succeed.
| Layer | What it checks | Where you configure it |
|---|---|---|
| Session Manager | Your IAM identity may call `ssm:StartSession` on the instance and on the `AWS-StartSSHSession` document | IAM policy |
| SSH | Your private key matches a public key in the login user's `authorized_keys` | Key pair on the instance |
The IAM policy from the earlier chapter allows only the SSM-SessionManagerRunShell document. For SSH tunneling, add the ARN of the AWS-StartSSHSession document to the Resource list of the ssm:StartSession statement. Document ARNs have the form arn:aws:ssm:us-east-1::document/AWS-StartSSHSession.
Common errors
TargetNotConnected: The instance is not online in Systems Manager. Check the SSM Agent, the instance profile, and the network path to the Systems Manager endpoints.AccessDeniedExceptiononStartSession: Your IAM policy does not cover theAWS-StartSSHSessiondocument or the instance.Permission denied (publickey): The tunnel works, but SSH authentication failed. Check the key file, its permissions (chmod 400), and the login user.session-manager-pluginnot found: Install the plugin on the machine where you run thesshcommand.
Exercise
You can run aws ssm start-session --target i-0123456789abcdef0 successfully, but ssh ec2-user@i-0123456789abcdef0 fails with an AccessDeniedException from StartSession. What is the likely cause, and how do you fix it?
Show answer
The SSH config entry uses the AWS-StartSSHSession document, and your IAM policy allows only SSM-SessionManagerRunShell. Add arn:aws:ssm:us-east-1::document/AWS-StartSSHSession (using your Region) to the Resource list of the ssm:StartSession statement.
Exercise
Why can you remove the inbound port 22 rule from the security group and still use SSH over Session Manager?
Show answer
The SSM Agent makes an outbound HTTPS connection to Systems Manager. The SSH stream travels inside that connection and the agent hands it to sshd on localhost, so no inbound connection to port 22 is needed.