วิธีจัดการเมื่อเจอกับปัญหา AWS IAM 「You are not authorized to perform this operation.」

เมื่อเกิด「You are not authorized to perform this operation.」ให้ทำการตรวจสอบว่ามีการตั้งค่า Policy ในการดำเนินการนั้นหรือไม่ ส่วนปัญหาเกี่ยวกับข้อความที่ถูกเข้ารหัส สามารถอ่านได้โดยใช้ Command "aws sts decode-authorization-message"
2023.07.06

ปัญหาที่เกิดขึ้น

เมื่อเราทำการรันคำสั่ง AWS CLI Command หรือควบคุมผ่าน Management Console แล้วเราเจอกับข้อความ Error

You are not authorized to perform this operation.

จะมีวิธีแก้ปัญหาอย่างไรดี

แล้วตัวอักษรที่เหมือนกับถูกเข้ารหัสอยู่ที่แสดงขึ้นจะมีวิธีอ่านหรือไม่

วิธีแก้ปัญหา

ปัญหานี้เกิดขึ้นเมื่อ IAM Policy ที่ใช้สำหรับควบคุม Management Console หรือ รันคำสั่ง AWS CLI Command อยู่มีสิทธิ์ไม่เพียงพอ

เราแนะนำให้คุณลองแก้ไขปัญหาโดยการเพิ่ม Policy ที่เป็นสิทธิ์ที่ต้องการใน Error ให้กับ IAM User หรือ IAM Role

วิธีจัดการกับ「You are not authorized to perform this operation.」

ตัวอย่าง: EC2 เปิดใช้งานไม่ได้

เพิ่ม IAM Policy ด้านล่างนี้ให้กับ IAM Role ที่ต้องการใช้งาน EC2 แล้วลองเปิดใช้งาน 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

จะเห็นได้ว่าเราจะไม่สามารถเปิดใช้งาน EC2 ได้ และจะเกิด error「You are not authorized to perform this operation.」ขึ้น

ทำการเพิ่มสิทธิ์ ec2:StartInstances ให้ Policy ของ IAM Role เมื่อครู่ครับ

{
    "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

จะเห็นได้ว่าเราสามารถเข้าใช้งาน EC2 instance ได้ โดยไม่เกิด error ครับ

การถอดข้อความที่เข้ารหัส

ในบางกรณีหลังจากข้อความ「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::<Instance ID>:assumed-role/<IAM Role name>/i-0d815c2129example is not authorized to perform: sts:DecodeAuthorizationMessage because no identity-based policy allows the sts:DecodeAuthorizationMessage action

การรันคำสั่ง aws sts decode-authorization-message จำเป็นต้องเพิ่มสิทธิ์ sts:DecodeAuthorizationMessage ใน IAM Policy ครับ

IAM Policy

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

เมื่อลองตั้งค่า Policy ใหม่แล้ว ลองรันคำสั่ง 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 (Japanese)

How can I decode an authorization failure message after receiving an "UnauthorizedOperation" error during an EC2 instance launch? (English)

decode-authorization-message — AWS CLI 1.21.8 Command Reference (English)

AWS IAM คืออะไร การแนะนำฟังก์ชันล่าสุดของ AWS ในปี 2022 | DevelopersIO

บทความต้นฉบับ

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