使用Boto3启动带有IAM的EC2实例时发生未经授权的操作错误

2024-03-29 05:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了这个问题How do I use Boto3 to launch an EC2 instance with an IAM role?,并尝试在python脚本中启动一个具有IAM角色的实例。代码如下:

instance = ec2.create_instances(
    ImageId='ami-1a7f6d7e',
    KeyName='MyKeyPair',
    MinCount=1,
    MaxCount=1,
    SecurityGroups=['launch-wizard-3'],
    InstanceType='t2.micro',
    IamInstanceProfile={
        'Arn': 'arn:aws:iam::627714603946:instance-profile/SSMforCC'}
)

但是,我在运行脚本botocore.exceptions.ClientError: An error occurred (UnauthorizedOperation) when calling the RunInstances operation: You are not authorized to perform this operation.之后遇到了这个错误,我发现这个问题how do I launch ec2-instance with iam-role?Ruby提供了解决问题的方法。有谁能告诉我在python Boto3中是否有解决这个问题的方法?在


Tags: to方法instance脚本anwithboto3ec2
2条回答

您没有足够的权限(iam::PassRole)将IAM角色附加到实例。所以附加一个政策,授予你特权。只有当您是IAM管理员或有足够的权限将策略附加到用户时,才能将策略附加到用户。在

我去查一下- 1如果您对AWS的身份验证是否正确-您可以在客户机中显式指定访问和密钥。在

client = boto3.client(
    'ec2',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
)
  1. 如果用户对您尝试创建的资源具有ec2:runInstancesIAM权限。在

相关问题 更多 >