MeWrite Docs

AccessDenied: User is not authorized to perform sts:AssumeRole

概要

IAMユーザーまたはロールが別のロールを引き受ける(AssumeRole)権限を持っていない場合に発生するエラーです。信頼ポリシーまたはIAMポリシーの設定が必要です。

エラーメッセージ

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::111111111111:user/my-user is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::222222222222:role/target-role
AccessDenied: Not authorized to perform sts:AssumeRole

原因

このエラーは以下の原因で発生します:

  1. 信頼ポリシーの不備: ターゲットロールがソースを信頼していない
  2. IAMポリシーの不足: ソースユーザー/ロールにAssumeRole権限がない
  3. アカウント間の設定ミス: クロスアカウントで両方の設定が必要
  4. 外部ID(ExternalId)の不一致: 必要な外部IDが提供されていない
  5. MFAが必要: AssumeRoleにMFA認証が必要

解決策

1. ターゲットロールの信頼ポリシーを設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::111111111111:user/my-user"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

2. ソースにAssumeRole権限を付与

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::222222222222:role/target-role"
        }
    ]
}

3. クロスアカウントの設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# アカウントA (111111111111) のユーザーが
# アカウントB (222222222222) のロールを引き受ける

# アカウントBのロール信頼ポリシー
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::111111111111:root"
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                    "sts:ExternalId": "my-external-id"
                }
            }
        }
    ]
}

4. ExternalIdを指定してAssumeRole

1
2
3
4
aws sts assume-role \
    --role-arn arn:aws:iam::222222222222:role/target-role \
    --role-session-name my-session \
    --external-id my-external-id

5. MFA付きでAssumeRole

1
2
3
4
5
6
# MFAトークンを使用
aws sts assume-role \
    --role-arn arn:aws:iam::222222222222:role/target-role \
    --role-session-name my-session \
    --serial-number arn:aws:iam::111111111111:mfa/my-user \
    --token-code 123456

6. SDKでの使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import boto3

# AssumeRole でセッションを取得
sts = boto3.client('sts')
response = sts.assume_role(
    RoleArn='arn:aws:iam::222222222222:role/target-role',
    RoleSessionName='my-session',
    ExternalId='my-external-id'  # 必要な場合
)

# 一時的な認証情報を使用
credentials = response['Credentials']
s3 = boto3.client(
    's3',
    aws_access_key_id=credentials['AccessKeyId'],
    aws_secret_access_key=credentials['SecretAccessKey'],
    aws_session_token=credentials['SessionToken']
)

よくある間違い

  • 信頼ポリシーのPrincipalにIAMユーザーではなくルートアカウントを指定
  • ソース側のポリシーでResource を * にして特定ロールを指定していない
  • ExternalIdが設定されているのに提供していない
  • セッション名に使用できない文字を含めている

デバッグ

1
2
3
4
5
# ポリシーシミュレーターで確認
aws iam simulate-principal-policy \
    --policy-source-arn arn:aws:iam::111111111111:user/my-user \
    --action-names sts:AssumeRole \
    --resource-arns arn:aws:iam::222222222222:role/target-role

関連エラー

参考リンク

関連エラー

AWS の他のエラー

最終更新: 2025-12-16