Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving command invocation in AWS SSM

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.

like image 939
Remilekun Basaru Avatar asked Apr 27 '18 16:04

Remilekun Basaru


People also ask

Which command is used to get Awscli?

You can get help with any command when using the AWS Command Line Interface (AWS CLI). To do so, simply type help at the end of a command name.

What is SSM Run command?

What Is Run Command In AWS System Manager (SSM): AWS Systems Manager Run Command lets you remotely and securely manage the configuration of your managed instances. A managed instance is any EC2 instance or on-premises machine in your hybrid environment that has been configured for Systems Manager.

Which CLI command is used to tag AWS resources?

By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.


2 Answers

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.

like image 128
Mohd Mujtaba Avatar answered Sep 21 '22 14:09

Mohd Mujtaba


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.

like image 26
Drake Petersen Avatar answered Sep 17 '22 14:09

Drake Petersen