【问题标题】:Access denied when assuming role as IAM user via boto3通过 boto3 担任 IAM 用户角色时访问被拒绝
【发布时间】:2020-02-25 04:52:50
【问题描述】:

问题

我有一个 IAM 用户和一个 IAM 角色。我正在尝试将 IAM 用户配置为有权使用 STS 代入 IAM 角色。我不确定为什么会收到“拒绝访问”错误。

详情

IAM 角色:arn:aws:iam::123456789:role/athena_access

IAM 用户:arn:aws:iam::123456789:user/athena-external-user

允许代入角色的 IAM 用户策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "StsAssumeRole",
            "Effect": "Allow",
            "Action": "sts:*",
            "Resource": "arn:aws:iam::123456789:role/athena_access"
        }
    ]
}

代码:

import boto3
os.environ['AWS_ACCESS_KEY_ID'] = '...'
os.environ['AWS_SECRET_ACCESS_KEY'] = '...'

client = boto3.client('sts')
role_to_assume_arn='arn:aws:iam::123456789:role/athena_access'
role_session_name='test_session'
response=client.assume_role(
    RoleArn=role_to_assume_arn,
    RoleSessionName=role_session_name
)

错误:

botocore.exceptions.ClientError:调用 AssumeRole 操作时发生错误 (AccessDenied):用户:arn:aws:iam::123456789:user/athena-external-user 无权执行:sts:AssumeRole on resource : arn:aws:iam::123456789:role/athena_access

【问题讨论】:

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


    【解决方案1】:

    当然,我在发布问题后不久就找到了解决方案。

    IAM 角色需要为将担任该角色的用户制定 TrustRelationship 政策。

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": "ec2.amazonaws.com",
            "AWS": "arn:aws:iam::123456789:user/athena-external-user"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    }
    

    【讨论】:

    • 我最近刚刚回答了一个与此非常相似的问题。信任关系部分很重要。如果有多个帐户在玩,它也会涉及更多。
    猜你喜欢
    • 2019-09-10
    • 2016-04-07
    • 2017-11-27
    • 1970-01-01
    • 2022-12-08
    • 2016-02-07
    • 2020-11-08
    • 2022-07-28
    • 2021-11-13
    相关资源
    最近更新 更多