The Secure Shell protocol (SSH) is one of the most widespread ways of managing remote servers securely across many platforms; by default it uses TCP port 22. Its popularity comes from the fact that it provides a cryptographically secure connection over which a user can authenticate, run commands, and pass input and output to and from the remote server, even across an insecure network (the internet).
For many new AWS users, SSH is the primary way of accessing compute services such as EC2 or Lightsail. After longer use, however, or after stumbling across news such as “Hackers hijacked my account to mine bitcoin, resulting in a $40,000 monthly bill”, you may come to see managing private keys and leaving inbound port 22 open in the security group as an unnecessary risk.
How can you connect to Amazon EC2 using AWS SSM Session Manager?
AWS Systems Manager (SSM) is a service that lets you view and manage the infrastructure running in your account. Among its many capabilities, Session Manager gives you secure and auditable access to your machines through the console or the AWS CLI, without having to open inbound ports, maintain bastion hosts or manage SSH keys.
The steps below describe how to connect to an EC2 instance running Linux using a session. The article assumes that you already have such an instance, running with inbound port 22 open.
1. Create and attach the required EC2 instance role
Create an IAM role with the AmazonSSMManagedInstanceCore policy, and attach the role to your EC2 instance. This grants the permissions for communication between the instance and the Systems Manager API: the AmazonSSMManagedInstanceCore policy allows the instance to use the core functions of AWS Systems Manager.
There is a more convenient option now: by turning on the Default Host Management Configuration setting, Systems Manager automatically applies a default IAM role, per region, to every EC2 instance in the account, so you don’t have to create and attach an instance profile by hand. The requirement is that the instance uses IMDSv2 and runs SSM Agent version 3.2.582.0 or later.
2. Check that the SSM Agent is running on your instance
Let’s check that the latest SSM Agent is installed and running on the EC2 instance we want to log in to with Session Manager. Log in to the Linux machine over SSH using the method already set up, or use the EC2 Instance Connect service in the AWS console. Once you’re in, continue with the steps below.
The SSM Agent comes preinstalled by default on AWS-provided AMIs of, among others, Amazon Linux 2 and Amazon Linux 2023, Ubuntu Server LTS releases, SUSE Linux Enterprise Server 15.3+, AlmaLinux, macOS and Windows Server. The preinstalled version isn’t necessarily the latest, so AWS recommends updating the agent regularly. If you do need to install it on Amazon Linux 2 or Amazon Linux 2023, use the following command:
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
Or, for an ARM-based (Graviton, e.g. T4g) instance:
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_arm64/amazon-ssm-agent.rpm
To check that the installed agent is working:
sudo systemctl status amazon-ssm-agent
If we see that it isn’t running, just start it:
sudo systemctl start amazon-ssm-agent
3. Changing the security group’s outbound rules
The EC2 instance’s security group has to allow outbound HTTPS traffic (port 443), so that the SSM Agent can reach the required AWS endpoints, e.g. ssm.region.amazonaws.com and ssmmessages.region.amazonaws.com. Alternatively, you can set up VPC interface endpoints.
Edit your instance’s security group so that it allows outbound HTTPS on port 443. At this point, delete all inbound rules from the security group, so that every inbound port is closed.
4. Installing Session Manager on your own machine
You run the aws ssm commands with the AWS CLI from your own machine’s terminal, but to start sessions you also have to install the Session Manager plugin separately, alongside the CLI. The plugin requires AWS CLI version 1.16.12 or later. You can find the steps for installing AWS CLI v2 here.
5. Finally, create a session to connect to the instance
Look up your EC2 instance’s ID in the AWS console, and start a session to the instance with the aws ssm start-session command. When configuring the AWS CLI credentials, make sure you specify the region where the instance is running. For this, you can also create a profile set to a region other than the default.
The following command uses the CLI profile named “frankfurt” (which is set to the eu-central-1 region) to start a session to the instance with the ID i-092c4481dc1f3gc6g.
aws ssm start-session --target i-092c4481dc1f3gc6g --profile frankfurt
Note: The SSM Agent creates a user account called ssm-user on Linux, from which we can switch to root (or another user) with sudo or su. By default, sessions start as this account.
Voilà! We have started a session and connected securely to our EC2 instance without using private keys or leaving any port open on the instance’s network firewall.
Alternative: EC2 Instance Connect Endpoint
If you still need SSH (or RDP for Windows), in June 2023 AWS introduced the EC2 Instance Connect Endpoint. Through an endpoint created in the VPC, it lets you connect to the instance’s private address without a public IP address or a bastion host; according to the documentation, the VPC doesn’t even need direct internet connectivity for this. Unlike with Session Manager, here you have to allow inbound traffic from the endpoint in the instance’s security group, and you can control access with IAM policies.
We hope you find this information useful. Thank you for your time!