Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS SSM Direct connection #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

theomega
Copy link

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.

@theomega
Copy link
Author

@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...

@theomega
Copy link
Author

I updated the PR with more content for the README.md, thanks for the suggestion.

@boris-smidt-klarrio
Copy link
Contributor

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.

@boris-smidt-klarrio
Copy link
Contributor

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.

@maruu
Copy link

maruu commented May 29, 2024

@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)

@flaupretre
Copy link
Owner

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

@theomega
Copy link
Author

theomega commented Jun 3, 2024

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.

@flaupretre
Copy link
Owner

@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

@jack-at-circle
Copy link

Hi all, I just accidentally re-implemented this before finding this pr. It would be super helpful if this got merged!

Comment on lines +1 to +4
aws ssm start-session \
--target $TUNNEL_GATEWAY_HOST \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters "{\"host\":[\"$TUNNEL_TARGET_HOST\"], \"portNumber\":[\"$TUNNEL_TARGET_PORT\"], \"localPortNumber\":[\"$TUNNEL_LOCAL_PORT\"]}" &
Copy link

@jack-at-circle jack-at-circle Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
aws ssm start-session \
--target $TUNNEL_GATEWAY_HOST \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters "{\"host\":[\"$TUNNEL_TARGET_HOST\"], \"portNumber\":[\"$TUNNEL_TARGET_PORT\"], \"localPortNumber\":[\"$TUNNEL_LOCAL_PORT\"]}" &
aws ssm start-session $TUNNEL_SSM_OPTIONS \
--target $TUNNEL_GATEWAY_HOST \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters "{\"host\":[\"$TUNNEL_TARGET_HOST\"], \"portNumber\":[\"$TUNNEL_TARGET_PORT\"], \"localPortNumber\":[\"$TUNNEL_LOCAL_PORT\"]}" &

I just hit an issue with this opening a connection to an instance in us-west-2 while doing a deployment in us-east-1. It would be great to include support for the ssm_options like the current ssm tunnel does so this works cross-region etc

@@ -0,0 +1,5 @@
aws ssm start-session \
--target $TUNNEL_GATEWAY_HOST \
--document-name AWS-StartPortForwardingSessionToRemoteHost \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be updated to use $TUNNEL_SSM_DOCUMENT_NAME please?

so that it'll be possible to specify custom document if needed.

@sashasimkin
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants