Home > Net >  boto3 SSM: sending multiple commands in sequence
boto3 SSM: sending multiple commands in sequence

Time:07-08

TLDR: What is the best way to send tens of commands to a remote instance using ssm, in sequence, such that I can get the output of each command before moving to the next one?

This question (How to SSH and run commands in EC2 using boto3?) asks how to run SSH commands using boto3 on a remote instance. The top voted answer explains how one can use the boto3 ssm send_command function to do so:

resp = client.send_command(
  DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
  Parameters={'commands': commands},
  InstanceIds=instance_ids,
)

However, the response for this function is a dictionary that does not contain the actual output.

An answer to a different question (https://stackoverflow.com/a/67441672/3269537) helpfully answers that the ec2.get_command_invocation function can get the output. However, you have to essentially poll using get_command_invocation until the remote command has completed.

I want to run multiple commands remotely in sequence, including uploading files to the instance (without using ssh). Is there any better way to do this besides calling send_command and get_command_invocation over and over?

CodePudding user response:

There is no other way to do that. The only improvement you can make is to use waiter get_waiter('command_executed') provided by boto3. It essentially does the same as you would manually pool get_command_invocation, except that you don't have to write that loop. boto3 does that for you.

  • Related