Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
1–10 of 16 posts
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#2I’m surprised this hasn’t come up more frequently. I’d expect a lot of security products to flag it as very much looking like malware.
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#3Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#4https://gist.github.com/nicornk/5d2c0cd02179f9b46cc7df459af0...
host i-* IdentityFile ~/.ssh/id_rsa TCPKeepAlive yes ServerAliveInterval 120 User ec2-user ProxyCommand sh -c "aws ec2 start-instances --instance-ids %h ; aws ec2 wait instance-running --instance-ids %h ; aws ec2-instance-connect send-ssh-public-key --instance-id %h --instance-os-user %r --ssh-public-key 'file://~/.ssh/id_rsa.pub' --availability-zone $(aws ec2 describe-instances --instance-ids %h --query 'Reservations[0].Instances[0].Placement.AvailabilityZone') ; aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
This will also allow VSCode remote development.
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#5Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#6Funny to read this after seeing Fly.io’s recent blog about VS Code’s remote SSH agent: https://fly.io/blog/vscode-ssh-wtf/ I’m surprised this hasn’t come up more frequently. I’d expect a lot of security products to flag it as very much looking like malware.
What do you suggest as an alternative? Session Manager is IAM controlled and much more secure and easily managed than the traditional means involving direct access via SSH and opening a port through the security group for a certain IP address or a jump box server.
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#7If you just want to enable ssh to ec2 instances (through SSM) using ssh i-… you can add the following lines to your ssh config https://gist.github.com/nicornk/5d2c0cd02179f9b46cc7df459af0... host i-* IdentityFile ~/.ssh/id_rsa TCPKeepAlive yes ServerAliveInterval 120 User ec2-user ProxyCommand sh -c "aws ec2 start-instances --instance-ids %h ; aws ec2 wait instance-running --instance-ids %h ; aws ec2-instance-connect…
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#8If you just want to enable ssh to ec2 instances (through SSM) using ssh i-… you can add the following lines to your ssh config https://gist.github.com/nicornk/5d2c0cd02179f9b46cc7df459af0... host i-* IdentityFile ~/.ssh/id_rsa TCPKeepAlive yes ServerAliveInterval 120 User ec2-user ProxyCommand sh -c "aws ec2 start-instances --instance-ids %h ; aws ec2 wait instance-running --instance-ids %h ; aws ec2-instance-connect…
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#9If you just want to enable ssh to ec2 instances (through SSM) using ssh i-… you can add the following lines to your ssh config https://gist.github.com/nicornk/5d2c0cd02179f9b46cc7df459af0... host i-* IdentityFile ~/.ssh/id_rsa TCPKeepAlive yes ServerAliveInterval 120 User ec2-user ProxyCommand sh -c "aws ec2 start-instances --instance-ids %h ; aws ec2 wait instance-running --instance-ids %h ; aws ec2-instance-connect…
My variation is to use a custom script as `ProxyCommand` that resolves private route53 DNS names to instance ids, because remembering instance IDs is insane.
Re: Down the rabbit hole: Implementing SSH port forwarding over AWS Session Manager
#10I've used a combination of ProxyCommand directive in ssh config + a script it calls w/ the `%h` (host) to unpack what the correct instance-id is (like @galanwe). For the proxycommand, you can embed an `aws ec2-instance-connect send-ssh-public-key` for pushing a key valid for 60s followed by activating the SSM session.
The downside is it adds ~20-30s delay in connection due to the API requests, but if you're making repeated rapid requests to same instance, I recommend looking into ssh's ControlPath, ControlMaster and ControlPersist to keep a longer lived session that's re-used for client re-connections (ref: https://blog.scottlowe.org/2015/12/11/using-ssh-multiplexing...)
[Edit to add that I've hit the registry bug myself]