「You are not authorized to perform this operation.」が発生したときの対処法

「You are not authorized to perform this operation.」が発生したときの対処法

「You are not authorized to perform this operation.」が発生した時は、当該のアクションが実行できるポリシーが設定されているかを確認しましょう。また、暗号のようなメッセージが表示される場合がありますが、aws sts decode-authorization-message コマンドで復元ができます。

Clock Icon2021.11.9

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

困っていた内容

AWS CLI コマンド実行時、また、マネジメントコンソールで操作を行ったところ

You are not authorized to perform this operation.」というエラーメッセージが発生しました。

どのように対応すれば良いですか?

また、暗号のような文字列も同時に表示されました。解読できますか?

どう対応すればいいの?

当該コマンドの実行やマネジメントコンソールでの操作実施時に、当該操作を行うために必要な IAM ポリシーが不足している場合に発生します。

そのため、エラー発生に表示されたアクションを許可するポリシーを IAM ユーザーや IAM ロールに付与することで、事象が解消するかをお試しください。

「You are not authorized to perform this operation.」への対応

例: EC2 が開始できない

以下の IAM ポリシーを付与した IAM ロールにて、特定の EC2 を開始するアクションを実行します。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DescribeOnly",
            "Effect": "Allow",
            "Action": "ec2:Describe*",
            "Resource": "*"
        }
    ]
}
$ aws ec2 start-instances --instance-ids i-0d815c2129example --region ap-northeast-1

An error occurred (UnauthorizedOperation) when calling the StartInstances operation: You are not authorized to perform this operation. Encoded authorization failure message: TU8IM<中略>EXAMPLE

アクションの実行に失敗します。

IAM ロールに付与されているポリシーに以下のように、ec2:StartInstancesアクションを許可する設定を加えます。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DescribeAndStartInstances",
            "Effect": "Allow",
            "Action": [
                "ec2:Describe*",
                "ec2:StartInstances"
            ]
            "Resource": "*"
        }
    ]
}

再度コマンドを実行します。

$ aws ec2 start-instances --instance-ids i-0d815c2129example --region ap-northeast-1

エラーメッセージの発生はなく、インスタンスは開始されます。

暗号の解読

「Encoded authorization failure message」というメッセージの後、ランダムの文字列が表示される場合があります。

こちらを解読する方法は、aws sts decode-authorization-message を実行する方法があります。

AWS CLI コマンドにて実行してみます。

$ aws sts decode-authorization-message --encoded-message TU8IM<中略>EXAMPLE

An error occurred (AccessDenied) when calling the DecodeAuthorizationMessage operation: User: arn:aws:sts::<インスタンスID>:assumed-role/<IAMロール名>/i-0d815c2129example is not authorized to perform: sts:DecodeAuthorizationMessage because no identity-based policy allows the sts:DecodeAuthorizationMessage action

aws sts decode-authorization-message コマンドも、IAM ポリシーにて、sts:DecodeAuthorizationMessage アクションを許可する権限を追加する必要があります。

IAM ポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DecodeMessage",
            "Effect": "Allow",
            "Action": "sts:DecodeAuthorizationMessage",
            "Resource": "*"
        }
    ]
}

ポリシーを設定したので AWS CLI コマンドを再度実行します。※以下の結果は視認性を上げるために文字列を操作し、一部改行しています。

$ aws sts decode-authorization-message --encoded-message TU8IM<中略>EXAMPLE
{
"DecodedMessage": "{
    "allowed":false,
    "explicitDeny":false,
    "matchedStatements":{
        "items":[]
    },
    "failures":{
        "items":[]
    },
    "context":{
        "principal":{
            "id":"AROAVOYEZV6HWNEXAMPLE:i-0d815c2129example",
            "arn":"arn:aws:sts::<AccountID>:assumed-role/<IAMRoleName>/i-0d815c2129exapmle"
        },
        "action":"ec2:StartInstances",
        "resource":"arn:aws:ec2:ap-northeast-1:<AccountID>:instance/i-0d815c2129exapmle",
        "conditions":{
            "items":[
                {
                    "key":"ec2:InstanceMarketType",
                    "values":{
                        "items":[
                            {"value":"on-demand"}
                        ]
                    }
                },
                {
                    "key":"aws:Resource",
                    "values":{
                        "items":[
                            {"value":"instance/i-0d815c2129exapmle"}
                        ]
                    }
                },
                {
                    "key":"aws:Account",
                    "values":{
                        "items":[
                            {"value":"<AccountID>"}
                        ]
                    }
                },
                {
                    "key":"ec2:AvailabilityZone",
                    "values":{
                        "items":[
                            {"value":"ap-northeast-1a"}
                        ]
                    }
                },
                {
                    "key":"ec2:ResourceTag/Name",
                    "values":{
                        "items":[
                            {"value":"test"}
                        ]
                    }
                },
                {
                    "key":"ec2:InstanceProfile",
                    "values":{
                        "items":[
                            {"value":"arn:aws:iam::<AccountID>:instance-profile/<IAMRoleName>"}
                        ]
                    }
                },
                {
                    "key":"ec2:InstanceType",
                    "values":{
                        "items":[
                            {"value":"t3.micro"}
                        ]
                    }
                },
                {
                    "key":"ec2:RootDeviceType",
                    "values":{
                        "items":[
                            {"value":"ebs"}
                        ]
                    }
                },
                {
                    "key":"aws:Region",
                    "values":{
                        "items":[
                            {"value":"ap-northeast-1"}
                        ]
                    }
                },
                {
                    "key":"aws:Service",
                    "values":{
                        "items":[
                            {"value":"ec2"}
                        ]
                    }
                },
                {
                    "key":"ec2:EbsOptimized",
                    "values":{
                        "items":[
                            {"value":"true"}
                        ]
                    }
                },
                {
                    "key":"ec2:InstanceID",
                    "values":{
                        "items":[
                            {"value":"i-0d815c2129exapmle"}
                        ]
                    }
                },
                {
                    "key":"aws:Type",
                    "values":{
                        "items":[
                            {"value":"instance"}
                        ]
                    }
                },
                {
                    "key":"ec2:Tenancy",
                    "values":{
                        "items":[
                            {"value":"default"}
                        ]
                    }
                },
                {
                    "key":"ec2:Region",
                    "values":{
                        "items":[{
                            "value":"ap-northeast-1"}
                        ]
                    }
                },
                {
                    "key":"aws:ARN",
                    "values":{
                        "items":[
                            {"value":"arn:aws:ec2:ap-northeast-1:<AccountID>:instance/i-0d815c2129exapmle"}
                        ]
                    }
                }
            ]
        }
    }
}"

メッセージによると、"allowed":false"action":"ec2:StartInstances" といった記述あり、StartInstances アクションが許可する設定がないことが確認できます。

参考資料

Management Consoleの権限不足エラーをデコードする | DevelopersIO

Amazon EC2 の RunInstances でエンコードされた認証の失敗

decode-authorization-message — AWS CLI 1.21.8 Command Reference

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.