Sub topic 9
Preparing the instance: SSM Agent, instance profile, and endpoints
Session Manager opens no inbound port, so the instance has to be ready in three other ways. It needs software that makes outbound connections, an IAM identity to authenticate with, and a network path to the Systems Manager service. This chapter covers how to set up each one and how to diagnose the case where an instance never shows up as managed.
This chapter assumes you can launch an instance in a VPC and read a route table. It also assumes you know what an IAM role and a trust policy are, and have the AWS CLI configured. Granting users permission to start sessions is covered in the next chapter.
The three requirements
An instance becomes a managed node when all three of these are true:
- The SSM Agent is installed and running.
- The instance has an IAM role, delivered through an instance profile, that allows the agent to talk to Systems Manager.
- The agent can reach the Systems Manager endpoints over HTTPS (port 443).
If any one is missing, the instance does not appear in Systems Manager and aws ssm start-session fails with a TargetNotConnected error.
SSM Agent
The SSM Agent is a service that runs on the instance. It registers the instance with Systems Manager, then holds an outbound connection open so the service can deliver commands and session requests. This is why no inbound rule is needed: the instance always initiates the connection.
Many AWS-provided AMIs include the agent already. These include Amazon Linux 2, Amazon Linux 2023, recent Ubuntu Server releases, and current Windows Server AMIs. Check the AMI's documentation rather than assuming. Custom AMIs, older images, and most third-party Marketplace images often lack it.
To check the agent on Amazon Linux, run this on the instance:
sudo systemctl status amazon-ssm-agentOn Ubuntu images that install the agent as a snap, the service name differs:
sudo systemctl status snap.amazon-ssm-agent.amazon-ssm-agent.serviceIf the agent is missing, install it from the package for your distribution, following the AWS documentation for that OS. After installing, enable and start the service so it survives reboots:
sudo systemctl enable --now amazon-ssm-agentThe agent log is at /var/log/amazon/ssm/amazon-ssm-agent.log. When an instance fails to register, this log usually names the cause directly, such as a missing credentials error or a failed connection to an endpoint.
Keep the agent current. Some Session Manager features, including certain port forwarding documents and newer logging options, require a minimum agent version. Systems Manager can update the agent automatically through State Manager if you enable that association.
The instance profile
The agent has no IAM user and no access keys. It uses the credentials of the role attached to the instance. An EC2 instance cannot use a role directly. The role is wrapped in an instance profile, and the profile is associated with the instance. The console hides this wrapper, but the CLI makes you create both objects.
AWS provides the managed policy AmazonSSMManagedInstanceCore, which contains the permissions the agent needs for Session Manager and the other core Systems Manager features. Use it as the starting point.
First, create a trust policy that lets EC2 assume the role. Save it as trust.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}Then create the role, attach the managed policy, create the profile, and add the role to it:
aws iam create-role \
--role-name SSMInstanceRole \
--assume-role-policy-document file://trust.json
aws iam attach-role-policy \
--role-name SSMInstanceRole \
--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
aws iam create-instance-profile \
--instance-profile-name SSMInstanceProfile
aws iam add-role-to-instance-profile \
--instance-profile-name SSMInstanceProfile \
--role-name SSMInstanceRoleFinally, attach the profile to an existing instance. For a new instance, pass --iam-instance-profile Name=SSMInstanceProfile to run-instances instead.
aws ec2 associate-iam-instance-profile \
--instance-id i-0123456789abcdef0 \
--iam-instance-profile Name=SSMInstanceProfileAfter you associate a profile with a running instance, the agent does not always notice at once. It retries registration on a schedule, so the instance can take several minutes to appear. Restarting the agent (sudo systemctl restart amazon-ssm-agent) forces an immediate retry.
If the instance has already been given a role for other reasons, check that the instance metadata service is reachable. The agent reads credentials from it. Both IMDSv1 and IMDSv2 work with the agent, so requiring IMDSv2 does not break Session Manager. If you set the metadata hop limit to 1, the agent still works on the instance itself. Containers on the instance are a separate concern and may need a higher limit.
You can also skip per-instance profiles by enabling Default Host Management Configuration in Systems Manager. It supplies the agent with credentials from a service-managed role, and it suits accounts where many instances lack a profile. The per-instance profile remains the most common and most explicit approach.
Network path
The agent needs outbound HTTPS to three endpoints in the instance's Region. The name of each is <service>.<region>.amazonaws.com:
| Endpoint | Purpose |
|---|---|
| `ssm` | Registration and Systems Manager API calls |
| `ec2messages` | Message delivery for Run Command and other documents |
| `ssmmessages` | The data channel used by Session Manager itself |
ssmmessages is the one specific to interactive sessions. An instance with working ssm access but blocked ssmmessages may show as Online yet fail when you start a session.
There are two ways to provide the path.
Public route. An instance in a public subnet with a public IP and an internet gateway route can reach the public endpoints directly. An instance in a private subnet can reach them through a NAT gateway. This needs no extra setup, but all agent traffic leaves your VPC through the internet path.
VPC interface endpoints. For private subnets without internet access, create an interface endpoint for each of the three services. Traffic then stays on the AWS network. Create them with the CLI, repeating the command for ssm, ec2messages, and ssmmessages:
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Interface \
--service-name com.amazonaws.us-east-1.ssm \
--subnet-ids subnet-0123456789abcdef0 \
--security-group-ids sg-0aaaabbbbccccdddd \
--private-dns-enabledThree details commonly cause failures here:
- Endpoint security group. The security group on the endpoint must allow inbound TCP 443 from the instances, either by CIDR or by referencing the instances' security group. Without that rule, connections time out.
- Private DNS. The
--private-dns-enabledoption makes the standard hostname resolve to the endpoint's private IPs. It only works if the VPC has both DNS support and DNS hostnames enabled. - Instance egress. The instance's own security group must allow outbound 443. The default egress rule does, but a locked-down egress policy may not.
Some features need more endpoints. Writing session logs to S3 needs access to S3, usually through an S3 gateway endpoint. Streaming logs to CloudWatch Logs needs a logs interface endpoint. Encrypting sessions with a KMS key needs a kms endpoint. The logging chapter covers these in context.
Verifying registration
From your own machine, list the managed instances and their connection state:
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textPingStatus values mean the following:
| PingStatus | Meaning |
|---|---|
| Online | The agent is checking in. The instance is ready. |
| ConnectionLost | The agent stopped checking in, typically after about five minutes of silence. |
| Inactive | The instance is no longer registered. This applies to non-EC2 nodes that were deregistered. |
An instance missing from the list entirely never registered. Work through these checks in order:
- Is the agent running, and does its log show errors?
- Does the instance have a profile with the policy attached? Run
aws ec2 describe-iam-instance-profile-associations --filters Name=instance-id,Values=i-0123456789abcdef0. - Can the instance reach the endpoints? From the instance,
curl -sI https://ssm.us-east-1.amazonaws.comshould return an HTTP response, even an error status. A hang or DNS failure points at routing, endpoint security groups, or private DNS. - Are you querying the right Region and account?
describe-instance-informationonly shows the current Region.
Exercise
An instance in a private subnet has AmazonSSMManagedInstanceCore attached through its profile, the agent is running, and the VPC has interface endpoints for ssm and ec2messages. The instance shows as Online, but aws ssm start-session fails. What is missing, and what else must be true about the endpoint?
Show answer
The ssmmessages interface endpoint is missing. Session Manager uses it for the interactive data channel, so registration (via ssm) can succeed while sessions fail. Like the other endpoints, it needs private DNS enabled and a security group that allows inbound TCP 443 from the instance.