2017-02-16 12 views
8

मैं EC2 उदाहरणों पर ssh कमांड चलाने के लिए boto3 उपयोग करने के लिए कोशिश कर रहा हूँ करने के लिए आदेश भेज देते हैं। मैं इस गाइड पढ़ें: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/troubleshooting-remote-commands.html और मैं क्या वे वहाँ लिखा था की सब कुछ है, लेकिन मैं त्रुटि संदेश मिलता है रखना था:एसएसएम 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: 

कोई इसे हल करने के बारे में जानता है? जब आप SSM agent उदाहरण आप पहुँचने का प्रयास कर रहे हैं पर स्थापित नहीं है

+0

अन्य क्षेत्र में उदाहरण है?सुनिश्चित करें कि आपके पास सही एसडब्ल्यूएस खाता है और क्षेत्र आपके एसडीके और/या सीएलआई उपकरण के साथ कॉन्फ़िगर किया गया है। –

उत्तर

7

यह तब हो सकता। जिन मामलों में आप एसएसएम आदेश चला सकते हैं की एक सूची के लिए, चलाएँ:

aws ssm describe-instance-information --output text 

वहाँ से, आप एक उदाहरण आईडी खींचते हैं और फिर कि उदाहरण के साथ send_command आदेश चला सकते हैं।

2

के रूप में प्रलेखित here in AWS' troubleshooting guide इस त्रुटि के लिए संभावित कारणों की एक सीमा नहीं है।

स्वीकृत उत्तर aws ssm describe-instance-information मान्य स्थिति में उपलब्ध दोनों उदाहरणों के लिए जांच करता है और एसएसएम एजेंट स्थापित है, ताकि एक पंक्ति में कई समस्या निवारण चरणों को शामिल किया जा सके (अच्छा;))।

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 देखें), लेकिन मैं (अधिक प्रयास के बिना की तुलना में मैं लेने के लिए! तैयार हूँ) नहीं बता सकते हैं कि यह है कि, या बस एडब्ल्यूएस अपनी सेवाओं डेटाबेस अद्यतन में निहित समय।

इसे हल करने के लिए, मैंने एक छोटा वेटर लिखा (अन्य विफलता मोड को संभालने के लिए टाइमआउट अपवाद के साथ) जिसे बार-बार वर्णन_instance_information() कहा जाता है जब तक कि इंस्टेंस आईडी सूची में दिखाई न दे।

संबंधित मुद्दे