AWS SSM screen sessions do not persist

I'm making a script with the Javascript AWS-SDK that sends commands to an EC2 instance in order to create a screen and then execute a process in said screen. After some time, SSM is supposed to restore the previously created screen and stop the process. However, after some tests, the screens don't seem to persist when using the following sequence of commands.

// First command batch

cd /home/ec2-user/
screen -S server
run process

// Second command batch

cd /home/ec2-user/
screen -S server -X "stop process"

When using the second batch of commands, SSM returns an error saying that there are no screens or "Must be connected to a Terminal". I'm at a loss. Why can't I use a screen?


AWS Systems Manager Session Manager uses an 'Agent' to connect to the instance. Commands you type are sent to the agent, which executes the commands and returns the response.

It's not using a normal SSH connection or a 'terminal' -- it's a bit of a cheat, but a very useful cheat because it allows connections to an EC2 instance in a private subnet and can even connect to instances that block all incoming connections (because the agent connects via an Outgoing connection).

If you need to do more fancy stuff, you could use Port Forwarding Using AWS System Manager Session Manager, which allows you to establish other connections across the SSM connection (eg an SSH session or port-forwarding a web service). This would then allow you to connect using normal 'terminal' type functionality.