Sub topic 9
Preparing the instance: SSM Agent, instance profile, and endpoints
This chapter assumes you know what an EC2 instance, an AMI, and a VPC subnet are, and that you understand IAM roles and policies. You also need the AWS CLI configured with credentials. Session Manager only works when the instance meets three requirements. This chapter covers each one and how to check it.
The three requirements
An instance appears in Session Manager only when all of these are true:
- The SSM Agent is installed and running on the instance.
- The instance has an IAM instance profile that allows the agent to talk to Systems Manager.
- The instance can reach the Systems Manager service endpoints over HTTPS (port 443).
The agent opens an outbound connection to AWS, so you do not need an inbound rule on the security group. The agent needs to be able to reach AWS, though.
Requirement 1: the SSM Agent
SSM Agent comes preinstalled on Amazon Linux 2, Amazon Linux 2023, recent Ubuntu AMIs, and Windows Server AMIs published by AWS. For other AMIs, install it yourself. Check the AMI documentation for your operating system, because the install method differs.
On a system you can already reach, check that the agent is running:
sudo systemctl status amazon-ssm-agentOn Ubuntu images that install the agent as a snap, the service name is snap.amazon-ssm-agent.amazon-ssm-agent instead.
Requirement 2: the instance profile
The agent needs credentials to call Systems Manager. It gets them from an IAM role attached to the instance through an instance profile. The simplest choice is the AWS managed policy AmazonSSMManagedInstanceCore.
The commands below create a role, attach the policy, and associate it with an instance. The instance ID is an example, so replace it with yours.
cat > trust.json <<'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}
EOF
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 SSMInstanceRole
aws ec2 associate-iam-instance-profile \
--instance-id i-0123456789abcdef0 \
--iam-instance-profile Name=SSMInstanceProfileThe console creates the instance profile for you when you create a role for EC2. The CLI does not, so you must create it as shown.
Requirement 3: a network path to the endpoints
The agent connects to three endpoints in your Region: ssm, ssmmessages, and ec2messages. It can reach them in two ways:
| Path | Use when | Notes |
|---|---|---|
| Internet gateway with a public IP, or a NAT gateway | The subnet already has internet access | No extra setup. Outbound HTTPS on port 443 must be allowed. |
| VPC interface endpoints | The subnet is private and has no internet route | Create one endpoint for each of the three services. |
For endpoints, the endpoint's security group must allow inbound HTTPS (port 443) from the instance. Enable private DNS on each endpoint so the standard service names resolve to private addresses. The VPC also needs DNS support and DNS hostnames enabled.
Verifying readiness
After a few minutes, the instance should register with Systems Manager. Check its status:
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textOnline means the instance is ready. If the instance is missing from the list or shows ConnectionLost, work through the three requirements in order.
Common errors
- The instance never appears: the profile is missing, or it lacks the permissions in
AmazonSSMManagedInstanceCore. - The instance appears in the console but a session fails to start: the
ssmmessagesendpoint is unreachable, which is a common gap in private subnets. - Only some endpoints exist in a private subnet: create all three.
Exercise
An instance in a private subnet with no NAT gateway has the correct instance profile and a running SSM Agent, but it never shows up in describe-instance-information. What should you add?
Show answer
Create VPC interface endpoints for ssm, ssmmessages, and ec2messages in the VPC. Enable private DNS on them, and let their security group accept inbound HTTPS (port 443) from the instance. This gives the agent an outbound path to Systems Manager without an internet route.