14

I am trying to send command to a running ubuntu ec2 instance. I have configured the appropriate role and I have an ssm agent running on the ec2 instance. Using the boto3 SDK I am able to use the client.send_command() function to successfully send a shell command and was subsequently able to get the command Id. Now the challenge is polling for the result. I am trying to use the client.get_command_invocation() function but keep getting a InvocationDoesNotExist error. I am confident that I am using the correct command ID and instance ID because I have tested these using the AWS CLI as in aws ssm list-command-invocations --command-id #### --instance-id i-##### and this worked. Here is a snippet of my code:

`ssm_client = boto3.client('ssm')
target_id = "i-####"
response = ssm_client.send_command(
            InstanceIds=[
                target_id 
                    ],
            DocumentName="AWS-RunShellScript",
            Comment="Code to run" ,
            Parameters={
                    "commands": ["ls -l"]
                        }
            )
cmd_id= response['Command']['CommandId']
response = ssm_client.get_command_invocation(CommandId=cmd_id, 
InstanceId=target_id)
print(response)`

Here is the returned error: botocore.errorfactory.InvocationDoesNotExist: An error occurred (InvocationDoesNotExist) when calling the GetCommandInvocation operation

Thanks in advance.

4 Answers 4

17

I had the same issue, I fixed it by adding a time.sleep() call before calling get_command_invocation(). A short delay should be enough.

1
8

Just add these 2 line.

import time
time.sleep(2)

Then It'll work properly generally it take only 0.65sec but it's better to give 2 sec. To make it better you can add some cool stuffs like some print statement in for loop and sleep inside it something like that.

5

The following worked for me. Check out the API documentation for how to update the default delays and max attempts at https://boto3.amazonaws.com.

from botocore.exceptions import WaiterError 
ssm_client = boto3.client(
    "ssm",
    region_name=REGION,
    aws_access_key_id=KEY,
    aws_secret_access_key=SECRET,
)

waiter = ssm_client.get_waiter("command_executed")
try:
    waiter.wait(
        CommandId=commands_id,
        InstanceId=instance_id,
    )
except WaiterError as ex:
    logging.error(ex)
    return
1
  • Showing a full example for posterity: ``` client = boto3.client('ssm') response = run_command_on_ec2_instance(client, command, instance_id) command_id = response['Command']['CommandId'] waiter = client.get_waiter("command_executed") try: waiter.wait( CommandId=command_id, InstanceId=instance_id, ) except WaiterError as ex: logging.error(ex) return print(client.get_command_invocation(CommandId=command_id, InstanceId=instance_id)['StandardOutputContent']) ```
    – Ken J
    Sep 26 at 13:20
0

time.sleep does the job 99% of the time. Here is a better way to do this:

MAX_RETRY = 10

get_command_invocation_params = {
    'CommandId': 'xxx',
    'InstanceId': 'i-xxx'
}

for i in range(MAX_RETRY):
    try:
       command_executed_waiter.wait(**get_command_invocation_params)
       break
    except WaiterError as err:
        last_resp = err.last_response
        if 'Error' in last_resp:
            if last_resp['Error']['Code'] != 'InvocationDoesNotExist' or i + 1 == MAX_RETRY:
                raise err
        else:
            if last_resp['Status'] == 'Failed':
                print(last_resp['StandardErrorContent'], file=sys.stderr)
                exit(last_resp['ResponseCode'])
        continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.