gpt4 book ai didi

amazon-web-services - AWS Boto3 : The security token included in the request is invalid

转载 作者:行者123 更新时间:2023-12-01 21:48:51 26 4
gpt4 key购买 nike

读完这个问题后How to SSH and run commands in EC2 using boto3?我尝试使用 SSM 在 EC2 实例上自动运行命令。但是,当我编写这样的代码时

def excute_command_on_instance(client, command, instance_id):
response = client.send_command(
DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
Parameters={'commands': command},
InstanceIds=instance_id,
)
return response

# Using SSM in boto3 to send command to EC2 instances.
ssm_client = boto3.client('ssm')
commands = ['echo "hello world']
instance_id = running_instance[0:1]
excute_command_on_instance(ssm_client, commands, instance_id)

这让我想起了

botocore.exceptions.ClientError:调用 SendCommand 操作时发生错误 (AccessDeniedException):用户:arn:aws:iam::62771xxxx946:user/Python_CloudComputing 无权对资源执行:ssm:SendCommand: arn:aws:ec2:eu-west-2:6277xxxx3946:实例/i-074f862c3xxxxfc07
.

在我使用SSTclient生成凭据后,我得到了如下代码。

    def excute_command_on_instance(client, command, instance_id):
response = client.send_command(
DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
Parameters={'commands': command},
InstanceIds=instance_id,
)
return response

# Using SSM in boto3 to send command to EC2 instances.
sts = boto3.client('sts')
sts_response = sts.get_session_token()
ACCESS_KEY = sts_response['Credentials']['AccessKeyId']
SECRET_KEY = sts_response['Credentials']['SecretAccessKey']
ssm_client = boto3.client(
'ssm',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
)
commands = ['echo "hello world']
instance_id = running_instance[0:1]
excute_command_on_instance(ssm_client, commands, instance_id)

但是,这一次它提醒了我

botocore.exceptions.ClientError:调用 SendCommand 操作时发生错误 (UnrecognizedClientException):请求中包含的安全 token 无效。

谁能告诉我如何解决这个问题?

最佳答案

您缺少 IAM 用户或角色访问 SSM 的权限。

您还尝试使用 STS 来获取访问权限,这使您需要做的事情变得过于复杂。 STS需要承担的策略需要相同的权限。有很多使用 STS(最小权限规则)的好案例,但我认为您在这里不需要 STS。

Amazon 为 SSM 提供预定义策略,您可以将其快速添加到策略或角色中,例如:

AmazonEC2RoleForSSM
AmazonSSMFullAccess
AmazonSSMReadOnlyAccess

此链接将帮助您配置对 Systems Manager 的访问:

Configuring Access to Systems Manager

关于amazon-web-services - AWS Boto3 : The security token included in the request is invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com