簡體   English   中英

SSM發送命令到EC2實例失敗

[英]SSM send command to EC2 instance Failed

我正在嘗試使用boto3在EC2實例上運行ssh命令。 我讀了這本指南: http//docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshooting-remote-commands.html我做了他們在那里寫的所有內容,但我一直收到錯誤信息:

>>>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:

有人知道怎么解決嗎?

當您嘗試訪問的實例上沒有安裝SSM代理時,可能會發生這種情況。 有關可以運行SSM命令的實例列表,請運行:

aws ssm describe-instance-information --output text

從那里,您可以獲取實例ID,然后使用該實例運行send_command命令。

AWS'故障排除指南中所述,此錯誤可能有多種原因。

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

如果您使用的是boto3 ,可以通過以下方式實現:

ssm.client.describe_instance_information()

我不確定它是否檢查權限但是假定如此。 如果列表中缺少instance_id,則可以按照此處的分步操作確保正確的權限。

然而,還有另一個原因(最后但並非最不重要,因為它並不明顯):

新創建的實例需要一些時間才能顯示在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腳本所花費的時間(請參閱此SO帖子以了解有關等待用戶數據完成的可能相關的討論 ),但我無法分辨(沒有比我願意采取的更多努力) !)是否是那個,或者只是AWS更新其服務數據庫所固有的時間。

為了解決這個問題,我寫了一個短服務員(處理其他故障模式的超時異常),重復調用describe_instance_information(),直到實例id出現在列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM