Open an SSH connection to your ec2 instances via AWS SSM without the need to open any ssh port in you security groups.
ⓘ Unix users please refere to README.md
- Local Setup
- Install AWS CLI
- Windows
winget install Amazon.AWSCLI
- Windows
- Install AWS CLI Session Manager Plugin
- Windows
winget install Amazon.SessionManagerPlugin
- Windows
- Install AWS CLI
- Ensure Your IAM Permissions
- IAM Policy Example
ssm:StartSessionfor DocumentName:AWS-StartSSHSessionand Target Instancessm:SendCommandfor DocumentName:AWS-RunShellScriptand Target Instance
- Target Instance Setup
- Ensure SSM Permissions fo Target Instance Profile
- Ensure SSM Agent is installed (preinstalled on all AWS Linux AMIs already)
- Install SSM Agent on Linux Instances
yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm & service amazon-ssm-agent restart
- SSM Agent on Windows Instances
- Install SSM Agent on Linux Instances
-
Move proxy command script aws-ssm-ec2-proxy-command.ps1 to
~/.ssh/aws-ssm-ec2-proxy-command.ps1 -
Ensure you are allowed to execute powershell scripts (see Set-ExecutionPolicy command)
Unfortunately on Windows is not possible to show output while running ProxyCommand, script output is interpreted as SSH banner which is available with SSH verbose options.
- Add ssh config entry for aws ec2 instances to your
~/.ssh/config. Adjust key file path if needed.
host i-* mi-*
IdentityFile ~/.ssh/id_rsa
ProxyCommand powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub
StrictHostKeyChecking no- Ensure AWS CLI environemnt variables are set properly e.g.
export AWS_PROFILE=defaultorAWS_PROFILE=default ssh ... <INSTACEC_USER>@<INSTANCE_ID>
- If default region does not match instance region you need to provide it
- e.g.
<INSTACEC_USER>@<INSTANCE_ID>--<INSTANCE_REGION>
- e.g.
ssh.exe <INSTACEC_USER>@<INSTANCE_ID>
- e.g.
ssh.exe ec2-user@i-1234567890
ssh.exe <INSTACEC_USER>@<INSTANCE_ID> `
-i "~/.ssh/id_rsa" `
-o ProxyCommand="powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub"The advantage from security perspective it that you don't need to grant ssm:SendCommand to users and there by the permission to execute everything as root.
Instead you only grant ec2-instance-connect:SendSSHPublicKey permission to a specific instance user e.g. ec2-user.
- Ensure Prerequisits
- Use this aws-ssm-ec2-proxy-command.ps1 proxy command script instead
- Use this IAM Policy Example instead
ssm:StartSessionfor DocumentName:AWS-StartSSHSessionand Target Instanceec2-instance-connect:SendSSHPublicKey- AWS Documentation
- You may need to adjust
ec2:osuserto match your needs. Default osuser isec2-user
- Follow Install Guide