2017-02-16 64 views
8

私はboto3を使用してEC2インスタンスでsshコマンドを実行しようとしています。 私は、このガイド読み: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshooting-remote-commands.html を、私は、彼らが書いたもののすべてをしたが、私は続けるエラーメッセージが表示されます:SSMがEC2インスタンスにコマンドを送信するのに失敗しました

>>>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エージェントがインストールされているため、1行にいくつかのトラブルシューティング手順が含まれていることを確認します(nice;))。

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']) 

(存在する場合は、それは確かに私のものでした)。

このthis SO post for a possibly-related discussion on waiting for user data to completeを参照)により、UserDataのスクリプトを実行するのにかかった時間にかもしれないが、私は(私が取って喜んだよりも多くの努力なしで!)言うことができないことがあること、または単にですかそのサービスデータベースを更新するAWS固有の時間。

これを解決するために、インスタンスIDがリストに現れるまで、繰り返しdescribe_instance_information()を呼び出す短いウェイター(他の失敗モードを処理するタイムアウト例外あり)を書きました。

関連する問題