AWS SSM 描述实例信息找不到我的实例

AWS SSM describe-instance-information doesn't find my instances

我正在使用 boto3 从 python 环境中使用 ec2 和 ssm 服务控制我在 AWS 上的 EC2 实例。我创建了一个 IAM 账户,可以访问 AmazonSSMFullAccess 和 AmazonEC2FullAccess 策略。

ec2 = boto3.client(
    'ec2',
    region_name='eu-west-1',
    aws_access_key_id='…',
    aws_secret_access_key='…/…+…'
)

ssm = boto3.client(
    'ssm',
    region_name='eu-west-1',
    aws_access_key_id='…',
    aws_secret_access_key='…/…+…'
)

我运行:

ec2.describe_instances()['Reservations']

Wich 返回了我所有实例的列表。 但是当我 运行 :

ssm.describe_instance_information()

我得到一个空列表,尽管我在 AWS Linux AMI (ami-ca0135b3) 上至少有一个实例 运行ning,在最近的 Ubuntu AMI 上还有六个实例。它们都在 eu-west-1(爱尔兰)。 他们应该预装了 SSM 代理:(https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-agent.html)

我进入 AWS Linux 实例,并尝试使用 :

获取 ssm 的日志
sudo tail -f /var/log/amazon/ssm/amazon-ssm-agent.log

但是当我 运行 我的 python 代码时没有任何反应。不时显示一系列消息:

HealthCheck reporting agent health.
error when calling AWS APIs. error details - NoCredentialProviders: no valid providers in chain. Deprecated.

我也试过运行通过 Web 界面执行命令,选择了“AWS-RunRemoteScript”,但下面没有显示任何实例。

我的目标是 运行 :

ssm.send_command(
        DocumentName="AWS-RunShellScript",
        Parameters={'commands': [command]},
        InstanceIds=[instance_id],
    )

但它给了我以下错误,可能是由于之前的问题。

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

代理已预先安装,但实例(不仅仅是您的 IAM 用户)仍需要适当的角色才能与系统管理员通信。特别是 this step of Configuring Access to Systems Manager.

By default, Systems Manager doesn't have permission to perform actions on your instances. You must grant access by using an IAM instance profile. An instance profile is a container that passes IAM role information to an Amazon EC2 instance at launch.

您应该查看整个配置指南并确保您已正确配置所有必需的角色。