I'm attempting to write a wrapper script to start an ssm session in a bash prompt using the following:
echo '/bin/bash' | aws ssm start-session --target $instance_id
When I do this, I get an infinite loop of bash prompts and must terminate the process externally. The session is also left open and must be terminated manually with aws ssm terminate-session Similarly, if I pipe something like echo hello, I see "hello" printed, but then get the infinite loop of shell prompts.
Only echo 'exit' | aws ssm start-session doesn't exhibit this behavior, instead exiting immediately.
@phene The pipe is closed while ssm is not so that's causing problems. Luckily this works: https://stackoverflow.com/a/5852578/2115135
(echo '/bin/bash' && cat) | aws ssm start-session --target $instance_id
FYI, that works great, just don't ever try to exit the shell with ctrl+d, otherwise you end up with the infinite loop of prompts that I described before.
Exiting the shell without the command piping using ctrl+d works as normal.
@phene Yeah, that probably could be fixed by killing the aws cli on the client side after the cat command exits but I don't have a ready snippet for that one. Or you could try expect.
This seems to work in the case of ctrl-d:
(echo '/bin/bash' && cat && echo 'exit' && echo 'exit') | aws ssm start-session --target $instance_id
Both exits are needed (1 for /bin/bash, the other for /bin/sh). The problem is other control commands are totally broken. If I run something like top after starting the session, exiting with ctrl-c gives me the infinite prompt issue again. My guess is that cat is receiving and processing the ctrl-c rather than top.
Thank you for the feedback! We will look into this.
I was able to get the ctrl-c stuff to work along with other items by adding stty raw -echo ; prior to the (...) and then after the aws ssm start-session... I add ; reset to reset my tty. The only issue I am really running into is the cat command hanging and having to hit a key.
You can now use Shell Profile to start in bash. https://aws.amazon.com/blogs/mt/bring-your-own-cli-session-manager-configurable-shell-profiles/
Closing.
Most helpful comment
@phene The pipe is closed while ssm is not so that's causing problems. Luckily this works: https://stackoverflow.com/a/5852578/2115135
(echo '/bin/bash' && cat) | aws ssm start-session --target $instance_id