Attach the IAM role to your EC2 instance

Attach the IAM role to your EC2 instance

You launched the instance in Step 1 and created the IAM role in Step 2. Now we attach that role to the instance, so both the CloudWatch agent (Step 5) and AWS Systems Manager can use it — and so the aws sts get-caller-identity command that failed in Step 1 finally succeeds.

  1. Open the EC2 console, select Instances, check the box next to your ec2-rightsizing-lab instance, then select Actions > Security > Modify IAM role.

Select the instance, then Actions, Security, Modify IAM role

  1. Choose the role you created in Step 2 (EC2-CloudWatch-Agent-Role) from the dropdown, then select Update IAM role.

Choose EC2-CloudWatch-Agent-Role and select Update IAM role

A reboot is not required — it typically takes under a minute for the instance metadata service to reflect the new role.

Verify it worked, right from the terminal

  1. Go back to your EC2 Instance Connect session from Step 1 (or open a new one via Connect), and re-run the same command that failed earlier:
aws sts get-caller-identity
  1. This time it should return your account ID, user ID, and the role’s ARN instead of a credentials error — proof the instance is now picking up temporary credentials automatically from the attached IAM role, with no access key ever stored on the machine.

aws sts get-caller-identity now returns the account, user ID, and role ARN

Confirm the instance registers with Systems Manager

The AmazonSSMManagedInstanceCore policy in your role also lets AWS Systems Manager manage this instance — that’s what Step 5 relies on to install the CloudWatch agent with no SSH needed. Let’s confirm it registered.

  1. Wait 2–3 minutes after attaching the role, then use the search bar and type System Manager to open the AWS Systems Manager console, making sure you’re still in the same Region as your instance.

Search for the Systems Manager console

  1. In the left navigation pane, under Node Tools, select Fleet Manager, then select Get started.

Open Fleet Manager and select Get started

  1. Your instance should appear under Managed Nodes with a Ping status of Online.

Instance shown as a managed node with Ping status Online

If it’s not there after a few minutes, check in this order: the IAM role (Security tab on the instance must show AmazonSSMManagedInstanceCore attached), the instance’s public IP (Details tab — this should already be set from Step 1), the Region selector (must match between the EC2 and Systems Manager consoles), and finally, as a last resort, connect via EC2 Instance Connect and run sudo systemctl status amazon-ssm-agent to confirm the agent itself is running.

Troubleshooting: “i/o timeout” connecting to the SSM endpoint

Having a public IP is not enough by itself — the instance’s subnet must also have a route table that sends internet-bound traffic to an Internet Gateway. If you check the SSM Agent log on the instance (sudo cat /var/log/amazon/ssm/amazon-ssm-agent.log) and see an error like the one below, it means the agent resolved the Systems Manager endpoint but couldn’t open a TCP connection to it — a routing problem, not a permissions problem:

SSM Agent unable to acquire credentials: <error>unexpected error getting instance profile role credentials
or calling UpdateInstanceInformation ... RequestError: send request failed
caused by: Post "https://ssm.<region>.amazonaws.com/": dial tcp <ip>:443: i/o timeout</error>

This shouldn’t happen if you launched in the default VPC as described in Step 1, since default VPCs come with an Internet Gateway and public route already configured. But if you used a custom VPC, here’s how to fix it:

  1. Open the VPC console, select Subnets, select the subnet your instance is running in, and check the Route table tab.
  2. Confirm there is a route with Destination 0.0.0.0/0 pointing to a Target starting with igw- (an Internet Gateway). If instead the route table only has the local VPC route, that’s the problem — the subnet is actually a private subnet with no path to the internet.
  3. If there’s no Internet Gateway attached to the VPC at all, go to Internet gateways, create one (or select the existing one), and Attach to VPC.
  4. Go to Route tables, select the route table associated with your subnet, choose Edit routes, and Add route: Destination 0.0.0.0/0, Target the Internet Gateway you just attached.
  5. Double-check the subnet also has Auto-assign public IPv4 address enabled (Subnet actions > Edit subnet settings), and that the instance’s security group allows outbound HTTPS (port 443) — the default outbound rule does, unless it was modified.
  6. The SSM Agent retries periodically — give it 2–3 minutes after fixing routing, or reboot the instance to force an immediate retry.

Attaching an IAM role, calling the AWS CLI, and registering with Systems Manager are all free — none of this consumes Free Tier hours or generates a charge on their own.