AWS Systems Manager (SSM) Sessions


A common administrative and troubleshooting task is to SSH into an EC2 instance, while this should be done minimally it does make certain adhoc operations easier but one of the hassles of this approach is that you either need to have your instance exposed to the internet, which is generally a no-no, or you need to maintain a bastion to provide access. Here’s where AWS Systems Manager (SSM) enters. Through the use of the SSM agent on an instance, you can SSH or run arbitrary commands on the instance without having it exposed to the internet and without the need of a bastion. AWS does the heavy lifting for us of managing public endpoints, the SSM API endpoints, and routing your SSH traffic from your local machine to the instance.

To get this up and running you will need to:

Possible errors during installation

There can be a few issues with the above configurations that can occur, here is a short list of some of the common ones I’ve found and how to remediate them.

export PATH=$PATH:/usr/local/sessionmanagerplugin/bin

How do we use SSM sessions?

Once you’ve successfully completed the installation and verification steps there are many SSM operations you can perform but the two I want to concentrate on are:

“SSH”-ing

aws --region ap-southeast-2 ssm start-session --target <instance id>
cat << EOF > ~/.ssh/config
# SSH over Session Manager
host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

EOF

ssh i-abcdefg12345678 # Example instance id

Port Forwarding

aws ssm start-session --target <instance_with_ssm_agent> --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters '{"portNumber":["<remote_port>"],"localPortNumber":["<local_port>"],"host":["<load_balancer_cname>"]}'

For even more information you can read this AWS Blog and this documentation.


While I’ve focused on the SSH’ing and port forwarding functionality of SSM via its session management, you can run a variety of other SSM Documents for general instance management as well, such as patching, or even creating your own custom SSM Documents for bespoke actions.

The power of the SSM agent for instance and session management is vast and I hope these examples help you think up additional ways you could also use SSM.