|
| 1 | +# aws-ssm-ec2-proxy-command (Windows) |
| 2 | + |
| 3 | +Open an SSH connection to your ec2 instances via AWS SSM without the need to open any ssh port in you security groups. |
| 4 | + |
| 5 | +#### Prerequisits |
| 6 | + |
| 7 | +* Local Setup |
| 8 | + * [Install AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) |
| 9 | + * Windows `winget install Amazon.AWSCLI` |
| 10 | + * [Install AWS CLI Session Manager Plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html) |
| 11 | + * Windows `winget install Amazon.SessionManagerPlugin` |
| 12 | +* Ensure Your IAM Permissions |
| 13 | + * [IAM Policy Example](aws-ssm-ec2-iam-policy.json) |
| 14 | + * `ssm:StartSession` for DocumentName: `AWS-StartSSHSession` and Target Instance |
| 15 | + * [AWS Documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-restrict-access-examples.html) |
| 16 | + * `ssm:SendCommand` for DocumentName: `AWS-RunShellScript` and Target Instance |
| 17 | + * [AWS Documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-rc-setting-up.html) |
| 18 | +* Target Instance Setup |
| 19 | + * [Ensure SSM Permissions](https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html) fo Target Instance Profile |
| 20 | + * Ensure SSM Agent is installed (preinstalled on all AWS Linux AMIs already) |
| 21 | + * [Install SSM Agent on Linux Instances](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-agent.html) |
| 22 | + * `yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm & service amazon-ssm-agent restart` |
| 23 | + * [SSM Agent on Windows Instances](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-win.html) |
| 24 | + |
| 25 | +#### Install SSH Proxy Command |
| 26 | + |
| 27 | +- Move proxy command script [aws-ssm-ec2-proxy-command.ps1](aws-ssm-ec2-proxy-command.ps1) to `~/.ssh/aws-ssm-ec2-proxy-command.ps1` |
| 28 | + |
| 29 | +- Ensure you are allowed to execute powershell scripts (see [Set-ExecutionPolicy](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy) command) |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +##### Setup SSH Config [optional] |
| 34 | + |
| 35 | +* Add ssh config entry for aws ec2 instances to your `~/.ssh/config`. Adjust key file path if needed. |
| 36 | + |
| 37 | +```ssh-config |
| 38 | +host i-* mi-* |
| 39 | + IdentityFile ~/.ssh/id_rsa |
| 40 | + ProxyCommand powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub |
| 41 | + StrictHostKeyChecking no |
| 42 | +``` |
| 43 | + |
| 44 | +#### Open SSH Connection |
| 45 | + |
| 46 | +* Ensure AWS CLI environemnt variables are set properly e.g. |
| 47 | + * `export AWS_PROFILE=default` or `AWS_PROFILE=default ssh ... <INSTACEC_USER>@<INSTANCE_ID>` |
| 48 | +* If default region does not match instance region you need to provide it |
| 49 | + * e.g. `<INSTACEC_USER>@<INSTANCE_ID>--<INSTANCE_REGION>` |
| 50 | + |
| 51 | +###### SSH Command with SSH Config Setup |
| 52 | + |
| 53 | +`ssh <INSTACEC_USER>@<INSTANCE_ID>` |
| 54 | + |
| 55 | +* e.g. `ssh ec2-user@i-1234567890` |
| 56 | + |
| 57 | +###### SSH Command with ProxyCommand CLI Option |
| 58 | + |
| 59 | +```powershell |
| 60 | +ssh.exe <INSTACEC_USER>@<INSTANCE_ID> ` |
| 61 | +-i "~/.ssh/id_rsa" ` |
| 62 | +-o ProxyCommand="powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub" |
| 63 | +``` |
| 64 | + |
| 65 | +## Alternative Implementation with `ec2-instance-connect` |
| 66 | + |
| 67 | +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. |
| 68 | +Instead you only grant `ec2-instance-connect:SendSSHPublicKey` permission to a specific instance user e.g. `ec2-user`. |
| 69 | + |
| 70 | +* Ensure [Prerequisits](#prerequisits) |
| 71 | +* Use this [aws-ssm-ec2-proxy-command.ps1](ec2-instance-connect/aws-ssm-ec2-proxy-command.ps1) proxy command script instead |
| 72 | +* Use this [IAM Policy Example](ec2-instance-connect/aws-ssm-ec2-iam-policy.json) instead |
| 73 | + * `ssm:StartSession` for DocumentName: `AWS-StartSSHSession` and Target Instance |
| 74 | + * [AWS Documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-restrict-access-examples.html) |
| 75 | + * `ec2-instance-connect:SendSSHPublicKey` |
| 76 | + * [AWS Documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-rc-setting-up.html) |
| 77 | + * You may need to adjust `ec2:osuser` to match your needs. Default osuser is `ec2-user` |
| 78 | +* Follow [Install Guide](#install-ssh-proxy-command) |
0 commit comments