هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- The instance needs three things:
| Need | How |
|---|---|
| SSM Agent | Installed on Amazon Linux 2023 and the official Ubuntu AMIs; on RHEL install the amazon-ssm-agent package from AWS. |
| An instance profile (IAM role) | With the managed policy AmazonSSMManagedInstanceCore. |
| Outbound HTTPS (443) to SSM | Through a NAT gateway — or, for private subnets, VPC endpoints ssm, ssmmessages, ec2messages. |
aws ec2 associate-iam-instance-profile --instance-id i-0abc1234def567890 --iam-instance-profile Name=ec2-ssm-core- Your laptop needs AWS CLI v2 and the Session Manager plugin (a small package from AWS), and IAM rights for
ssm:StartSessionon that instance. - Open a shell (quick answer). You start as
ssm-user;sudo -ifor root if your role allows it. - Port forwarding — reach a private database through the instance, no port open anywhere:
aws ssm start-session --target i-0abc1234def567890 --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters host=shop-db.cluster-abc.eu-central-1.rds.amazonaws.com,portNumber=5432,localPortNumber=15432
psql -h 127.0.0.1 -p 15432 -U app shop- Need real SSH (scp, Ansible)? Two good options without public port 22:
- EC2 Instance Connect Endpoint — SSH to private instances with a short-lived key:
aws ec2-instance-connect ssh --instance-id i-0abc1234def567890 --connection-type eice- SSH over Session Manager (a
ProxyCommandin~/.ssh/config), so tools like Ansible work.
- Log every session — Session Manager preferences: send session logs to CloudWatch Logs or S3 (encrypted). Then close port 22 in the security group:
aws ec2 revoke-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 22 --cidr 0.0.0.0/0Check that it worked
describe-instance-information shows the instance as Online, start-session gives a shell, the security group has no inbound 22, and the session appears in Systems Manager → Session Manager → Session history.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Instance not in describe-instance-information | No agent, no instance profile, or no way out to SSM. | Check the role, the agent (systemctl status amazon-ssm-agent / the snap on Ubuntu) and 443 outbound or VPC endpoints. |
SessionManagerPlugin is not found | Plugin missing on your computer. | Install the Session Manager plugin, open a new terminal. |
AccessDeniedException … ssm:StartSession | Your IAM role may not start sessions on that instance. | Ask for ssm:StartSession on that instance (or tag-based). |
| Port forward works, the database refuses | RDS security group does not allow the instance. | Allow the instance's security group on 5432 in the RDS group. |
| Session ends after some minutes idle | Idle timeout (default 20 min). | Change it in Session Manager preferences. |
| Agent was "Online", now "ConnectionLost" | Instance stopped, role removed, or endpoint/NAT change. | Check the instance state, role and network path. |
Go further
- Command pages: aws, psql, ssh.
- Next how-tos: Log in to AWS with SSO, Open a port in the firewall, Port-forward to a Kubernetes service.
- Full guides: AWS CLI, Install and harden the OpenSSH server. The same idea in Azure: Azure Bastion.
- At work: The aws CLI in daily DevOps.
1Which IAM policy does an EC2 instance need for Session Manager?
AmazonSSMManagedInstanceCore (in its instance profile).
2How do you reach a private RDS database from your laptop without opening any port?
Session Manager port forwarding with the AWS-StartPortForwardingSessionToRemoteHost document.