【问题标题】:Unauthorized operation error occurs when using Boto3 to launch an EC2 instance with an IAM role使用Boto3启动具有IAM角色的EC2实例时出现未经授权的操作错误
【发布时间】:2018-04-11 06:41:48
【问题描述】:

我已阅读此问题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 中解决这个问题?

【问题讨论】:

  • 如果您只删除IamInstanceProfile,相同的脚本是否有效?
  • 当我删除IamInstanceProfile时它确实有效

标签: python amazon-web-services amazon-ec2 boto3


【解决方案1】:

您没有足够的权限 (iam::PassRole) 将 IAM 角色附加到实例。因此,请附加一项授予您特权的策略。仅当您是 IAM 管理员或具有将策略附加到用户的足够权限时,您才能将策略附加到用户。

【讨论】:

    【解决方案2】:

    我会检查 - 1. 如果您正确地向 AWS 进行身份验证 - 您可以在客户端中明确指定访问密钥和密钥。

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

    【讨论】:

    • 当我删除属性 IamInstanceProfile 时,此脚本有效。添加该属性后,它就不起作用了。我认为身份验证没有问题,请您解释第二个原因吗?
    • 第二点与经过身份验证的 IAM 用户的权限有关 - 基本上是如果它具有所需的权限。由于您提到它可以在不指定 IamInstanceProfile 的情况下工作,我建议您也验证 - 1. 如果此用户对您要使用的角色具有 iam:passrole 权限。 2.检查您要使用的角色是否有实例配置文件arn - 可以在IAM控制台下检查 - 如果实例配置文件arn为空,则不能在实例上使用。
    猜你喜欢
    • 2017-05-21
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2016-10-07
    • 2019-12-17
    • 2021-03-01
    相关资源
    最近更新 更多