terraform-ssh-tunnel
terraform-ssh-tunnel copied to clipboard
Add AWS SSM Direct connection
This PR implements a second way how to use AWS SSM to do the port forwarding. It does not use SSH but rather directly uses AWS SSM without SSH to create the tunnel. See the docs for how this is done: https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html#sessions-remote-port-forwarding
Main advantage is that this also works with containers/tasks running inside AWS ECS and thus allows much cheaper bastion hosts. Also it is way simpler than SSH.
@boris-smidt-klarrio , did you try this version, so we can confirm it actually works in the EKS use-case of yours as well? I'll edit the README and add your suggestions, thanks...
I updated the PR with more content for the README.md, thanks for the suggestion.
I had another try at using this script and it seems i have a race condition in this script with the Kubernetes provider. I.e. terraform continues its connection before the script really has a working port set up.
Update it seems that we have to use the module.kubernetes_tunnel.host otherwise the script will not wait for the ssm command. I suppose i should make a pr that this script waits on both port and host untill the port forwarding script ran.
@theomega @boris-smidt-klarrio Any plans to merge this? I just tried it out by patching my local module and it works as expected. Whereas the "normal" ssm-ssh approach did not work for me. (I got a connection refused for the local tunnel endpoint, but no further error message from the module itself)
Hi @theomega, thanks for this PR. After looking at this code, it seems much simpler and smarter than the current 'ssm' code. That's why I wonder if it could just just replace the current 'ssm' implementation. In other words, is there any interest to keep 'ssm', once we add 'ssm_direct'. If you think it can replace it, can you please modify your PR accordingly and include the profile and assumed role optional parameters which were recently added to the 'ssm' code ? Thanks
I'm happy to do this modification, just before I do that: The ssm_direct solution requires that the SSM Agent on the Node (i.e. EC2-Machine, K8s Pod or ECS task) is at least version 3.1.1374.0 according to the docs. This version was released on 2022-05-02 according to the Github releases.
I'm unsure how bad it is to require this version that is more than two years old. I leave it to you to decide if you want to accept this.
@theomega , requiring a version released more than two years ago seems completely acceptable to me, We just need to document it correctly in the readme file. Please feel free to proceed. Thanks
Hi all, I just accidentally re-implemented this before finding this pr. It would be super helpful if this got merged!
I also was about to re-implement this and send a PR :)
The reason for me is a little different - right now using SSM tunnel still requires the SSH key installed on the bastion host that you proxy the connection through. The direct method uses SSM built-in magic to do this with temporary keys.