Secure Shell Access via SSM Without SSH Keys
Connect to EC2 instances securely using AWS Systems Manager Session Manager, eliminating the need for SSH keys, bastion hosts, or open inbound ports.
Prerequisites
- -AWS CLI v2 installed and configured
- -Session Manager plugin installed for the AWS CLI
- -EC2 instance with SSM Agent running and IAM instance profile attached
Steps
Verify the instance is managed by SSM
Check that the target EC2 instance appears in the SSM managed instances list, confirming the agent is running and has connectivity.
If the instance does not appear, verify the instance has an IAM role with AmazonSSMManagedInstanceCore policy and that the SSM Agent is running.
Start an interactive session
Open a shell session to the EC2 instance through SSM, tunneled over HTTPS without requiring any inbound ports.
Session Manager sessions are logged in CloudTrail and can optionally stream session output to S3 or CloudWatch for auditing.
Run a command without an interactive session
Execute a one-off command across one or more instances using SSM Run Command without opening an interactive shell.
SSM Run Command can target multiple instances at once using instance IDs, tags, or resource groups.
Get command output
Retrieve the output of a previously executed Run Command by its command ID.
Start a port forwarding session
Forward a remote port from the EC2 instance to your local machine through SSM, useful for accessing databases or internal services without VPN.
Port forwarding keeps running until you press Ctrl+C. Your local port 15432 will forward to port 5432 on the remote instance, so you can connect with tools like psql -h localhost -p 15432.
Full Script
FAQ
Discussion
Loading comments...