【问题标题】:SSM send command to EC2 instance FailedSSM 向 EC2 实例发送命令失败
【发布时间】:2017-07-05 22:03:39
【问题描述】:

我正在尝试使用 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:

有人知道怎么解决吗?

【问题讨论】:

  • 实例是否在不同的区域?确保您使用 SDK 和/或 CLI 工具配置了正确的 AWS 账户和区域。

标签: python amazon-web-services instances ssm


【解决方案1】:

当您尝试访问的实例上没有安装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 代理的实例,这样就可以在一行中涵盖多个故障排除步骤(很好;))。

    如果您使用的是boto3,则可以通过以下方式实现:

    ssm.client.describe_instance_information()
    

    我不确定它是否检查权限,但假设是这样。如果列表中缺少您的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 出现在列表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2021-01-19
      • 2016-07-17
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      相关资源
      最近更新 更多