I find that, while stop and pull run without issue, I get the following error on run:
paramiko.ssh_exception.SSHException: Channel is not open
I believe that the error occurs during the first check of the connect_ssm piece, which is reproduced minimally below:
@task
def ssm(
c: Context, host: str, cmd: str | None = None, local_port: str | None = None, host_port: str | None = None
) -> None:
"""
Use AWS SSM to open an SSH connection with a specific EC2 instance.
"""
if not _check_known_host(c, host):
public_key = c.run("cat ~/.ssh/<path_to_key>", hide=True).stdout.rstrip()
remote_cmd = f"mkdir -p ~/.ssh && echo {public_key} >> /home/ubuntu/.ssh/authorized_keys"
add_public_key_to_ec2_cmd = (
f"aws ssm send-command --instance-ids {host} --document-name AWS-RunShellScript "
f"--parameters commands='{remote_cmd}'"
)
c.run(add_public_key_to_ec2_cmd, hide=True)
cmd = cmd or ""
if local_port and not host_port:
return
if host_port:
cmd = f"-L {local_port or host_port}:localhost:{host_port} '{cmd}'"
c.run(f"ssh {host} -t {cmd}", pty=True)
def _check_known_host(c: Context, host: str) -> bool:
return c.run(f"ssh-keygen -F {host}", warn=True, hide=True).ok
I have tried adding c = Connection(host) to the run task as well as the ssm task, but I get errors of the following sort:
Encountered a bad command exit code!
Command: 'cat ~/.ssh/<path_to_key>'
Exit code: 1
Stdout:
Stderr:
cat: /home/ubuntu/.ssh/<path_to_key>: No such file or directory
Notably though, I can run inv pull, inv stop, and inv run in sequence with no issue.
I'm thinking that the issue has something to do with the Context, but this is where I am getting stuck -- I don't understand why these tasks work sequentially but not when chained together.
I apologize for the length of code involved in understanding this issue, but any help in the right direction would be appreciated.
Hi there, I have a set of
invoke
tasks that are, minimally, as follows:I am trying to chain these events together so that one does not need to invoke
stop
,pull
, andrun
in sequence. Let's call itupdate
.I find that, while
stop
andpull
run without issue, I get the following error onrun
:I believe that the error occurs during the first check of the
connect_ssm
piece, which is reproduced minimally below:I have tried adding
c = Connection(host)
to therun
task as well as thessm
task, but I get errors of the following sort:Notably though, I can run
inv pull
,inv stop
, andinv run
in sequence with no issue.I'm thinking that the issue has something to do with the
Context
, but this is where I am getting stuck -- I don't understand why these tasks work sequentially but not when chained together.I apologize for the length of code involved in understanding this issue, but any help in the right direction would be appreciated.