【问题标题】:AWS Assume Role access denied while using AWS PHP SDK使用 AWS PHP 开发工具包时 AWS Assume Role 访问被拒绝
【发布时间】:2022-01-26 21:35:20
【问题描述】:

我在使用 AWS PHP 开发工具包时遇到问题 从 apache 服务器(PHP SDK)调用 AssumeRole 时出现以下错误

    Error executing "AssumeRole" on "https://sts.amazonaws.com"; 
    AWS HTTP error: Client error: `POST https://sts.amazonaws.com` resulted in a `403 Forbidden` 
response: Sender AccessDeni (truncated...) AccessDenied (client): Access denied - Sender AccessDenied Access denied

我使用 AWS CLI 测试了在同一台 ec2 机器中担任角色的命令,它工作正常。

这是我使用的代码。

const AccessKey = "<AccessKey>";
    const SecretAccessKey = "<SecretAccessKey>";
    const AccountID = "<AccountID>";
    const Name_space = "default";  // leave this as default

    use Aws\Sts\StsClient;
    use Aws\Sts\StsException;     

 try {
  $sts = new Aws\Sts\StsClient([
            'region' => 'us-east-1',
            'version' => 'latest',
            'credentials  ' => array('key' => AccessKey,
               'secret' => SecretAccessKey)
        ]);



 $session = $sts->assumeRole([
           'DurationSeconds' => 900,
           'RoleArn' => '<arn>', // REQUIRED
           'RoleSessionName' => testSession, // REQUIRED
      ]);

 } catch (Exception $e) {

        exit($e->getMessage());
    }

--编辑添加以下内容--

用户具有承担该角色的策略

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "<role arn>"
    }
}

这是角色的信任关系

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com",
        "AWS": "<user arn>"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

【问题讨论】:

  • 您的 IAM 用户是否有权担任该角色?如果是,您能否检查 IAM 角色的信任关系是否允许 IAM 用户代入该角色?
  • @krishna_mee2004 是的,我有权担任角色,信任关系允许 IAM 用户担任该角色。请看看我添加了它们。是否有可能我必须为 SDK 或 API 添加信任关系?
  • 请确保信任关系中IAM用户的ARN正确。你到目前为止所做的都是正确的。尝试运行以下命令以确保权限没有问题:AWS_ACCESS_KEY_ID="" AWS_SECRET_ACCESS_KEY="" aws sts assume-role --role-arn iam_role_arn --role-session-name Session1。如果此命令返回凭据,则代码有问题。如果没有,请检查您的 IAM 用户权限和 IAM 角色权限。

标签: php apache amazon-web-services sts-securitytokenservice aws-php-sdk


【解决方案1】:

在 AWS 控制台中打开角色 > 信任关系。 点击“编辑信任关系”并粘贴以下内容

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::014361779291:user/<<username>>"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多