2017-02-16 223 views
8

我試圖用boto3對EC2實例運行SSH命令EC2實例。 我閱讀本指南: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshooting-remote-commands.html 和我做了他們寫有什麼一切,但我不斷收到錯誤消息:SSM發送命令失敗

>>>import boto3 
>>> ec2 = boto3.client('ssm') 
>>> a = ec2.send_command(InstanceIds=['i-0d5e16f6'], DocumentName='AWS-RunShellScript', Comment='abcdabcd', Parameters={"commands":["ifconfig"]}) 

輸出:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 253, in _api_call 
    return self._make_api_call(operation_name, kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 543, in _make_api_call 
    raise error_class(parsed_response, operation_name) 
    botocore.errorfactory.InvalidInstanceId: An error occurred (InvalidInstanceId) when calling the SendCommand operation: 

,如果我要發送的命令awscli我得到了同樣的問題:

aws ssm send-command --instance-ids "i-0d5e16f6" --document-name "AWS-RunShellScript" --comment "IP config" --parameters commands=ifconfig --output text 

An error occurred (InvalidInstanceId) when calling the SendCommand operation: 

一些人知道如何解決這個問題?

+0

是在不同區域的實例?確保使用SDK和/或CLI工具配置了正確的AWS賬戶和區域。 –

回答

7

這時候您沒有安裝到您嘗試訪問的實例SSM agent就可能發生。對於您可以運行命令SSM實例的列表,請運行:

aws ssm describe-instance-information --output text 

從那裏,你可以抓住一個實例ID,然後運行與該實例的send_command命令。

2

作爲記錄here in AWS' troubleshooting guide有一系列的此錯誤的可能原因。

接受的答案aws ssm describe-instance-information檢查兩個可用的實例,處於有效狀態並安裝了SSM代理程序,因此涵蓋了一行(很好;)中的幾個故障排除步驟。

ssm.client.describe_instance_information() 

我不能肯定它是否檢查權限,但假設這樣:

如果您使用boto3同樣可以實現。如果您的instance_id從列表中缺失,您可以按照步驟here的步驟確保正確的權限。

然而,還有另外一個原因(最後但絕對不是最因爲它不是很明顯):

剛創建的實例花費一點時間在describe_instance_information列表露面。

這是即使等待的實例來完成創作後。因此,例如做:

# Key names are the same as the keyword arguments required by boto 
    params = { 
      'ImageId': image_id_to_use, 
      'InstanceType': instance_type_to_launch, 
      'MinCount': 1, 
      'MaxCount': 1, 
      'UserData': user_data_script, 
      'SecurityGroups': ['your groups'], 
      'KeyName': 'yourkeyname', 
      } 

    # Run the instance and wait for it to start 
    reservation = ec2.client.run_instances(**params) 
    instance = ec2.resource.Instance(reservation['Instances'][0]['InstanceId']) 
    instance.wait_until_running() 

    # Also wait status checks to complete 
    waiter = ec2.client.get_waiter('instance_status_ok') 
    waiter.wait(InstanceIds=[instance.id]) 

    # Apply the IAM roles required (this instance will need access to, e.g., S3) 
    response = ec2.client.associate_iam_instance_profile(
     IamInstanceProfile={ 
      'Arn': 'your_arn', 
      'Name': 'ApplicableRoleEGAdministratorAccess' 
     }, 
     InstanceId=instance.id 
    ) 

    print('Instance id just created:', instance.id) 
    print('Instances in the SSM instances list right now:') 
    print(ssm.client.describe_instance_information()['InstanceInformationList']) 

將突出這個問題(如果存在的話 - 它肯定是對我來說)。

可能是由於執行的UserData腳本所花費的時間(見this SO post for a possibly-related discussion on waiting for user data to complete),但我不能告訴(沒有更多的精力比我願意拿!)不管是那個,或者只是AWS更新其服務數據庫的固有時間。

爲了解決這個問題,我寫了一個短服務員(有超時異常處理其他故障模式),其一再呼籲describe_instance_information(),直到實例ID在列表中出現了。